Skip to content

Commit cf45f8e

Browse files
chandrasekhar-188kApache9
authored andcommitted
HBASE-28124 Missing fields in Scan.toJSON (#5678)
Signed-off-by: Pankaj Kumar < pankajkumar@apache.org> Signed-off-by: Rajeshbabu Chintaguntla <rajeshbabu@apache.org> Signed-off-by: Duo Zhang <zhangduo@apache.org> (cherry picked from commit 0763a74)
1 parent 75d7ae9 commit cf45f8e

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/Scan.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.NavigableSet;
2626
import java.util.TreeMap;
2727
import java.util.TreeSet;
28+
import java.util.stream.Collectors;
2829
import org.apache.hadoop.hbase.HConstants;
2930
import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
3031
import org.apache.hadoop.hbase.filter.Filter;
@@ -863,7 +864,7 @@ public Map<String, Object> getFingerprint() {
863864
*/
864865
@Override
865866
public Map<String, Object> toMap(int maxCols) {
866-
// start with the fingerpring map and build on top of it
867+
// start with the fingerprint map and build on top of it
867868
Map<String, Object> map = getFingerprint();
868869
// map from families to column list replaces fingerprint's list of families
869870
Map<String, List<String>> familyColumns = new HashMap<>();
@@ -911,6 +912,34 @@ public Map<String, Object> toMap(int maxCols) {
911912
if (getId() != null) {
912913
map.put("id", getId());
913914
}
915+
map.put("includeStartRow", includeStartRow);
916+
map.put("includeStopRow", includeStopRow);
917+
map.put("allowPartialResults", allowPartialResults);
918+
map.put("storeLimit", storeLimit);
919+
map.put("storeOffset", storeOffset);
920+
map.put("reversed", reversed);
921+
if (null != asyncPrefetch) {
922+
map.put("asyncPrefetch", asyncPrefetch);
923+
}
924+
map.put("mvccReadPoint", mvccReadPoint);
925+
map.put("limit", limit);
926+
map.put("readType", readType);
927+
map.put("needCursorResult", needCursorResult);
928+
map.put("targetReplicaId", targetReplicaId);
929+
map.put("consistency", consistency);
930+
if (!colFamTimeRangeMap.isEmpty()) {
931+
Map<String, List<Long>> colFamTimeRangeMapStr = colFamTimeRangeMap.entrySet().stream()
932+
.collect(Collectors.toMap((e) -> Bytes.toStringBinary(e.getKey()), e -> {
933+
TimeRange value = e.getValue();
934+
List<Long> rangeList = new ArrayList<>();
935+
rangeList.add(value.getMin());
936+
rangeList.add(value.getMax());
937+
return rangeList;
938+
}));
939+
940+
map.put("colFamTimeRangeMap", colFamTimeRangeMapStr);
941+
}
942+
map.put("priority", getPriority());
914943
return map;
915944
}
916945

hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestOperation.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969

7070
import org.apache.hbase.thirdparty.com.google.common.reflect.TypeToken;
7171
import org.apache.hbase.thirdparty.com.google.gson.Gson;
72+
import org.apache.hbase.thirdparty.com.google.gson.GsonBuilder;
73+
import org.apache.hbase.thirdparty.com.google.gson.LongSerializationPolicy;
74+
import org.apache.hbase.thirdparty.com.google.gson.ToNumberPolicy;
7275

7376
/**
7477
* Run tests that use the functionality of the Operation superclass for Puts, Gets, Deletes, Scans,
@@ -345,6 +348,101 @@ public void testOperationJSON() throws IOException {
345348
kvMap.get("qualifier"));
346349
}
347350

351+
/**
352+
* Test the client Scan Operations' JSON encoding to ensure that produced JSON is parseable and
353+
* that the details are present and not corrupted.
354+
* @throws IOException if the JSON conversion fails
355+
*/
356+
@Test
357+
public void testScanOperationToJSON() throws IOException {
358+
// produce a Scan Operation
359+
Scan scan = new Scan().withStartRow(ROW, true);
360+
scan.addColumn(FAMILY, QUALIFIER);
361+
scan.withStopRow(ROW, true);
362+
scan.readVersions(5);
363+
scan.setBatch(10);
364+
scan.setAllowPartialResults(true);
365+
scan.setMaxResultsPerColumnFamily(3);
366+
scan.setRowOffsetPerColumnFamily(8);
367+
scan.setCaching(20);
368+
scan.setMaxResultSize(50);
369+
scan.setCacheBlocks(true);
370+
scan.setReversed(true);
371+
scan.setTimeRange(1000, 2000);
372+
scan.setAsyncPrefetch(true);
373+
scan.setMvccReadPoint(123);
374+
scan.setLimit(5);
375+
scan.setReadType(Scan.ReadType.PREAD);
376+
scan.setNeedCursorResult(true);
377+
scan.setFilter(SCV_FILTER);
378+
scan.setReplicaId(1);
379+
scan.setConsistency(Consistency.STRONG);
380+
scan.setLoadColumnFamiliesOnDemand(true);
381+
scan.setColumnFamilyTimeRange(FAMILY, 2000, 3000);
382+
scan.setPriority(10);
383+
384+
// get its JSON representation, and parse it
385+
String json = scan.toJSON();
386+
Type typeOfHashMap = new TypeToken<Map<String, Object>>() {
387+
}.getType();
388+
Gson gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING)
389+
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
390+
Map<String, Object> parsedJSON = gson.fromJson(json, typeOfHashMap);
391+
// check for the row
392+
assertEquals("startRow incorrect in Scan.toJSON()", Bytes.toStringBinary(ROW),
393+
parsedJSON.get("startRow"));
394+
// check for the family and the qualifier.
395+
List familyInfo = (List) ((Map) parsedJSON.get("families")).get(Bytes.toStringBinary(FAMILY));
396+
assertNotNull("Family absent in Scan.toJSON()", familyInfo);
397+
assertEquals("Qualifier absent in Scan.toJSON()", 1, familyInfo.size());
398+
assertEquals("Qualifier incorrect in Scan.toJSON()", Bytes.toStringBinary(QUALIFIER),
399+
familyInfo.get(0));
400+
assertEquals("stopRow incorrect in Scan.toJSON()", Bytes.toStringBinary(ROW),
401+
parsedJSON.get("stopRow"));
402+
assertEquals("includeStartRow incorrect in Scan.toJSON()", true,
403+
parsedJSON.get("includeStartRow"));
404+
assertEquals("includeStopRow incorrect in Scan.toJSON()", true,
405+
parsedJSON.get("includeStopRow"));
406+
assertEquals("maxVersions incorrect in Scan.toJSON()", 5L, parsedJSON.get("maxVersions"));
407+
assertEquals("batch incorrect in Scan.toJSON()", 10L, parsedJSON.get("batch"));
408+
assertEquals("allowPartialResults incorrect in Scan.toJSON()", true,
409+
parsedJSON.get("allowPartialResults"));
410+
assertEquals("storeLimit incorrect in Scan.toJSON()", 3L, parsedJSON.get("storeLimit"));
411+
assertEquals("storeOffset incorrect in Scan.toJSON()", 8L, parsedJSON.get("storeOffset"));
412+
assertEquals("caching incorrect in Scan.toJSON()", 20L, parsedJSON.get("caching"));
413+
assertEquals("maxResultSize incorrect in Scan.toJSON()", "50", parsedJSON.get("maxResultSize"));
414+
assertEquals("cacheBlocks incorrect in Scan.toJSON()", true, parsedJSON.get("cacheBlocks"));
415+
assertEquals("reversed incorrect in Scan.toJSON()", true, parsedJSON.get("reversed"));
416+
List trList = (List) parsedJSON.get("timeRange");
417+
assertEquals("timeRange incorrect in Scan.toJSON()", 2, trList.size());
418+
assertEquals("timeRange incorrect in Scan.toJSON()", "1000", trList.get(0));
419+
assertEquals("timeRange incorrect in Scan.toJSON()", "2000", trList.get(1));
420+
421+
assertEquals("asyncPrefetch incorrect in Scan.toJSON()", true, parsedJSON.get("asyncPrefetch"));
422+
assertEquals("mvccReadPoint incorrect in Scan.toJSON()", "123",
423+
parsedJSON.get("mvccReadPoint"));
424+
assertEquals("limit incorrect in Scan.toJSON()", 5L, parsedJSON.get("limit"));
425+
assertEquals("readType incorrect in Scan.toJSON()", "PREAD", parsedJSON.get("readType"));
426+
assertEquals("needCursorResult incorrect in Scan.toJSON()", true,
427+
parsedJSON.get("needCursorResult"));
428+
429+
Map colFamTimeRange = (Map) parsedJSON.get("colFamTimeRangeMap");
430+
assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", 1L, colFamTimeRange.size());
431+
List testFamily = (List) colFamTimeRange.get("testFamily");
432+
assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", 2L, testFamily.size());
433+
assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", "2000", testFamily.get(0));
434+
assertEquals("colFamTimeRangeMap incorrect in Scan.toJSON()", "3000", testFamily.get(1));
435+
436+
assertEquals("targetReplicaId incorrect in Scan.toJSON()", 1L,
437+
parsedJSON.get("targetReplicaId"));
438+
assertEquals("consistency incorrect in Scan.toJSON()", "STRONG", parsedJSON.get("consistency"));
439+
assertEquals("loadColumnFamiliesOnDemand incorrect in Scan.toJSON()", true,
440+
parsedJSON.get("loadColumnFamiliesOnDemand"));
441+
442+
assertEquals("priority incorrect in Scan.toJSON()", 10L, parsedJSON.get("priority"));
443+
444+
}
445+
348446
@Test
349447
public void testPutCreationWithByteBuffer() {
350448
Put p = new Put(ROW);

0 commit comments

Comments
 (0)