Use of the Java API in HDFs

Source: Internet
Author: User

Transferred from:http://www.cnblogs.com/liuling/p/2013-6-17-01.html

Related source code: http://www.cnblogs.com/ggjucheng/archive/2013/02/19/2917020.html

HDFs is a distributed file system that, since it is a file system, can manipulate its files, such as creating new files, deleting files, and reading the contents of files. The process of using the Java API to manipulate files in HDFs is documented below.

The file operations in the sub-HDFs mainly involve several classes:

Configuration class: Objects of this class are marshaled to the client or server configurations.

FileSystem class: The object of this class is a file system object that can be manipulated by some methods of the object. FileSystem fs = Filesystem.get (conf); Gets the object through a static method of FileSystem.

Fsdatainputstream and Fsdataoutputstream: These two classes are input and output streams in HDFs. It is obtained by filesystem open method and create method respectively.

How to do the file operation clear the following example:

  1 package Com.hdfs;  2 3 Import Java.io.FileInputStream;  4 Import java.io.IOException;  5 Import Java.io.InputStream;  6 7 Import Org.apache.hadoop.conf.Configuration;  8 Import Org.apache.hadoop.fs.FSDataOutputStream; 9 Import Org.apache.hadoop.fs.FileStatus; Ten import Org.apache.hadoop.fs.FileSystem; Import Org.apache.hadoop.fs.Path; Import Org.apache.hadoop.io.IOUtils; public class Hdfstest {15 16//Create new file-public static void CreateFile (String DST, byte[] contents) t Hrows ioexception{Configuration conf = new configuration (); FileSystem fs = Filesystem.get (conf); 2 0 Path Dstpath = new path (DST); Target path 21//open an output stream fsdataoutputstream OutputStream = fs.create (Dstpath); Outputstream.write (contents); Outputstream.close (); Fs.close (); System.out.println ("File created successfully! "); 27} 28 29//upload local file public static void UploadFile (String src,string DST) thRows ioexception{Configuration conf = new configuration (), FileSystem fs = Filesystem.get (conf); 33 Path Srcpath = new path (SRC); Original path Dstpath Path = new Path (DST); Target path 35//Call file System file copy function, the preceding parameter refers to whether to delete the original file, true to delete, the default is False Fs.copyfromlocalfile (False,srcpath, Dstpath); 37 38//Print file path System.out.println ("Upload to" +conf.get ("Fs.default.name")); System.out.println ("------------list files------------" + "\ n"); Filestatus [] filestatus = Fs.liststatus (Dstpath);         Filestatus File:filestatus (SYSTEM.OUT.PRINTLN) (File.getpath ()); 45 } fs.close ();         47} 48 49//file renamed in public static void rename (String oldname,string newName) throws ioexception{51 Configuration conf = new configuration (); FileSystem fs = Filesystem.get (conf); OldPath Path = new Path (oldname); New PathPath = new Path (newName); A. Boolean isOK = Fs.rename (OldPath, NewPath); isOK {System.out.println ("rename ok!"),}else{( "Rename failure"); Fs.close ();  62} 63//delete file public static void Delete (String filePath) throws ioexception{Configuration conf = new Configuration (); FileSystem fs = Filesystem.get (conf); * * * Path PATH = new Path (FilePath); isOK Boolean = fs.deleteonexit (path); if (isOK) {System.out.println ("delete ok!");}else{System.out.println ( "Delete Failure"); Fs.close (); 75} 76 77//Create directory throws public static void mkdir (String path) ioexception{Configuration C onf = new Configuration (); FileSystem fs = Filesystem.get (conf); Bayi Path Srcpath = new Path (path); isOK Boolean = Fs.mkdirs (srCPath); if (isOK) {System.out.println ("Create dir ok!"),}else{System.out.prin Tln ("Create dir failure"); Fs.close (); 89} 90 91//Read the contents of the file ReadFile public static void (String FilePath) throws ioexception{Confi Guration conf = new Configuration (); 94 FileSystem fs = Filesystem.get (conf); Srcpath Path = new Path (FilePath); InputStream in = null; 98 in = Fs.open (Srcpath), Ioutils.copybytes (in, System.out, 4096, false);//Copy To the standard output stream, the finally {101 Ioutils.closestream (in), 102}103}104, 106 public static void Main (string[] args) throws IOException {107//test upload file 108//uploadfile ("D:\\c.txt", "/user/hadoo p/test/"); 109//Test Create file/*byte[] contents =" Hello World hi \ n ". GetBytes (); 111 CreateFile ("/US Er/hadoop/test1/d.txt ", contents); */112//Test rename 113//rename ("/user/hadoop/test/d.txt", "/user/hadoop/test/dd.txt"); 114//Test Delete file 115 Delete ("Test/dd.txt");    Use relative path to//delete ("test1"); Delete Directory 117//test new directory 118//mkdir ("Test1"); 119//test Read file ReadFile ("Test1/d.txt"); 121}12 2 123}

Use of the Java API in HDFs

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.