java - hbase 导出到平面文件

标签 java hadoop hbase

我对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/

相关文章:

HBase 未在 Windows 中启动

java - Hbase MasterNotRunningException 尽管 Hmaster、regionserver 和 Zookeeper 已启动

java - 在 Java 中打印出星期几

java - 'createVideoSource(boolean )' in ' org.webrtc.PeerConnectionFactory' 不能应用于 '(org.webrtc.CameraVideoCapturer)'

hadoop - 尽管映射器完成,但Count *查询卡在reducer 0%处

java - Windows 上的 Hbase 集成测试

java - 从 Windows 运行时出现 Phoenix CsvBulkLoadTool 错误

java - 如何比较泛型值和对象

java - 二分查找给出了错误的输出 Java

hadoop - 插入 Hive 表错误