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 get and scan methods to write data.
Example Code
- Invoking methods
// Get data with single get. getData(client, TABLE_NAME); // Get data with getlist. getDataList(client, TABLE_NAME); // Scan data. scanData(client, TABLE_NAME);
- Using the get method to write data.
The following code snippets are in the getData method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.
private void getData(THBaseService.Iface client, String tableName) throws TException { LOGGER.info("Test getData."); TGet get = new TGet(); get.setRow("row1".getBytes()); ByteBuffer table = ByteBuffer.wrap(tableName.getBytes()); TResult result = client.get(table, get); printResult(result); LOGGER.info("Test getData done."); }
- Using the getlist method to write data.
The following code snippets are in the getDataList method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.
private void getDataList(THBaseService.Iface client, String tableName) throws TException { LOGGER.info("Test getDataList."); List<TGet> getList = new ArrayList<>(); TGet get1 = new TGet(); get1.setRow("row1".getBytes()); getList.add(get1); TGet get2 = new TGet(); get2.setRow("row2".getBytes()); getList.add(get2); ByteBuffer table = ByteBuffer.wrap(tableName.getBytes()); List<TResult> resultList = client.getMultiple(table, getList); for (TResult tResult : resultList) { printResult(tResult); } LOGGER.info("Test getDataList done."); }
- Using the scan method to write data.
The following code snippets are in the scanData method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.
private void scanData(THBaseService.Iface client, String tableName) throws TException { LOGGER.info("Test scanData."); int scannerId = -1; try { ByteBuffer table = ByteBuffer.wrap(tableName.getBytes()); TScan scan = new TScan(); scan.setLimit(500); scannerId = client.openScanner(table, scan); List<TResult> resultList = client.getScannerRows(scannerId, 100); while (resultList != null && !resultList.isEmpty()) { for (TResult tResult : resultList) { printResult(tResult); } resultList = client.getScannerRows(scannerId, 100); } } finally { if (scannerId != -1) { client.closeScanner(scannerId); LOGGER.info("Closed scanner {}.", scannerId); } } LOGGER.info("Test scanData done."); }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.