我需要將4億行插入到HBase表中。使用MapReduce將數據批量插入HBase
架構看起來像這樣
在那裏,我通過簡單地串聯INT生成密鑰,int和值System.nanoTime()
我的映射器看起來像這樣
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的新手。請幫幫我。
感謝
嗨塔裏克感謝指出了這一點對我和有用的鏈接。我看到的所有示例都使用HBase作爲源和接收器,我對此感到困惑。讓我按照自己的方式做吧..將用正確的代碼發佈我的結果。 –
歡迎@ Shashank.Kr。當然。 – Tariq