在HDFS Java中将数据附加到现有文件 [英] Append data to existing file in HDFS Java

查看:192
本文介绍了在HDFS Java中将数据附加到现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将数据追加到HDFS中的现有文件。我希望如果文件存在,然后附加一行,如果没有,创建一个新的文件名称给定。



这是我写入HDFS的方法。

  if(!file.exists(path)){
file.createNewFile(path);
}

FSDataOutputStream fileOutputStream = file.append(path);
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
br.append(Content:+ content +\\\
);
br.close();

实际上,这个方法写入HDFS并创建一个文件,但正如我所提到的那样不是附加的。 >

这是我如何测试我的方法:

  RunTimeCalculationHdfsWrite.hdfsWriteFile(RunTimeParserLoaderMapperTest2 ,Error message test 2.2,context,null); 

第一个参数是文件的名称,第二个消息和其他两个参数不是很重要。



所以任何人都有一个想法,我失踪或做错了什么?

实际上,你可以附加到一个HDFS文件:


从Client的角度来看,append操作首先调用append DistributedFileSystem,这个操作会返回一个流对象FSDataOutputStream。如果客户端需要将数据附加到该文件,它可以调用out.write写入,并调用out.close以关闭。


我检查了HDFS源文件,有 DistributedFileSystem#append 方法:

  FSDataOutputStream append(Path f,final int bufferSize,final Progressable progress)throws IOException 

有关详细信息,请参见 presentation

还有你可以通过命令行追加:

  hdfs dfs -appendToFile< localsrc> ...< dst> 

直接从stdin添加行:

  echoLine-to-add| hdfs dfs -appendToFile  - < dst> 


I'm having trouble to append data to an existing file in HDFS. I want that if the file exists then append a line, if not, create a new file with the name given.

Here's my method to write into HDFS.

if (!file.exists(path)){
   file.createNewFile(path);
}

FSDataOutputStream fileOutputStream = file.append(path); 
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
br.append("Content: " + content + "\n");
br.close();

Actually this method writes into HDFS and create a file but as I mention is not appending.

This is how I test my method:

RunTimeCalculationHdfsWrite.hdfsWriteFile("RunTimeParserLoaderMapperTest2", "Error message test 2.2", context, null);

The first param is the name of the file, the second the message and the other two params are not important.

So anyone have an idea what I'm missing or doing wrong?

解决方案

Actually, you can append to a HDFS file:

From the perspective of Client, append operation firstly calls append of DistributedFileSystem, this operation would return a stream object FSDataOutputStream out. If Client needs to append data to this file, it could calls out.write to write, and calls out.close to close.

I checked HDFS sources, there is DistributedFileSystem#append method:

 FSDataOutputStream append(Path f, final int bufferSize, final Progressable progress) throws IOException

For details, see presentation.

Also you can append through command line:

hdfs dfs -appendToFile <localsrc> ... <dst>

Add lines directly from stdin:

echo "Line-to-add" | hdfs dfs -appendToFile - <dst>

这篇关于在HDFS Java中将数据附加到现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆