分享

请问hbase用java api如何实现批量插入数据

Hentai 发表于 2016-12-8 15:03:06 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 4 36574
场景:只有一个列族,一个word列和一个count列,word的所有值在一个set集合里面,count的所有值在一个list里面,如何用java api批量插入这两列的所有值?

已有(4)人评论

跳转到指定楼层
langke93 发表于 2016-12-8 16:34:17
分别循环两个集合即可
比如set集合  
    Put put=new Put(Bytes.toBytes("word"));
    for (String str : set) {  
        put.add(Bytes.toBytes("列簇"), Bytes.toBytes("word"), Bytes.toBytes(str));
}  

    table.put(put);

    table.close();

另外一个同理。

回复

使用道具 举报

Hentai 发表于 2016-12-9 10:06:55
本帖最后由 Hentai 于 2016-12-9 10:08 编辑
langke93 发表于 2016-12-8 16:34
分别循环两个集合即可
比如set集合  
  

好像你这个只有一个rowkey 我可以把  Put put=new Put(Bytes.toBytes(rowkey));放到那个for循环里面吗?我的rowkey是一直变化的
回复

使用道具 举报

nextuser 发表于 2016-12-11 16:56:48
本帖最后由 nextuser 于 2016-12-11 16:59 编辑
Hentai 发表于 2016-12-9 10:06
好像你这个只有一个rowkey 我可以把  Put put=new Put(Bytes.toBytes(rowkey));放到那个for循环里面吗? ...

  Put put=mull;
  for (String str : set) {  
        put = new Put(Bytes.toBytes(rowKey));// 设置rowkey
        put.add(Bytes.toBytes("列簇"), Bytes.toBytes("word"), Bytes.toBytes(str));
}  

    table.put(put);

    table.close();

如果是一行一行的话,楼主可以尝试上面代码

推荐参考
hbase编程:Eclipse远程连接创建hbase表以及填充列与列数据
http://www.aboutyun.com/forum.php?mod=viewthread&tid=8655


回复

使用道具 举报

Tank_2000 发表于 2016-12-18 10:23:01
[mw_shl_code=java,true]        public void save(List<Put> put, String tableName) {
                HTableInterface table = null;
                try{
                        table = hTablePool.getTable(tableName);
                        table.put(put);
                }catch(Exception e){
                        e.printStackTrace();
                }finally{
                        try{
                                table.close();
                        }catch(IOException e){
                                e.printStackTrace();
                        }
                }
        }[/mw_shl_code]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条