有人可以提供一個示例代碼片段來了解如何將文件追加到現有的序列文件中嗎?追加到現有的序列文件
下面是我以前附加到現有的序列文件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