在HDFS Java中向现有文件追加数据
我无法将数据附加到HDFS中的现有文件中。我希望,如果文件存在,然后追加一行,如果没有,创建一个具有给定名称的新文件 下面是我写入HDFS的方法在HDFS Java中向现有文件追加数据,java,hadoop,hdfs,filewriter,Java,Hadoop,Hdfs,Filewriter,我无法将数据附加到HDFS中的现有文件中。我希望,如果文件存在,然后追加一行,如果没有,创建一个具有给定名称的新文件 下面是我写入HDFS的方法 if (!file.exists(path)){ file.createNewFile(path); } FSDataOutputStream fileOutputStream = file.append(path); BufferedWriter br = new BufferedWriter(new OutputStreamWriter(
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();
实际上,这个方法写入HDFS并创建一个文件,但正如我提到的,它并没有附加
这就是我测试我的方法的方式:
RunTimeCalculationHdfsWrite.hdfsWriteFile("RunTimeParserLoaderMapperTest2", "Error message test 2.2", context, null);
第一个参数是文件名,第二个参数是消息名,另外两个参数并不重要
有人知道我遗漏了什么或做错了什么吗?HDFS不允许
append
操作。实现与附加相同功能的一种方法是:
- 检查文件是否存在李>
- 如果文件不存在,则创建新文件并写入新文件
- 如果文件存在,请创建一个临时文件李>
- 从原始文件中读取行并将该行写入临时文件(不要忘记换行符)
- 写入要附加到临时文件的行
- 最后,删除原始文件并将临时文件移动(重命名)到原始文件
分布式文件系统#append
方法:
FSDataOutputStream append(Path f, final int bufferSize, final Progressable progress) throws IOException
有关详细信息,请参阅
也可以通过命令行追加:
hdfs dfs -appendToFile <localsrc> ... <dst>
hdfs-dfs-appendToFile。。。
直接从标准输入添加行:
echo "Line-to-add" | hdfs dfs -appendToFile - <dst>
echo“要添加的行”| hdfs dfs-appendToFile-
已解决强>
HDFS中支持追加
您只需执行一些配置和简单代码,如下所示:
步骤1:在hdfs-site.xml中将dfs.support.append设置为true:
<property>
<name>dfs.support.append</name>
<value>true</value>
</property>
或者,您也可以在运行时通过java代码执行相同的操作:
fsShell.setrepr((short) 1, filePath);
步骤3:创建数据/将数据附加到文件中的代码:
public void createAppendHDFS() throws IOException {
Configuration hadoopConfig = new Configuration();
hadoopConfig.set("fs.defaultFS", hdfsuri);
FileSystem fileSystem = FileSystem.get(hadoopConfig);
String filePath = "/test/doc.txt";
Path hdfsPath = new Path(filePath);
fShell.setrepr((short) 1, filePath);
FSDataOutputStream fileOutputStream = null;
try {
if (fileSystem.exists(hdfsPath)) {
fileOutputStream = fileSystem.append(hdfsPath);
fileOutputStream.writeBytes("appending into file. \n");
} else {
fileOutputStream = fileSystem.create(hdfsPath);
fileOutputStream.writeBytes("creating and writing into file\n");
}
} finally {
if (fileSystem != null) {
fileSystem.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
请告诉我任何其他帮助
干杯 首先,您需要知道hdfs是一次性写入文件系统。我们无法在hdfs中追加或覆盖。然而,我们可以尽可能多地阅读。请阅读《Hadoop:这方面的权威指南》一书。变量
文件的类型是什么?
?检查Ok,实际上我修改了hdfs-site.xml,添加了两个属性,它对我有效,这是我使用的两个属性:dfs.replication 2 dfs.support.append true刚刚测试了另一个响应,而您的响应是并列的,所以我必须测试并确保-appendToFile确实有效+源代码片段为1。什么是fShell?它的fsShell是一个输入错误
public void createAppendHDFS() throws IOException {
Configuration hadoopConfig = new Configuration();
hadoopConfig.set("fs.defaultFS", hdfsuri);
FileSystem fileSystem = FileSystem.get(hadoopConfig);
String filePath = "/test/doc.txt";
Path hdfsPath = new Path(filePath);
fShell.setrepr((short) 1, filePath);
FSDataOutputStream fileOutputStream = null;
try {
if (fileSystem.exists(hdfsPath)) {
fileOutputStream = fileSystem.append(hdfsPath);
fileOutputStream.writeBytes("appending into file. \n");
} else {
fileOutputStream = fileSystem.create(hdfsPath);
fileOutputStream.writeBytes("creating and writing into file\n");
}
} finally {
if (fileSystem != null) {
fileSystem.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}