hbase2.1.x 批量添加数据(put) 作者:马育民 • 2021-11-30 07:22 • 阅读:10202 # 说明 hbase java api,批量操作的底层,都是调用 `batch` 方法 批量put方法在 `HTable` 类中,声明如下: ``` @Override public void put(final List puts) throws IOException { for (Put put : puts) { validatePut(put); } Object[] results = new Object[puts.size()]; try { batch(puts, results, writeRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException) new InterruptedIOException().initCause(e); } } ``` **解释:** 将 多个 `Put` 对象放入 `List` 中,然后将 list 传入 `put` 方法 # 案例 批量添加数据 ### 清空表 ``` truncate 'book' ``` ### 代码 创建 `HbaseBatchUtils` 类,继承 `HbaseUtils` 类 ``` public void putBatch(String tableName, List list) throws IOException { //获取table对象 Table table = conn.getTable(TableName.valueOf(tableName)); List putList=new ArrayList(); for(RowData row : list){ //创建put对象,对应put命令,封装rowkey、列族、列名、列值 Put put = new Put(Bytes.toBytes(row.getRowkey())); for(CellData item:row.getCellDatas()) { put.addColumn(Bytes.toBytes(item.getFamily()), Bytes.toBytes(item.getQualifier()), Bytes.toBytes((String) item.getValue())); putList.add(put); } } //执行put命令 table.put(putList); //关闭table table.close(); } ``` ### 测试 ``` public static void main(String[] args) throws IOException { HbaseUtils2_putbatch utils=new HbaseUtils2_putbatch(); utils.connect(); List rowDataList = new ArrayList<>(); { // 添加一行数据 RowData rowData = new RowData("1020"); CellData cellData = new CellData("c1", "title", "scala从入门到精通"); rowData.addCellData(cellData); CellData cellData2 = new CellData("c1", "price", "121.90"); rowData.addCellData(cellData2); CellData cellData3 = new CellData("c1", "author", "韩梅梅"); rowData.addCellData(cellData3); rowDataList.add(rowData); } { // 添加一行数据 RowData rowData = new RowData("1021"); CellData cellData = new CellData("c1", "title", "三国演义"); rowData.addCellData(cellData); CellData cellData2 = new CellData("c1", "price", "21.90"); rowData.addCellData(cellData2); CellData cellData3 = new CellData("c1", "author", "罗贯中"); rowData.addCellData(cellData3); rowDataList.add(rowData); } utils.putBatch("book",rowDataList); utils.close(); } ``` 原文出处:http://malaoshi.top/show_1IX2JskWHCxh.html