Some basic operations for Hadoop

Source: Internet
Author: User

Here are some of the basic Hadoop file operations, mainly upload files, download files and delete files, but to use the following features need to pay attention to some places:

1, to note whether the Windows system has been configured with the domain name and IP mapping relationship;

2, in order to operate the file on the HDFS system, you can use "/" instead of the root path of HDFs (Hdfs://hadoop01:9000/)


Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
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.junit.Before;
Import Org.junit.Test;


public class Hadoopdemo {
private static FileSystem fs = null;
@Before
public void Init () throws exception{
If there is no third parameter, there will be no permission error when uploading the file, but it is not safe to use
Hadoop provides a framework called Kerberos to address its security concerns
FS = Filesystem.get (New URI ("hdfs://hadoop1:9000"), New Configuration (), "root");
}

/*
* Download File
*/
@Test
public void DownLoad () throws exception{
InputStream in = Fs.open (new Path ("/jdk.bin")); Open an input stream
OutputStream out = new FileOutputStream ("c:\\jdk_1.7"); Create a file output stream
Ioutils.copybytes (in, out, 4096, true); The third parameter represents the number of bytes cached, and the fourth parameter indicates that the stream is automatically closed after the current download is complete
}

/*
* Upload Files
*/
@Test
public void UpLoad () throws exception{
InputStream in = new FileInputStream ("E://android-sdk_r24.3.3-windows.zip"); Create a local file input stream
OutputStream out = fs.create (new Path ("/ni/android.zip")); To create an HDFS output stream
Ioutils.copybytes (in, out, 4096, true);
}

/*
* Delete Files
*/
@Test
public void Delete () throws exception{
Fs.delete (New Path ("/jdk.bin"), true);
}
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub

}

}

Some basic operations for Hadoop

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.