Method one is implemented by a class ioutils.
Write file Create read File open delete file
Create a directory mkdirs delete a file or directory delete the contents of the directory liststatus display the file system's directory and file metadata information Getfilestatus
PackageHDFs;ImportJava.io.InputStream;Importjava.net.MalformedURLException;ImportJava.net.URL;Importorg.apache.hadoop.fs.FsUrlStreamHandlerFactory;Importorg.apache.hadoop.io.IOUtils; Public classApp1 {/** * @paramargs*/ //first define a URI that connects to Hadoop (the identifier and URL make a difference) Public Static FinalString hdfs_path= "Hdfs://hadoop:9000/hello"; Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stub//URL does not know HDFs://so we need the parser .Url.seturlstreamhandlerfactory (Newfsurlstreamhandlerfactory ()); //use static Class Ioutils.copybytes ()URL url =NewURL (Hdfs_path); InputStream in=Url.openstream (); Ioutils.copybytes (in, System.out,1024,true ); }}
//method Two uses the method in the class filesystem. mkdir. Create. Open Get PackageHDFs;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.net.URI;Importjava.net.URISyntaxException;ImportJava.net.URL;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FSDataInputStream;ImportOrg.apache.hadoop.fs.FSDataOutputStream;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path;ImportOrg.apache.hadoop.fs.FileSystemTestHelper.fileType;Importorg.apache.hadoop.io.IOUtils; Public classAPP2 { Public Static FinalString hdfs_path= "hdfs://hadoop:9000"; Public Static FinalString dir_path= "/d1000"; Public Static FinalString file_path= "/d1000/f1000"; Public Static voidMain (string[] args)throwsException, urisyntaxexception {/*final FileSystem FileSystem = Filesystem.get (new URI (Hdfs_path), New Configuration ()); Create file mkdir filesystem.mkdirs (new Path (Dir_path)); Upload file Create Fsdataoutputstream out = Filesystem.create (new Path (File_path)); FileInputStream in= New FileInputStream ("C:/users/administrator/desktop/01.txt"); Ioutils.copybytes (in,out,1024,true); Download File Open final Fsdatainputstream in2= filesystem.open (New Path (File_path)); Ioutils.copybytes (in2, System.out, 1024,true); Delete the file delete filesystem.delete (new Path (File_path), true);*/FileSystem FileSystem= Filesystem.get (NewURI (Hdfs_path),NewConfiguration ()); //mkdirFilesystem.mkdirs (NewPath (Dir_path)); //upload create creates a target channelFsdataoutputstream out = Filesystem.create (NewPath (File_path)); FileInputStream in=NewFileInputStream ("C:/users/administrator/desktop/01.txt"); Ioutils. Copybytes (In,out,1024,true); //download File open one channelFsdatainputstream in2 = Filesystem.open (NewPath (File_path)); Ioutils.copybytes (in2, System.out,1024,true); //Remove DeleteFilesystem.delete (NewPath (Dir_path),true ); }}
Operating HDFs under Eclipse