hadoopはファイルを作成し、ファイルが存在する場合はコンテンツを追加します.

1405 ワード


public static void createNewFile(String path, String content, FileSystem fs) throws Exception{
Configuration conf = new Configuration();
conf.set("dfs.support.append", "true");
Path d_path = new Path(path);
FSDataOutputStream os = null;
if(fs.exists(d_path)){
try {
os = fs.append(d_path);
os.write(content.getBytes("UTF-8"));
} catch (Exception e) {
// TODO: handle exception
appendFileContent(path, content, fs);
}

}else{
os = fs.create(d_path);

os.write(content.getBytes("UTF-8"));

}
if(os != null){
os.close();
}
}

public static void appendFileContent(String path, String content, FileSystem fs) throws Exception{
Configuration conf = new Configuration();
Path d_path = new Path(path);
if(fs.exists(d_path)){
byte[] bytes = readHDFSFile(path);
FSDataOutputStream os = fs.create(d_path);
os.write(bytes);
os.write("
".getBytes("UTF-8"));
os.write(content.getBytes("UTF-8"));
os.close();
}else{
createNewFile(path, content, fs);
}
}