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

来源:互联网 发布:linux怎么ping大包 编辑:程序博客网 时间:2024/06/02 15:50

参考:http://f.dataguru.cn/thread-85493-1-1.html

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

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

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


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();}}


原创粉丝点击