设为首页 加入收藏

TOP

Java读写HDFS简单Demo
2015-07-16 12:57:25 来源: 作者: 【 】 浏览:7
Tags:Java 读写 HDFS 简单 Demo

Java读写HDFS简单Demo


package test;


import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;;


public class Test {
?
?
?public void WriteFile(String hdfs) throws IOException {
? Configuration conf = new Configuration();
? FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
? FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
? hdfsOutStream.writeChars("hello");
? hdfsOutStream.close();
? fs.close();?
?}
?
?public void ReadFile(String hdfs) throws IOException {
? Configuration conf = new Configuration();
? FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
? FSDataInputStream hdfsInStream = fs.open(new Path(hdfs));
?
? byte[] ioBuffer = new byte[1024];
? int readLen = hdfsInStream.read(ioBuffer);
? while(readLen!=-1)
? {
? ?System.out.write(ioBuffer, 0, readLen);
? ?readLen = hdfsInStream.read(ioBuffer);
? }
? hdfsInStream.close();
? fs.close();?
?}
?
?public static void main(String[] args) throws IOException {?
? String hdfs = "hdfs://192.168.56.112:9000/test/hello.txt";
? Test t = new Test();?
? t.WriteFile(hdfs);
? t.ReadFile(hdfs);
? }
}


--------------------------------------分割线 --------------------------------------


--------------------------------------分割线 --------------------------------------


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Hibernate4之Hello World(基础环.. 下一篇Linux 网络编程——原始套接字实..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: