Call the Hadoop Java API for the first time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.