Updated on 2023-08-31 GMT+08:00

Accessing ThriftServer to Write Data

Function

After importing the host where the ThriftServer instances are located and the port that provides services, you can create a Thrift client using the authentication credential and configuration file, access the ThriftServer, and use Put and putMultiple to write data.

Example Code

  • Invoking methods
    // Write data with put.
    putData(client, TABLE_NAME);
    
    // Write data with putlist.
     putDataList(client, TABLE_NAME);
  • Using Put to write data.

    The following code snippets are in the putData method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    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.");
     }
  • Using putMultiple to write data.

    The following code snippets are in the putDataList method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    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.");
     }