Hadoop example program code

Source: Internet
Author: User

Hadoop example code:

1. creatinga configuration object: to be able to read from or write to HDFS, you need tocreate a configuration object and pass configuration parameter to it usinghadoop configuration files.

ImportOrg. Apache. hadoop. conf. configuration;

ImportOrg. Apache. hadoop. fs. path;

 

Public
Class
Main {

Public
Static void
Main (string [] ARGs ){

Configurationconf =NewConfiguration ();

Conf. addresource (NewPATH ("C: \ documentsand Settings \ mz50947 \ Desktop \ hadoop-1.2.1 \ conf \ core-site.xml "));

Conf. addresource (NewPATH ("C: \ documentsand Settings \ mz50947 \ Desktop \ hadoop-1.2.1 \ conf \ hdfs-site.xml "));

System.Out. Println (CONF );

}

}

Output:

Configuration: core-default.xml, core-site.xml, C:/Documents andsettings/mz50947/desktop/hadoop-1.2.1/CONF/core-site.xml, C:/Documents andsettings/mz50947/desktop/hadoop-1.2.1/CONF/hdfs-site.xml

 

2. addingfile to HDFS: Create a filesystem object and use a file stream to add a file.

ImportJava. Io. bufferedinputstream;

ImportJava. Io. file;

ImportJava. Io. fileinputstream;

ImportJava. Io. ioexception;

ImportJava. Io. inputstream;

 

ImportOrg. Apache. hadoop. conf. configuration;

ImportOrg. Apache. hadoop. fs. fsdataoutputstream;

ImportOrg. Apache. hadoop. fs. filesystem;

ImportOrg. Apache. hadoop. fs. path;

 

Public
Class
Main {

Public
Static void
Main (string [] ARGs)
ThrowsIoexception {

Configurationconf =NewConfiguration ();

Conf. addresource (NewPATH ("C: \ documentsand Settings \ mz50947 \ Desktop \ hadoop-1.2.1 \ conf \ core-site.xml "));

Conf. addresource (NewPATH ("C: \ documentsand Settings \ mz50947 \ Desktop \ hadoop-1.2.1 \ conf \ hdfs-site.xml "));

System.Out. Println (CONF );

Filesystem = filesystem.Get(CONF );

// Check ifthe file already exists

Path =NewPATH ("C :\\ documents andsettings \ mz50947 \ Desktop \ output \ file. Ext ");

If(Filesystem. exists (PATH )){

System.Out. Println ("file" +
Path + "already exists ");

Filesystem. Delete (path,
True
);

}

 

// Create anew file and write data to it.

Fsdataoutputstream out = filesystem. Create (PATH );

Inputstream in =NewBufferedinputstream (NewFileinputstream (NewFile ("C: \ documentsand
Settings \ mz50947 \ Desktop \ hadoop-1.2.1 \ conf \ hdfs-site.xml ")));

 

Byte[] B =
New byte
[1024];

IntNumbytes = 0;

While(Numbytes = in. Read (B)> 0 ){

Out. Write (B, 0, numbytes );

}

 

// Closeall the fileDescripters

In. Close ();

Out. Close ();

Filesystem. Close ();

}

}

Will create file. file. ext and file under folder output

3. deployto Linux,

(1). c: \ Documents and Settings \ mz50947 \ Desktop> pscpc: \ eclipse \ workspaces \ 3.7 _ may_20 _

Dashboard \ hadoop_begin \ bin \ Main.classyj70978@retailvm1d.nam.nsroot.net:/home/YJ

70978/hadoop/hadoop-1.1.2/src/test

(2)./export/opt/jrockit/6.0 _ 14r27. 6.5l64/bin/jar-CVF main. jar-cwordcount_classes /.

(3)./home/yj70978/hadoop/hadoop-1.1.2/bin/hadoop JAR/home/yj70978/hadoop/hadoop-1.1.2/src/test/Main. Jar main

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.