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.