Hadoop第一个程序,利用API向HDFS中写入数据
2013-12-27 19:59
483 查看
参考: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();
}
}
原文地址:点击打开链接
这时学习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();
}
}
原文地址:点击打开链接
相关文章推荐
- Hadoop第一个程序,利用API向HDFS中写入数据
- Hadoop源码分析HDFS Client向HDFS写入数据的过程解析
- hadoop小程序---写入hdfs
- Hive数据分析——Spark是一种基于rdd(弹性数据集)的内存分布式并行处理框架,比于Hadoop将大量的中间结果写入HDFS,Spark避免了中间结果的持久化
- 浅析hadoop写入数据api
- 第一个hadoop程序(java程序访问hadoop的hdfs文件系统中的文件)
- Hadoop:第一个程序操作HDFS
- storm 开发系列三 Clojue编写程序读取kafka数据并写入到hdfs
- 大数据入门第五天——离线计算之hadoop(下)hadoop-shell与HDFS的JavaAPI入门
- 利用Hadoop运行第一个程序,计算文章中不同单词数量
- apihadoophadoop通过FileSystem API读取和写入数据
- 浅析hadoop写入数据api
- Hadoop学习笔记——1.java读取Oracle中表的数据,创建新文件写入Hdfs
- Hadoop源码分析HDFS Client向HDFS写入数据的过程解析
- FSDataOutputStream (浅析hadoop写入数据api)
- 第二篇:Hadoop HDFS常用JAVA api操作程序
- 浅析hadoop写入数据api
- HDFS的API对Hadoop文件系统的文件进行写入
- hadoop API 写入HDFS简单注释