用Java编写hdfs文件 [英] Write a file in hdfs with Java
本文介绍了用Java编写hdfs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在HDFS中创建一个文件并在其中写入数据。我使用这段代码:
I want to create a file in HDFS and write data in that. I used this code:
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(config);
Path filenamePath = new Path("input.txt");
try {
if (fs.exists(filenamePath)) {
fs.delete(filenamePath, true);
}
FSDataOutputStream fin = fs.create(filenamePath);
fin.writeUTF("hello");
fin.close();
}
它创建文件,但不会写入任何内容。我搜查了很多,但
没有找到任何东西。我的问题是什么?我需要任何权限写入HDFS吗?
It creates the file, but it does't write anything in it. I searched a lot but didn't find anything. What is my problem? Do I need any permission to write in HDFS?
谢谢。
推荐答案
替代@ Tariq的asnwer,您可以在获取文件系统时传递URI
an alternative to @Tariq's asnwer you could pass the URI when getting the filesystem
Configuration configuration = new Configuration();
FileSystem hdfs = FileSystem.get( new URI( "hdfs://localhost:54310" ), configuration );
Path file = new Path("hdfs://localhost:54310/s2013/batch/table.html");
if ( hdfs.exists( file )) { hdfs.delete( file, true ); }
OutputStream os = hdfs.create( file,
new Progressable() {
public void progress() {
out.println("...bytes written: [ "+bytesWritten+" ]");
} });
BufferedWriter br = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) );
br.write("Hello World");
br.close();
hdfs.close();
这篇关于用Java编写hdfs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文