hbase(main):002:0> create 'iteblog', 'f', SPLITS => ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
package com.iteblog.data;
hbase(main):001:0> scan 'iteblog', {'LIMIT'=>10}
ROW COLUMN+CELL
A-1000-1550572395399 column=f:age, timestamp=1549091990253, value=54
A-1000-1550572395399 column=f:uuid, timestamp=1549091990253, value=e9b10a9f-1218-43fd-bd01
A-1000-1550572413799 column=f:age, timestamp=1549092008575, value=4
A-1000-1550572413799 column=f:uuid, timestamp=1549092008575, value=181aa91e-5f1d-454c-959c
A-1000-1550572414761 column=f:age, timestamp=1549092009531, value=33
A-1000-1550572414761 column=f:uuid, timestamp=1549092009531, value=19aad8d3-621a-473c-8f9f
A-1001-1550572394570 column=f:age, timestamp=1549091989341, value=64
A-1001-1550572394570 column=f:uuid, timestamp=1549091989341, value=c6712a0d-3793-46d5-865b
A-1001-1550572405337 column=f:age, timestamp=1549092000108, value=96
A-1001-1550572405337 column=f:uuid, timestamp=1549092000108, value=4bf05d10-bb4d-43e3-9957
A-1001-1550572419688 column=f:age, timestamp=1549092014458, value=8
A-1001-1550572419688 column=f:uuid, timestamp=1549092014458, value=f04ba835-d8ac-49a3-8f96
A-1002-1550572424041 column=f:age, timestamp=1549092018816, value=84
A-1002-1550572424041 column=f:uuid, timestamp=1549092018816, value=99d6c989-afb5-4101-9d95
A-1003-1550572431830 column=f:age, timestamp=1549092026605, value=21
A-1003-1550572431830 column=f:uuid, timestamp=1549092026605, value=8c1ff1b6-b97c-4059-9b68
A-1004-1550572395399 column=f:age, timestamp=1549091990253, value=2
A-1004-1550572395399 column=f:uuid, timestamp=1549091990253, value=e240aa0f-c044-452f-89c0
A-1004-1550572403783 column=f:age, timestamp=1549091998555, value=6
A-1004-1550572403783 column=f:uuid, timestamp=1549091998555, value=e8df15c9-02fa-458e-bd0c
10 row(s)
Took 0.1104 seconds
option java_package = "com.iteblog.data.coprocessor.generated";
option java_outer_classname = "DataQueryProtos";
option java_generic_services = true;
option java_generate_equals_and_hash = true;
option optimize_for = SPEED;
message DataQueryRequest {
optional string tableName = 1;
optional string startRow = 2;
optional string endRow = 3;
optional bool incluedEnd = 4;
optional bool isSalting = 5;
}
message DataQueryResponse {
message Cell{
required bytes value = 1;
required bytes family = 2;
required bytes qualifier = 3;
required bytes row = 4;
required int64 timestamp = 5;
}
message Row{
optional bytes rowKey = 1;
repeated Cell cellList = 2;
}
repeated Row rowList = 1;
}
service QueryDataService{
rpc queryByStartRowAndEndRow(DataQueryRequest)
returns (DataQueryResponse);
}
package com.iteblog.data.coprocessor;
import com.google.protobuf.ByteString;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcController;
import com.google.protobuf.Service;
import com.iteblog.data.coprocessor.generated.DataQueryProtos.QueryDataService;
import com.iteblog.data.coprocessor.generated.DataQueryProtos.DataQueryRequest;
import com.iteblog.data.coprocessor.generated.DataQueryProtos.DataQueryResponse;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CoprocessorEnvironment;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.InternalScanner;
import org.apache.hadoop.hbase.shaded.protobuf.ResponseConverter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SlatTableDataSearch extends QueryDataService implements RegionCoprocessor {
private RegionCoprocessorEnvironment env;
public Iterable<Service> getServices() {
return Collections.singleton(this);
}
@Override
public void queryByStartRowAndEndRow(RpcController controller,
DataQueryRequest request,
RpcCallback<DataQueryResponse> done) {
DataQueryResponse response = null;
String startRow = request.getStartRow();
String endRow = request.getEndRow();
String regionStartKey = Bytes.toString(this.env.getRegion().getRegionInfo().getStartKey());
if (request.getIsSalting()) {
String startSalt = null;
if (null != regionStartKey && regionStartKey.length() != 0) {
startSalt = regionStartKey;
}
if (null != startSalt && null != startRow) {
startRow = startSalt + "-" + startRow;
endRow = startSalt + "-" + endRow;
}
}
Scan scan = new Scan();
if (null != startRow) {
scan.withStartRow(Bytes.toBytes(startRow));
}
if (null != endRow) {
scan.withStopRow(Bytes.toBytes(endRow), request.getIncluedEnd());
}
try (InternalScanner scanner = this.env.getRegion().getScanner(scan)) {
List<Cell> results = new ArrayList<>();
boolean hasMore;
DataQueryResponse.Builder responseBuilder = DataQueryResponse.newBuilder();
do {
hasMore = scanner.next(results);
DataQueryResponse.Row.Builder rowBuilder = DataQueryResponse.Row.newBuilder();
if (results.size() > 0) {
Cell cell = results.get(0);
rowBuilder.setRowKey(ByteString.copyFrom(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
for (Cell kv : results) {
buildCell(rowBuilder, kv);
}
}
responseBuilder.addRowList(rowBuilder);
results.clear();
} while (hasMore);
response = responseBuilder.build();
} catch (IOException e) {
ResponseConverter.setControllerException(controller, e);
}
done.run(response);
}
private void buildCell(DataQueryResponse.Row.Builder rowBuilder, Cell kv) {
DataQueryResponse.Cell.Builder cellBuilder = DataQueryResponse.Cell.newBuilder();
cellBuilder.setFamily(ByteString.copyFrom(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength()));
cellBuilder.setQualifier(ByteString.copyFrom(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength()));
cellBuilder.setRow(ByteString.copyFrom(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength()));
cellBuilder.setValue(ByteString.copyFrom(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength()));
cellBuilder.setTimestamp(kv.getTimestamp());
rowBuilder.addCellList(cellBuilder);
}
/**
* Stores a reference to the coprocessor environment provided by the
* {@link org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost} from the region where this
* coprocessor is loaded. Since this is a coprocessor endpoint, it always expects to be loaded
* on a table region, so always expects this to be an instance of
* {@link RegionCoprocessorEnvironment}.
*
* @param env the environment provided by the coprocessor host
* @throws IOException if the provided environment is not an instance of
* {@code RegionCoprocessorEnvironment}
*/
@Override
public void start(CoprocessorEnvironment env) throws IOException {
if (env instanceof RegionCoprocessorEnvironment) {
this.env = (RegionCoprocessorEnvironment) env;
} else {
throw new CoprocessorException("Must be loaded on a table region!");
}
}
@Override
public void stop(CoprocessorEnvironment env) {
// nothing to do
}
}
package com.iteblog.data;
import com.iteblog.data.coprocessor.generated.DataQueryProtos.QueryDataService;
import com.iteblog.data.coprocessor.generated.DataQueryProtos.DataQueryRequest;
import com.iteblog.data.coprocessor.generated.DataQueryProtos.DataQueryResponse;
import com.iteblog.data.coprocessor.generated.DataQueryProtos.DataQueryResponse.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils.BlockingRpcCallback;
import org.apache.hadoop.hbase.ipc.ServerRpcController;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class DataQuery {
private static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "https://www.iteblog.com:2181");
}
static List<Row> queryByStartRowAndStopRow(String tableName,
String startRow, String stopRow,
boolean isIncludeEnd, boolean isSalting) {
final DataQueryRequest.Builder requestBuilder = DataQueryRequest.newBuilder();
requestBuilder.setTableName(tableName);
requestBuilder.setStartRow(startRow);
requestBuilder.setEndRow(stopRow);
requestBuilder.setIncluedEnd(isIncludeEnd);
requestBuilder.setIsSalting(isSalting);
try {
Connection connection = ConnectionFactory.createConnection(conf);
HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));
Map<byte[], List<Row>> result = table.coprocessorService(QueryDataService.class,
null, null, counter -> {
ServerRpcController controller = new ServerRpcController();
BlockingRpcCallback<DataQueryResponse> call = new BlockingRpcCallback<>();
counter.queryByStartRowAndEndRow(controller, requestBuilder.build(), call);
DataQueryResponse response = call.get();
if (controller.failedOnException()) {
throw controller.getFailedOn();
}
return response.getRowListList();
});
List<Row> list = new LinkedList<>();
for (Map.Entry<byte[], List<Row>> entry : result.entrySet()) {
if (null != entry.getKey()) {
list.addAll(entry.getValue());
}
}
return list;
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
List<Row> rows = queryByStartRowAndStopRow("iteblog", "1000", "1001", false, true);
if (null != rows) {
System.out.println(rows.size());
for (DataQueryResponse.Row row : rows) {
List<DataQueryResponse.Cell> cellListList = row.getCellListList();
for (DataQueryResponse.Cell cell : cellListList) {
System.out.println(row.getRowKey().toStringUtf8() + " \t " +
"column=" + cell.getFamily().toStringUtf8() +
":" + cell.getQualifier().toStringUtf8() + ", " +
"timestamp=" + cell.getTimestamp() + ", " +
"value=" + cell.getValue().toStringUtf8());
}
}
}
}
}
A-1000-1550572395399 column=f:age, timestamp=1549091990253, value=54
A-1000-1550572395399 column=f:uuid, timestamp=1549091990253, value=e9b10a9f-1218-43fd-bd01
A-1000-1550572413799 column=f:age, timestamp=1549092008575, value=4
A-1000-1550572413799 column=f:uuid, timestamp=1549092008575, value=181aa91e-5f1d-454c-959c
A-1000-1550572414761 column=f:age, timestamp=1549092009531, value=33
A-1000-1550572414761 column=f:uuid, timestamp=1549092009531, value=19aad8d3-621a-473c-8f9f
B-1000-1550572388491 column=f:age, timestamp=1549091983276, value=1
B-1000-1550572388491 column=f:uuid, timestamp=1549091983276, value=cf720efe-2ad2-48d6-81b8
B-1000-1550572392922 column=f:age, timestamp=1549091987701, value=7
B-1000-1550572392922 column=f:uuid, timestamp=1549091987701, value=8a047118-e130-48cb-adfe
hbase(main):020:0> scan 'iteblog', {STARTROW => 'A-1000', ENDROW => 'A-1001'}
ROW COLUMN+CELL
A-1000-1550572395399 column=f:age, timestamp=1549091990253, value=54
A-1000-1550572395399 column=f:uuid, timestamp=1549091990253, value=e9b10a9f-1218-43fd-bd01
A-1000-1550572413799 column=f:age, timestamp=1549092008575, value=4
A-1000-1550572413799 column=f:uuid, timestamp=1549092008575, value=181aa91e-5f1d-454c-959c
A-1000-1550572414761 column=f:age, timestamp=1549092009531, value=33
A-1000-1550572414761 column=f:uuid, timestamp=1549092009531, value=19aad8d3-621a-473c-8f9f
3 row(s)
Took 0.0569 seconds
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |