Uploading files to the HDFs file system via Javaapi

Source: Internet
Author: User

Import Java.io.BufferedInputStream;

Import Java.io.FileInputStream;

Import Java.io.InputStream;

Import Java.io.OutputStream;

Import Java.net.URI;


Import org.apache.hadoop.conf.Configuration;

Import Org.apache.hadoop.fs.FileSystem;

Import Org.apache.hadoop.fs.Path;

Import Org.apache.hadoop.io.IOUtils;

Import org.apache.hadoop.util.Progressable;


public class UploadFile {

public static void Main (string[] args) {

try {

String localsrc = "E://temp/117227.jpg";


String DST = "hdfs://ip:8020/tmp/hadoop-xiaohu/117227.jpg";


InputStream in = new Bufferedinputstream (new FileInputStream (LOCALSRC));


Configuration conf = new configuration ();


FileSystem fs = Filesystem.get (Uri.create (DST), conf);


OutputStream out = fs.create (new Path (DST), new progressable () {


public void Progress () {


System.out.print (".");


}


});


Ioutils.copybytes (in, out, 4096, true);


SYSTEM.OUT.PRINTLN ("Success");


} catch (Exception e) {


E.printstacktrace ();


}


}

}

After successful upload, open 50070 view via browser, select

Browse Directory

The file list is displayed.

This article is from "Learning God linux--star Moon Love" blog, please be sure to keep this source http://xuegodxingyue.blog.51cto.com/5989753/1904018

Uploading files to the HDFs file system via Javaapi

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.