您的位置:首页 > 运维架构

Hadoop第一个程序,利用API向HDFS中写入数据

2013-12-27 19:59 423 查看
参考:http://f.dataguru.cn/thread-85493-1-1.html

这时学习Hadoop以来写的第一个成功的程序,程序仿照《Hadoop实战》中的PutMerge程序,这里有几个要注意的地方:

1.hdfs的地址是一个网络地址,如下面的:hdfs://localhost:9000/test3

2.确保不会出现“权限不足”的异常

[java] view
plaincopy





import java.io.IOException;

import java.net.URI;



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;

/**

*

*/



/**

* Hadoop版本1.2.1

* 系统ubuntu 12.04

* JDK 1.7

*

*/

public class PutMerge {



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



Configuration conf = new Configuration();



Path inputDir = new Path("/home/hadoop/input");

String serverPath = "hdfs://localhost:9000/test3";

Path hdfsfile = new Path(serverPath);



FileSystem hdfs = FileSystem.get(URI.create(serverPath), conf);

FileSystem local = FileSystem.getLocal(conf);

FileStatus[] status = local.listStatus(inputDir);

FSDataOutputStream out = hdfs.create(hdfsfile);



for(int i = 0; i < status.length; i++) {

FSDataInputStream in = local.open(status[i].getPath());

byte buffer[] = new byte[256];

int byteread = 0;

while((byteread = in.read(buffer)) > 0) {

out.write(buffer);

}

in.close();

}

out.close();

}



}

原文地址:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: