Environment: Hadoop1.2.1
Example: Displaying files in a Hadoop file system directly using filesystem in a standard output format
Writing Java files
Import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.filesystem;import Org.apache.hadoop.fs.path;import Org.apache.hadoop.io.ioutils;import Java.io.inputstream;import Java.net.URI; public class Filesystemcat {public static void Main (string[] args) throws Exception { String uri = args[0]; Configuration conf = new configuration (); 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);}} }
Set the JAVA_HOME environment variable to add $java_home/bin to the PATH environment variable
Set the class directory for Hadoop (
Hadoop_classpath)
[Email protected] hadoop-1.2.1]# VI conf/hadoop-env.sh# the Java implementation to use. Required.export java_home=/usr/java/jdk1.7.0_72# Extra JAVA CLASSPATH elements. Optional.export hadoop_classpath=/home/grid/hadoop-1.2.1/myclasses
Upload the edited Java file
[[email protected] hadoop-1.2.1]$ mkdir myclasses[[email protected] hadoop-1.2.1]$ CD Myclasses[[email protected] myclasses]$ RZ
compiling Java files
[Email protected] myclasses]$ Javac-classpath. /hadoop-core-1.2.1.jar Filesystemcat.java
Perform:
[[email protected] hadoop-1.2.1]$./bin/hadoop filesystemcat Hdfs://hadoop1:9000/user/grid/in/test3.txthello Hadoop Java API
Call the Hadoop Java API for the first time