访问ThriftServer读数据
功能简介
传入ThriftServer实例所在host和提供服务的port,新建Thrift客户端,访问ThriftServer,分别使用get和scan进行读数据操作。
代码样例
- 方法调用
// Get data with single get. getData(client, TABLE_NAME); // Get data with getlist. getDataList(client, TABLE_NAME); // Scan data. scanData(client, TABLE_NAME);
- 使用get进行读数据
以下代码片段在“hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples”包的“ThriftSample”类的getData方法中。
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."); }
- 使用getlist进行读数据
以下代码片段在“hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples”包的“ThriftSample”类的getDataList方法中。
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."); }
- 使用scan进行读数据
以下代码片段在“hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples”包的“ThriftSample”类的scanData方法中。
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."); }