gpt4 book ai didi

java - 使用 MapReduce 将数据批量插入 HBase

转载 作者:行者123 更新时间:2023-11-29 05:40:15 25 4
gpt4 key购买 nike

我需要向 HBase 表中插入 4 亿行。

架构看起来像这样

我通过简单地将 int 和 int 以及值连接为 System.nanoTime() 来生成 key

我的映射器看起来像这样

public class DatasetMapper extends Tablemapper <Text,LongWritable> {


private static Configuration conf = HBaseConfiguration.create();


public void map (Text key, LongWritable values, Context context) throws exception {

// instantiate HTable object that connects to table name
HTable htable = new HTable(conf,"temp") // already created temp table
htable.setAutoFlush(flase);
htable.setWriteBufferSize(1024*1024*12);

// construct key
int i = 0, j = 0;
for(i=0; i<400000000,i++) {
String rowkey = Integer.toString(i).concat(Integer.toString(j));
Long value = Math.abs(System.nanoTime());
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes("location"),Bytes.toBytes("longlat"),Bytes.toBytes(value);
htable.put(put)
j++;
htable.flushCommits();
}
}

我的工作是这样的

Configuration config = HBaseConfiguration.create();
Job job = new Job(config,"initdb");
job.setJarByClass(DatasetMapper.class); // class that contains mapper

TableMapReduceUtil.initTableMapperJob(
null, // input table
null,
DatabaseMapper.class, // mapper class
null, // mapper output key
null, // mapper output value
job);
TableMapReduceUtil.initTableReducerJob(
temp, // output table
null, // reducer class
job);
job.setNumReduceTasks(0);

boolean b = job.waitForCompletion(true);
if (!b) {
throw new IOException("error with job!");
}

作业运行但插入 0 条记录。我知道我犯了一些错误,但由于我是 HBase 的新手,所以无法发现它。请帮助我。

谢谢

最佳答案

首先,您的映射器名称是DatasetMapper,但在您的作业配置中您指定了DatabaseMapper。我想知道它是如何正常工作的。

接下来,您似乎将 TableMapper 和 Mapper 的用法混合在一起了。 Hbase TableMapper 是一个抽象类,它扩展了 Hadoop Mapper 并帮助我们方便地从 HBase 读取,TableReducer 帮助我们回写到 HBase。您正在尝试从 Mapper 中放入数据,同时您正在使用 TableReducer。您的映射器实际上永远不会被调用。

要么使用 TableReducer 来放置数据,要么只使用 Mapper。如果你真的想在你的 Mapper 中这样做,你可以使用 TableOutputFormat 类。请参阅 HBase 权威指南第 301 页给出的示例。这是 Google Books link

HTH

附言:您可能会发现这些链接有助于正确学习 HBase+MR 集成:

Link 1.

Link 2.

关于java - 使用 MapReduce 将数据批量插入 HBase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17883662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com