Hbase批量入库

获取Hbase HBaseConfiguration对象方法

// 获取配置
    public static HBaseConfiguration getConfiguration() {
        Configuration HBASE_CONFIG = new Configuration();
        if (conf == null) {
            HBASE_CONFIG.setInt("hbase.rpc.timeout", 20000);
            HBASE_CONFIG.setInt("hbase.client.operation.timeout", 30000);
            HBASE_CONFIG.setInt("hbase.client.scanner.timeout.period", 20000);
            HBASE_CONFIG.set("hbase.zookeeper.quorum","10.2.4.39,10.2.4.40,10.2.4.41");
            HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
            conf = new HBaseConfiguration(HBASE_CONFIG); // 读取配置文件
        }
        return conf;
    }

构建Hbase Connection

      Connection connection = ConnectionFactory.createConnection(getConfiguration());

批量入库方法

  public static void batchInsertTest_two(String value) throws IOException {
        
        Connection connection = ConnectionFactory.createConnection(getConfiguration());
        try {
            // 连接表 获取表对象
            BufferedMutator table = connection.getBufferedMutator(TableName.valueOf("t_event"));
            try {
                int count =1;
                List<Mutation> mutations = new ArrayList<Mutation>();
                for (int i = 0; i < 1; ++i) {
                    Random ran=new Random();
                    int j=ran.nextInt(100000);
                    String string = "000";
                    String temp = String.valueOf(j);
                    Put p = new Put(temp.getBytes());
                    p.add("bas".getBytes(), "id".getBytes(), value.getBytes());
                    mutations.add(p);
                    if (mutations.size() % 10000 == 0) {
                        table.mutate(mutations);
                        mutations.clear();
                    }
                }
                // 如果不flush 在后面get可能是看不见的
                table.flush();
            } finally {
                if (table != null)
                    table.close();
            }
        } finally {
            connection.close();
        }
        
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值