更新时间:2023-11-10 GMT+08:00
分享

访问ThriftServer写数据

功能简介

传入ThriftServer实例所在host和提供服务的port,根据认证凭据及配置文件新建Thrift客户端,访问ThriftServer,分别使用put和putMultiple进行写数据操作。

代码样例

  • 方法调用
    // Write data with put.
    putData(client, TABLE_NAME);
    
    // Write data with putlist.
     putDataList(client, TABLE_NAME);
  • 使用put进行写数据

    以下代码片段在“hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples”包的“ThriftSample”类的putData方法中。

    private void putData(THBaseService.Iface client, String tableName) throws TException {
         LOGGER.info("Test putData.");
         TPut put = new TPut();
         put.setRow("row1".getBytes());
    
         TColumnValue columnValue = new TColumnValue();
         columnValue.setFamily(COLUMN_FAMILY.getBytes());
         columnValue.setQualifier("q1".getBytes());
         columnValue.setValue("test value".getBytes());
         List<TColumnValue> columnValues = new ArrayList<>(1);
         columnValues.add(columnValue);
         put.setColumnValues(columnValues);
    
         ByteBuffer table = ByteBuffer.wrap(tableName.getBytes());
         client.put(table, put);
         LOGGER.info("Test putData done.");
     }
  • 使用putMultiple进行写数据

    以下代码片段在“hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples”包的“ThriftSample”类的putDataList方法中。

    private void putDataList(THBaseService.Iface client, String tableName) throws TException {
         LOGGER.info("Test putDataList.");
         TPut put1 = new TPut();
         put1.setRow("row2".getBytes());
         List<TPut> putList = new ArrayList<>();
    
         TColumnValue q1Value = new TColumnValue(ByteBuffer.wrap(COLUMN_FAMILY.getBytes()),
             ByteBuffer.wrap("q1".getBytes()), ByteBuffer.wrap("test value".getBytes()));
         TColumnValue q2Value = new TColumnValue(ByteBuffer.wrap(COLUMN_FAMILY.getBytes()),
             ByteBuffer.wrap("q2".getBytes()), ByteBuffer.wrap("test q2 value".getBytes()));
         List<TColumnValue> columnValues = new ArrayList<>(2);
         columnValues.add(q1Value);
         columnValues.add(q2Value);
         put1.setColumnValues(columnValues);
         putList.add(put1);
    
         TPut put2 = new TPut();
         put2.setRow("row3".getBytes());
    
         TColumnValue columnValue = new TColumnValue();
         columnValue.setFamily(COLUMN_FAMILY.getBytes());
         columnValue.setQualifier("q1".getBytes());
         columnValue.setValue("test q1 value".getBytes());
         put2.setColumnValues(Collections.singletonList(columnValue));
         putList.add(put2);
    
         ByteBuffer table = ByteBuffer.wrap(tableName.getBytes());
         client.putMultiple(table, putList);
         LOGGER.info("Test putDataList done.");
     }
分享:

    相关文档

    相关产品