"Big Data Series" Windows environment to build Hadoop development environment read data from Hadoop URLs

Source: Internet
Author: User
Tags readfile

Objective

Build a Java project in a Windows environment after the Hadoop cluster has been built test operations files in HDFs

Version One
Package Com.slp.hadoop274.hdfs;import Java.io.ioexception;import Java.io.inputstream;import java.net.URL;import Java.net.urlconnection;import org.apache.hadoop.fs.fsurlstreamhandlerfactory;import org.junit.Test;/** *  * @ Author Sangliping * Complete HDFs operation */public class Testhdfs {    /**     * Read HDFs file     * @throws ioexception      */@ testpublic void ReadFile () throws Ioexception{url url = new URL ("hdfs://192.168.181.201:8020/user/sanglp/hadoop/ Copyfromlocal "); URLConnection con = url.openconnection (); InputStream is = Con.getinputstream (); byte[] buf = new byte[is.available ()]; Is.read (BUF); Is.close (); String str = new String (buf, "UTF-8"); System.out.println (str);}}

The above error occurs when running the test because the URL does not recognize the HDFS protocol .

Version two,
Package Com.slp.hadoop274.hdfs;import Java.io.ioexception;import Java.io.inputstream;import java.net.URL;import Java.net.urlconnection;import org.apache.hadoop.fs.fsurlstreamhandlerfactory;import org.junit.Test;/** *  * @ Author Sangliping * Complete HDFs operation */public class Testhdfs {static{//Register HDFS protocol Otherwise the URL does not recognize the protocol url.seturlstreamhandlerfactory (new Fsurlstreamhandlerfactory ());}    /**     * Read HDFs file     * @throws IOException *      /@Testpublic void ReadFile () throws Ioexception{url url = new URL ("HDFs ://192.168.181.201:8020/user/sanglp/hadoop/copyfromlocal "); URLConnection con = url.openconnection (); InputStream is = Con.getinputstream (); byte[] buf = new byte[is.available ()]; Is.read (BUF); Is.close (); String str = new String (buf, "UTF-8"); System.out.println (str);}}

This time you can correctly print out the contents of the file copyfromlocal HDFs file.

Attached: You can put the log4j.properties file in etc under the Hadoop decompression file into the project file src file to make the console printing more friendly.

Big data series Windows environments build Hadoop development environment read data from Hadoop URLs

Related Article

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.