我对hadoop很陌生...
我在一个 hbase 表中有一堆数据,我需要将它们导出(进行一些小的转换)到一个平面文件中。为此,我正在构建一个 mapreduce 作业,它扫描表并将数据映射到具有 TextOutputFormat 的 Text 类型。
像这样的东西:
TableMapReduceUtil.initTableMapperJob("tablename", // input table
scan, // Scan instance to control CF and attribute selection
MyMapper.class, // mapper class
Text.class, // mapper output key
Text.class, // mapper output value
job);
job.setNumReduceTasks(1);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileOutputFormat.setOutputPath(job, new Path("/tmp/mydirectory"));
还有我的映射器:
private static class MyMapper extends TableMapper<Text, Text> {
public void map(ImmutableBytesWritable row, Result result, Context context) throws IOException,
InterruptedException {
String json = new String(result.getValue("cf".getBytes(), "qualifier".getBytes()));
StringBuilder line = new StringBuilder();
//...builds the line up
Text k = new Text("filename-20141205.txt");
Text lineText = new Text(line.toString());
context.write(k, lineText);
}
}
但是,我得到的是一个单独的文件,其中的 key 和数据位于 part-r-00000 文件中。我想我需要一个 reducer 来完成这项工作,但我不确定它是什么样子。
身份缩减器会起作用吗?除了 TextOutputFormat 之外,还有更好的方法来解决这个问题吗?
最佳答案
这有效:
private static class MyOutputFormat<K, V> extends TextOutputFormat<K, V>{
@Override
public Path getDefaultWorkFile(TaskAttemptContext context, String extension) throws IOException {
FileOutputCommitter committer =
(FileOutputCommitter) getOutputCommitter(context);
return new Path(committer.getWorkPath(), "my-file-name.txt");
}
}
关于java - hbase 导出到平面文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27327829/