HDFs file Operations (Java code Implementation)

Source: Internet
Author: User
Tags hadoop fs

For HDFS operations, you can use Hadoop FS commands, but also use Java to operate, the following small example, is a brief introduction of the Java Operation HDFs files, etc...

Package com.hdfs.nefu;/** * @auther XD **/import java.io.fileinputstream;import java.io.ioexception;import Java.net.uri;import Java.net.urisyntaxexception;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.fsdatainputstream;import Org.apache.hadoop.fs.fsdataoutputstream;import Org.apache.hadoop.fs.filesystem;import Org.apache.hadoop.fs.path;import Org.apache.hadoop.io.ioutils;public Class App {final static string url = "Hdfs://localhost/hello"; final static string Dir_path = "d1/1000"; final static string File_ Path = "d1/1000/f100";p ublic static void Main (string[] args) throws IOException, urisyntaxexception {/*** load file HDFs streaming data so in I need to get the input and output stream when the operation is done * The HDFS operation is only required to obtain the FileSystem object **/final FileSystem FileSystem = Filesystem.get (The new URI (URL), as shown in the following code. New Configuration ()); Create folder mkdir (filesystem);/* Upload a file As long as this file exists, create a file to form an output stream */final fsdataoutputstream out = filesystem.create (New Path ( File_path)); final FileInputStream in = new FileInputStream ("D:\\log.txt"); Uploaddata (The download file requires a file to be opened before it can be downloaded final fsdatainputstream input = Filesystem.open (new Path (File_path));d ownloaddata (input );//Delete folder DeleteFile (filesystem);} /* The following methods simply extract the refactoring method by using shortcut keys * Use shortcut keys where they are called: Alt+shift+m the following situation occurs */private static void DeleteFile (final FileSystem FileSystem) Throws IOException {filesystem.delete (new Path (File_path), true);} private static void Downloaddata (final fsdatainputstream input) throws IOException {ioutils.copybytes (input, System.out , 1024x768, true);} private static void Uploaddata (final fsdataoutputstream out,final FileInputStream in) throws IOException { Ioutils.copybytes (in, out, 1024x768, true);} private static void mkdir (final FileSystem FileSystem) throws IOException {filesystem.mkdirs (new Path (Dir_path));}}


HDFs file Operations (Java code Implementation)

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.