Common Java APIs of HDFS

Source: Internet
Author: User

1. Use hadoop URL to read data

package hadoop;import java.io.InputStream;import java.net.URL;import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;import org.apache.hadoop.io.IOUtils;public class URLCat {    static {        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());    }    public static void readHdfs(String url) throws Exception {        InputStream in = null;        try {            in = new URL(url).openStream();            IOUtils.copyBytes(in, System.out, 4096, false);        } finally {            IOUtils.closeStream(in);        }    }        public static void main(String[] args) throws Exception {        readHdfs("hdfs://192.168.49.131:9000/user/hadoopuser/input20120828/file01");    }}

The jar packages I use include:

The hadoop-core version must be consistent with the hadoop version installed in the distributed environment. Otherwise, an error is returned:

12/09/11 14:18:59 Info Security. usergroupinformation: JAAS configuration already set up for hadoop, not re-installing.
Exception in thread "Main" Java. Lang. noclassdeffounderror: ORG/Apache/hadoop/thirdparty/guava/common/collect/FIG
At org. Apache. hadoop. HDFS. socketcache. <init> (socketcache. Java: 48)
At org. Apache. hadoop. HDFS. dfsclient. <init> (dfsclient. Java: 240)

The hadoop version installed in the distributed environment is as follows:

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.