Writehdfs:
PackageAA;ImportJava.io.*;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.fs.*; Public classWritehdfs { Public Static voidMain (string[] args) {Configuration conf=NewConfiguration (); Try{FileSystem fs=filesystem.get (conf); Path F=NewPath ("Ttt.txt"); System.out.println (Fs.isfile (f)); System.out.println (F.tostring ()); Fsdataoutputstream OS= Fs.create (F,true); Os.writechars ("Test"); Os.flush (); Os.close (); } Catch(IOException e) {e.printstacktrace (); } }}
Readhdfs:
PackageAA;ImportJava.io.InputStream;ImportJava.net.URI;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.fs.*;Importorg.apache.hadoop.io.IOUtils; Public classReadhdfs { Public Static voidMain (string[] args)throwsException {String uri= "Ttt.txt"; Configuration conf=NewConfiguration (); FileSystem FS=FileSystem. Get (Uri.create (URI), conf); InputStream in=NULL; Try{ in= Fs.open (NewPath (URI)); Ioutils.copybytes (in, System.out,4096,false); } finally{ioutils.closestream (in); } }}
Write first read output:
for Logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). Log4j:warn Please initialize the log4j system Properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
T e s t
HDFS read-write program small test