Accessing Thrift Server to Write Data
Function Description
Input the host where the ThriftServer instance is located and the port that provides services, create a Thrift client, access the Thrift Server instance, and use the put and putMultiple commands 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."); }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot