Hadoop Learning notes: The HDFs Java API uses

Source: Internet
Author: User

The

Simply uses an example to use the HDFs Java interface.

Package cn.zhoucy.Hadoop;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URI;

Import java.net.URISyntaxException;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.fs.FSDataOutputStream;
Import Org.apache.hadoop.fs.FileSystem;
Import Org.apache.hadoop.fs.Path;
Import Org.apache.hadoop.io.IOUtils;
Import Org.junit.Before;

Import Org.junit.Test;
    public class Hdfsdemo {private filesystem fs = null; @Before public void Init () throws IOException, URISyntaxException, interruptedexception{fs = Filesystem.get (n
    EW URI ("hdfs://123.206.xxx.xxx:9000"), New Configuration (), "root"); @Test public void Testdel () throws IllegalArgumentException, ioexception{boolean flag = Fs.delete (new
        Path ("/words.txt"), true);
    SYSTEM.OUT.PRINTLN (flag); @Test public void Testmkdir () throws IllegalArgumentException, IOException{Boolean flag = Fs.mkdirs (new Path ("/itcast88888888"));
    SYSTEM.OUT.PRINTLN (flag); @Test public void Testupload () throws IllegalArgumentException, ioexception{fsdataoutputstream out = FS

        . Create (New Path ("/words.txt"));

        FileInputStream in = new FileInputStream (New File ("E:/w.txt"));
    Ioutils.copybytes (in, out, 2048, true);
        public static void Main (string[] args) throws Exception {Configuration conf = new Configuration ();
        Conf.set ("Fs.defaultfs", "hdfs://123.206.xxx.xxx:9000");

        Conf.set ("Fs.hdfs.impl", "Org.apache.hadoop.hdfs.DistributedFileSystem");

        Tool class filesystem FileSystem FS = Filesystem.get (conf);
        System.out.println ("Meiwentoi");
        InputStream in = Fs.open (new Path ("/woo");//download, download from HDFs System.out.println ("Chucuo");

        FileOutputStream out = new FileOutputStream (New File ("/home/woo"));

    Ioutils.copybytes (in, out, 2048, true);}

}
 

   This use case test is to store downloaded files from HDFs to the Linux file system. Under pseudo distributed deployments, you can run code directly in Eclipse, test with JUnit, and test the download method inside the main method. In the case of distributed deployment, there will be an error in the run, which may be configured incorrectly, and can be run as a jar pack on the server. Here is the way to play the jar that can be run, the name is called Hadoop. Use Java-jar Hadoop.jar on the server to complete the download function.

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.