Operations & Management plug-ins for HDFs via Java

Source: Internet
Author: User

Establish connection, guide package

If you don't need maven, unzip the Hadoop installation package,share==>hadoop==>common==>lib All the copy and paste in the past, share==>hadoop==>hdfs==>lib inside all copy paste past.

If you're using maven to search for Hadoop in maven repository , find common , find the corresponding version

Copy to pom.xml

Re-import Hadoop Client

The first step is to establish the connection and connect the Namenode

before configuring for HDFs via Linux , in core-site.xml , configure

the connection is Namenode.

now use myeclipse, write the running file, first read the file:

Hdfsclient.java

 Packagecom.neuedu.test;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FSDataInputStream;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path; Public classhfdsclient { Public Static voidMain (string[] args) {//TODO auto-generated Method Stubread (); }     Public Static voidRead () {//-------Connection Namenode://1, create a Configuration object (with the configuration file is a principle)Configuration conf=NewConfiguration (); Conf.set ("Fs.defaultfs", "hdfs://192.168.71.141:9000");//Set Configuration ObjectPath path=NewPath ("/hadoop/abc.txt"); //Read the file//FileSystem fs=new//The discovery method cannot be used because it is an abstract method that, when not in use, creates an object with a strange and useless method, and calls a static method to see which one can return                Try{FileSystem fs=filesystem.get (conf);//only used to connect Namenode//because it is a read file, you need to return an input streamFsdatainputstream input = fs.open (path);//f is a path, tell it where the file is//returns a stream//The read text can be converted to a character stream, converting:InputStreamReader isr=NewInputStreamReader (input); //If you want to read by line,BufferedReader br=NewBufferedReader (ISR); //start readingString Str=br.readline ();//read by line                     while(str!=NULL){//as long as it is not emptySystem.out.println (str); STR=br.readline ();//one line per output, read once} br.close ();                    Isr.close (); Input.close ();//The flow needs to be closed}Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}        

After running, the result:

To see if the file is correct:

The file is now written into append and direct write

Append method:

 Public Static voidappend () {Configuration conf=NewConfiguration (); Conf.set ("Fs.defaultfs", "hdfs://192.168.71.141:9000");//Set Configuration Object                Try{FileSystem fs=filesystem.get (conf); Fsdataoutputstream Outpustream= Fs.append (NewPath ("/hadoop/abc.txt"));//On which file to appendOutpustream.writebytes ("123123123");        Outpustream.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }

Then read it again to see if the writing was successful:

Direct Write form:

//write: Write directly     Public Static voidwrite () {Configuration conf=NewConfiguration (); Conf.set ("Fs.defaultfs", "hdfs://192.168.71.141:9000");//Set Configuration Object                Try{FileSystem fs=filesystem.get (conf); Fsdataoutputstream Outpustream= Fs.create (NewPath ("/hadoop/abc.txt"));//On which file to appendOutpustream.writebytes ("123123123");        Outpustream.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }

View Results by Read ():

because switching functions to view the contents of the file is inconvenient, configure it here DFSlocation for easy management of HDFs :

Copy to the plugins in the MyEclipse root directory first ,

Restart MyEclipse after restarting, click windows==>show view==>other==> to have one more view

Double-click this view, right-click New,

The view appears to display the file directory in HDFs to view the contents of the file

Operations & Management plug-ins for HDFs via Java

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.