我正在尝试将 csv 写入 MapReduce 的 reducer 函数中的文件。这是我的代码:
public class DataSet311Reducer extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path path = new Path(key.toString().toLowerCase() + ".csv");
FSDataOutputStream os = fs.create(path);
os.writeChars("KEY,DATE,AGENCY,DESCRIPTOR,LOCATIONTYPE,INCIDENTZIP,INCIDENTADDRESS,LATITUDE,LONGITUDE\n");
StringBuilder sb = new StringBuilder();
for (Text value : values) {
sb.append(value.toString());
sb.append("|");
os.writeUTF(value.toString());
os.writeUTF("\n");
}
os.close();
context.write(key, new Text(sb.toString()));
}
}
我需要以 UTF-8 编码存储文件以与 CartoDB 一起使用。在检查文件头时,它显示给我
unspecified.csv: application/octet-stream; charset=binary
如何使用正确的编码和标头内容存储内容?