Use a program to operate HDFS.

Source: Internet
Author: User

After debugging and installation for half a day, I didn't configure how to directly connect to HDFS through eclipse. Finally, I made a jar package and put it in a Linux Virtual Machine for execution.

Run Java-jar XXX. jar.

The operation on HDFS is relatively simple, mainly for the filesystem class. This is something you understand, and you are naturally very skilled in operating HDFS through programs.

The following is a simple example of reading file content from HDFS. Let's share with you.

Package COM. pzoom. HDFS; import Java. io. bufferedinputstream; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import java.net. uri; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. FS. fsdatainputstream; import Org. apache. hadoop. FS. filestatus; import Org. A Pache. hadoop. FS. filesystem; import Org. apache. hadoop. FS. path; import Org. apache. hadoop. io. ioutils; import Org. apache. hadoop. util. progressable; public class putfiletohdfs {/*** read files from HDFS */Private Static void readfromhdfs () throws filenotfoundexception, ioexception {string DST = "HDFS: // Ubuntu: 9000/"; configuration conf = new configuration (); filesystem FS = filesystem. get (URI. create (DST), conf); string Path = "/readme.txt"; fsdatainputstream hdfsinstream = FS. open (New Path (PATH); ioutils. copybytes (hdfsinstream, system. out, Conf, true);/* outputstream out = new fileoutputstream ("/home/chenlongquan/output"); byte [] iobuffer = new byte [1024]; int readlen = hdfsinstream. read (iobuffer); While (-1! = Readlen) {out. write (iobuffer, 0, readlen); readlen = hdfsinstream. read (iobuffer);} Out. close (); hdfsinstream. close (); FS. close (); */}/*** main function ** @ Param ARGs * @ throws exception */public static void main (string [] ARGs) throws exception {try {// uploadtohdfs (); readfromhdfs ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}finally {}}}

Use a program to operate HDFS.

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.