Hadoop example program code

來源:互聯網
上載者: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.

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

 

public
class
Main {

     public
static void
main(String[] args){

      Configurationconf = newConfiguration();

      conf.addResource(new Path("C:\\Documentsand Settings\\mz50947\\Desktop\\hadoop-1.2.1\\conf\\core-site.xml"));

        conf.addResource(new Path("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.

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

 

import org.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

 

public
class
Main {

     public
static void
main(String[] args)
throws IOException{

      Configurationconf = newConfiguration();

      conf.addResource(new Path("C:\\Documentsand Settings\\mz50947\\Desktop\\hadoop-1.2.1\\conf\\core-site.xml"));

        conf.addResource(new Path("C:\\Documentsand Settings\\mz50947\\Desktop\\hadoop-1.2.1\\conf\\hdfs-site.xml"));

        System.out.println(conf);

        FileSystem fileSystem = FileSystem.get(conf);

       

        // Check ifthe file already exists

        Path path = new Path("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 = new BufferedInputStream(new FileInputStream(new File("C:\\Documentsand
Settings\\mz50947\\Desktop\\hadoop-1.2.1\\conf\\hdfs-site.xml")));

 

        byte[] b =
new byte
[1024];

        int numBytes = 0;

        while ((numBytes = in.read(b)) > 0) {

            out.write(b, 0, numBytes);

        }

 

        // Closeall the file descripters

        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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.