Nidhi
Nidhi

Reputation: 276

Error displaying files in Hadoop filesystem on standard output using FileSystem directly

I'm new to Hadoop and was trying to display file contents in Hadoop filesystem on standard output using the FileSystem directly but am getting the below error.

import java.io.InputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


public class FileSystemCat {

		public static void main(String[] args) throws Exception {
			Configuration conf = new Configuration();
		    conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/core-site.xml"));
		    conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/hdfs-site.xml"));
		  String uri = "/books/pg5000.txt";
		FileSystem fs = FileSystem.get(URI.create(uri), conf);
		InputStream in = null;
		try {
			in = fs.open(new Path(uri));
			IOUtils.copyBytes(in, System.out, 4096, false);
			} finally {
			IOUtils.closeStream(in);
			}
		}
}
	
	

Error -

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.apache.hadoop.security.authentication.util.KerberosName.<clinit>(KerberosName.java:43)
    at org.apache.hadoop.security.UserGroupInformation.initialize(UserGroupInformation.java:261)
    at org.apache.hadoop.security.UserGroupInformation.ensureInitialized(UserGroupInformation.java:248)
    at org.apache.hadoop.security.UserGroupInformation.loginUserFromSubject(UserGroupInformation.java:763)
    at org.apache.hadoop.security.UserGroupInformation.getLoginUser(UserGroupInformation.java:748)
    at 

    org.apache.hadoop.security.UserGroupInformation.getCurrentUser(UserGroupInformation.java:621)
        at org.apache.hadoop.fs.FileSystem$Cache$Key.<init>(FileSystem.java:2753)
        at org.apache.hadoop.fs.FileSystem$Cache$Key.<init>(FileSystem.java:2745)
        at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2611)
        at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:370)
        at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:169)
        at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:354)
        at FileSystemCat.main(FileSystemCat.java:17)
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 13 more

Can someone guide me, where am I wrong?

Upvotes: 1

Views: 1179

Answers (1)

Rajesh N
Rajesh N

Reputation: 2574

java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

This error occurs if you do not have slf4j-api-x.x.x in your classpath. Try to add this jar in your classpath first. This jar can be found in $HADOOP_HOME/share/hadoop/common/lib (my hadoop version is 2.6.0).

You could also try this:

java -cp /path/to/hadoop/common/lib/slf4j-api-x.x.x.jar:. yourjavaclassname

Upvotes: 2

Related Questions