個人第一個hadoop程序

利用virtualbox+win的開發環境搭設好咯,在win下作開發,利用linux跑hadoop,僞分佈式的.java

上第一個程序linux

package org.apache.hadoop.examples;

import java.io.IOException;

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;

public class PutMerge {
    public static void main(String[] args) {
        //首先定義輸入和輸入的文件和文件夾
        String inputdir = "hdfs://master:9000/user/design";
        String outputdir = "hdfs://master:9000/user/design/out";
        //定義path
        Path input = new Path(inputdir);
        Path output = new Path(outputdir);
        //獲得配置文件
        Configuration conf = new Configuration();
        FileSystem hdfs = null;
        //FileSystem local = null;
        try {
            //根據path獲得文件系統,是與文件系統交互的類
            hdfs = input.getFileSystem(conf);
            //local = FileSystem.getLocal(conf);
            //獲得文件夾下的FileStatus對象,其實包含了文件的一些path,大小,更新時間,權限等信息
            FileStatus[] inputfiles = hdfs.listStatus(input);
            //建立文件夾
            //按照默認的快大小,已經默認覆蓋,建立了一個文件
            FSDataOutputStream out = hdfs.create(output);
            //讀取而且寫入的過程
            for(FileStatus fs : inputfiles){
                System.out.println("文件:"+fs.getPath().getName());
                FSDataInputStream fin = hdfs.open(fs.getPath());
                byte[] buffer = new byte[256];
                int bytereaded = 0;
                while((bytereaded=fin.read(buffer))>0){
                    System.out.println("寫入"+bytereaded);
                    out.write(buffer,0,bytereaded);
                }
                fin.close();
            }
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索