有人可以提供一个示例代码片段来了解如何将文件追加到现有的序列文件中吗?追加到现有的序列文件
下面是我以前附加到现有的序列文件OUTPUTFILE的代码,但在读取序列文件追加它是扔校验和错误之后:
问题打开校验文件:/用户/ {home目录} /桌面/采样/ SequenceFile/OUTPUTFILE。忽略例外:java.io.EOFException的
public class AppendSequenceFile {
/**
* @param args
* @throws IOException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws IOException,
InstantiationException, IllegalAccessException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path inputFile = new Path("/Users/{homedirectory}/Desktop/Sample/SequenceFile/sampleAppendTextFiles");
Path sequenceFile = new Path("/Users/{homedirectory}/Desktop/Sample/SequenceFile/outputfile");
FSDataInputStream inputStream;
Text key = new Text();
Text value = new Text();
SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf,
sequenceFile, key.getClass(), value.getClass());
FileStatus[] fStatus = fs.listStatus(inputFile);
for (FileStatus fst : fStatus) {
String str = "";
System.out.println("Processing file : " + fst.getPath().getName() + " and the size is : " + fst.getPath().getName().length());
inputStream = fs.open(fst.getPath());
key.set(fst.getPath().getName());
while(inputStream.available()>0) {
str = str+inputStream.readLine();
}
value.set(str);
writer.append(key, value);
}
}
}
序列文件阅读器:
public class SequenceFileReader{
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path path = new Path("/Users/{homedirectory}/Desktop/Sample/SequenceFile/outputfile");
SequenceFile.Reader reader = null;
try {
reader = new SequenceFile.Reader(fs, path, conf);
Text key = new Text();
Text value = new Text();
while (reader.next(key, value)) { System.out.println(key);
System.out.println(value);
}
} finally {
IOUtils.closeStream(reader);
}
}
}
在此先感谢。
您似乎在要求某人为您编写代码。你有什么试图寻找这个解决方案? –
我试图追加它,但在追加后读取序列文件时,我得到校验和错误 - – user3400887
打开校验和文件时出现问题:/ Users/{homedirectory}/Desktop/Sample/SequenceFile/outputfile。忽略异常:java.io.EOFException。这就是我在这里发布这个问题的原因。用我在一段时间试过的代码编辑问题。 – user3400887