HDFs Java Operations

Source: Internet
Author: User

1. Read the hello file from the Hadoop directory:

Scenario one (can only be read and cannot be written):

Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.io.*;

Import Org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
Import Org.apache.hadoop.io.IOUtils;

public class App1 {
public static final String hdfs_path= "Hdfs://mlj:9000/hello";
public static void Main (string[] args) throws Exception {
url.seturlstreamhandlerfactory (New Fsurlstreamhandlerfactory ()); URL parser, URL usually only recognizes HTTP, is declared here to let it recognize the HDFS protocol
Final URL url=new url (hdfs_path);
Final InputStream In=url.openstream ();
ioutils.copybytes (In, System.out, 1024x768, true);//input stream, output stream, buffer size, whether closed

}

}

----------------------------------

Scenario two (all operations are performed using the Filesytem class provided with HDFs) Alt+shift+m alt+shift+l:

Package HDFs;

Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
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.FileStatus;
Import Org.apache.hadoop.fs.FileSystem;
Import Org.apache.hadoop.fs.Path;
Import Org.apache.hadoop.io.IOUtils;

public class App2 {

/**
* @param args
*/
public static final String hdfs_path= "Hdfs://mlj:9000/hello";
public static final String dir_path= "/d";
public static final String file_path= "/d1";
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
Final FileSystem FileSystem =FileSystem. Get (New URI (Hdfs_path), New Configuration ());//Get Object
Create a folder
mkdir (FileSystem);
Upload files (to upload, create first)
UploadFile (FileSystem);
Download file (to download, open first)
Downloaddata (FileSystem);
deleting files
DeleteFile (FileSystem);
Browse folders
List (FileSystem);
}
private static void list (FileSystem FileSystem) throws IOException {
Final filestatus[] Liststatus = filesystem.liststatus (New Path ("/"));
for (Filestatus filestatus:liststatus) {
String isdir = Filestatus.isdir ()? " Folder ":" File ";
Final String Permission = Filestatus.getpermission (). toString ();
Final short replication = Filestatus.getreplication ();
Final Long len = Filestatus.getlen ();
Final String Path = Filestatus.getpath (). toString ();
System.out.println (isdir+ "\ t" +permission+ "\ T" +replication+ "\ T" +len+ "\ T" +path);
}
}
private static void DeleteFile (final FileSystem FileSystem)
Throws IOException {
FileSystem.Delete(New Path (File_path), true);
}
private static void Downloaddata (final FileSystem FileSystem)
Throws IOException {
Final Fsdatainputstream in = Filesystem.open (new Path (File_path));
Ioutils.copybytes (in, System.out, 1024x768, true);
}
private static void mkdir (final FileSystem FileSystem) throws IOException {
FileSystem.mkdirs(New Path (Dir_path));
}
private static void UploadFile (final FileSystem FileSystem)
Throws IOException, FileNotFoundException {
Final Fsdataoutputstream out = FileSystem.Create(New Path (File_path));//create generates an output stream, a file destination
Final FileInputStream in = new FileInputStream ("D:/b.txt");
ioutils. Copybytes (in, out, 1024x768, true);
}

}

HDFs Java Operations

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.