The next steps in "Hadoop Introductory learning Note---part3" continue to learn how to manipulate HDFs in a program with Java.
As we all know, the operation of the file is nothing more than creating, viewing, downloading, deleting. Let's start with the Java program, provided that the Hadoop pseudo-distribution environment is already set up in the virtual machine in the Hadoop introductory learning Note---part2, and that several processes for Hadoop in the Linux operating system are now fully activated.
All right, no more nonsense! The actual example goes up.
Create a new Java project in MyEclipse:
Create a new LIB package for the project to hold the relevant jar package for the project, I will not say much about build path. The relevant jar packages are as follows:
1. Create a file:
Public Static FinalString Hdfs_path = "Hdfs://hadoop:9000/hello"; Create a URL for a file Public Static voidMain (string[] args) {Try{url.seturlstreamhandlerfactory (Newfsurlstreamhandlerfactory ()); FinalURL url =NewURL (Hdfs_path); FinalInputStream in =Url.openstream (); Ioutils.copybytes (in, System.out,1024,true); } Catch(Exception e) {e.printstacktrace (); }
See the effect in the browser: Enter http://hadoop:50070/and click Browse the filesystem to see the file you just created in the HDFs root directory.
2. Uploading a file: the first thing to understand is that the upload needs to be created in HDFs and then written in the form of a stream.
public static final String hdfs_path = "Hdfs://hadoop:9000/" ; final FileSystem FileSystem = filesystem.get (new URI (hdfs_path), new Configuration ()); // upload file Fsdataoutputstream out = filesystem.create (new Path (File_path)); final FileInputStream in = new FileInputStream ("H:/redtestfileforhadoop" ); Ioutils.copybytes (in, Out, 1024x768, true );
The effect is as follows: Open directly in the browser.
(Upload 1)
---------------------------------------Split Line---------------------------------------------------------
(Upload 2)
3. Download the file , direct output to the console, you can also write this to a file alone, here is not introduced, the code is as follows:
Public Static Final String Hdfs_path = "Hdfs://hadoop:9000/"final FileSystem FileSystem = Filesystem.get (new URI (Hdfs_path),new// download file final fsdatainputstream in = Filesystem.open (new Path (File_path)); Ioutils.copybytes (in, System.out,true);
As follows:
4. Delete files:
Public Static Final String Hdfs_path = "Hdfs://hadoop:9000/"final FileSystem FileSystem = Filesystem.get (new URI (Hdfs_path),new Configuration ()); Filesystem.delete (newtrue);
The effect is needless to say, the file is not visible directly in the browser.
A lot of times seems to be very complex things in fact, the recognition of their own hearts, in fact, it is not so difficult to imagine! Enjoy yourself. The instant feeling of this and our usual use of the cloud network disk is somewhat the same. Haha, may as well be a own cloud network disk. It's absolutely possible!
itred Email:[email protected] Blog:http://www.cnblogs.com/itred personal website:http://wangxingyu.jd-app.com* * * copyright notice: This article is copyrighted by the author and the blog Park, Welcome to reprint, but please mark the article in a conspicuous position. I reserve all rights to be held accountable for his use without my written consent.
Getting Started with Hadoop learning notes---part4