Operating HDFs under Eclipse

Source: Internet
Author: User

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

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.