You can use request a timeline-consistent read in your application.
To request a timeline-consistent read in your application, use the
get.setConsistency(Consistency.TIMELINE)
method before
performing the Get
or Scan
operation.
To check whether the result is stale (comes from a secondary replica), use the
isStale()
method of the result object. Use the following
examples for reference.
-
Get request
Get get = new Get(key);
get.setConsistency(Consistency.TIMELINE);
Result result = table.get(get);
-
Scan request
Scan scan = new Scan();
scan.setConsistency(CONSISTENCY.TIMELINE);
ResultScanner scanner = table.getScanner(scan);
Result result = scanner.next();
-
Scan request to a specific replica.
This example overrides the normal behavior of sending the read request to all
known replicas, and only sends it to the replica specified by ID.
Scan scan = new Scan();
scan.setConsistency(CONSISTENCY.TIMELINE);
scan.setReplicaId(2);
ResultScanner scanner = table.getScanner(scan);
Result result = scanner.next();
-
Detect a stale result
You can also request timeline consistency using HBase Shell, allowing the
result to come from a secondary replica.
hbase> get 'myTable', 'myRow', {CONSISTENCY => "TIMELINE"}
hbase> scan 'myTable', {CONSISTENCY => 'TIMELINE'}