Skip to content

Commit c73f8b5

Browse files
HBASE-28497 Missing fields in Get.toJSON (#5800)
Signed-off-by: Duo Zhang <[email protected]> Signed-off-by: Pankaj Kumar <[email protected]>
1 parent 8b5ccda commit c73f8b5

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Set;
2828
import java.util.TreeMap;
2929
import java.util.TreeSet;
30+
import java.util.stream.Collectors;
3031
import org.apache.hadoop.hbase.HConstants;
3132
import org.apache.hadoop.hbase.filter.Filter;
3233
import org.apache.hadoop.hbase.io.TimeRange;
@@ -430,6 +431,26 @@ public Map<String, Object> toMap(int maxCols) {
430431
if (getId() != null) {
431432
map.put("id", getId());
432433
}
434+
map.put("storeLimit", this.storeLimit);
435+
map.put("storeOffset", this.storeOffset);
436+
map.put("checkExistenceOnly", this.checkExistenceOnly);
437+
438+
map.put("targetReplicaId", this.targetReplicaId);
439+
map.put("consistency", this.consistency);
440+
map.put("loadColumnFamiliesOnDemand", this.loadColumnFamiliesOnDemand);
441+
if (!colFamTimeRangeMap.isEmpty()) {
442+
Map<String, List<Long>> colFamTimeRangeMapStr = colFamTimeRangeMap.entrySet().stream()
443+
.collect(Collectors.toMap((e) -> Bytes.toStringBinary(e.getKey()), e -> {
444+
TimeRange value = e.getValue();
445+
List<Long> rangeList = new ArrayList<>();
446+
rangeList.add(value.getMin());
447+
rangeList.add(value.getMax());
448+
return rangeList;
449+
}));
450+
451+
map.put("colFamTimeRangeMap", colFamTimeRangeMapStr);
452+
}
453+
map.put("priority", getPriority());
433454
return map;
434455
}
435456

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,76 @@ public void testOperationSubClassMethodsAreBuilderStyle() {
504504

505505
BuilderStyleTest.assertClassesAreBuilderStyle(classes);
506506
}
507+
508+
/**
509+
* Test the client Get Operations' JSON encoding to ensure that produced JSON is parseable and
510+
* that the details are present and not corrupted.
511+
* @throws IOException if the JSON conversion fails
512+
*/
513+
@Test
514+
public void testGetOperationToJSON() throws IOException {
515+
// produce a Scan Operation
516+
Get get = new Get(ROW);
517+
get.addColumn(FAMILY, QUALIFIER);
518+
get.readVersions(5);
519+
get.setMaxResultsPerColumnFamily(3);
520+
get.setRowOffsetPerColumnFamily(8);
521+
get.setCacheBlocks(true);
522+
get.setMaxResultsPerColumnFamily(5);
523+
get.setRowOffsetPerColumnFamily(9);
524+
get.setCheckExistenceOnly(true);
525+
get.setTimeRange(1000, 2000);
526+
get.setFilter(SCV_FILTER);
527+
get.setReplicaId(1);
528+
get.setConsistency(Consistency.STRONG);
529+
get.setLoadColumnFamiliesOnDemand(true);
530+
get.setColumnFamilyTimeRange(FAMILY, 2000, 3000);
531+
get.setPriority(10);
532+
533+
// get its JSON representation, and parse it
534+
String json = get.toJSON();
535+
Type typeOfHashMap = new TypeToken<Map<String, Object>>() {
536+
}.getType();
537+
Gson gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING)
538+
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
539+
Map<String, Object> parsedJSON = gson.fromJson(json, typeOfHashMap);
540+
// check for the row
541+
assertEquals("row incorrect in Get.toJSON()", Bytes.toStringBinary(ROW), parsedJSON.get("row"));
542+
// check for the family and the qualifier.
543+
List familyInfo = (List) ((Map) parsedJSON.get("families")).get(Bytes.toStringBinary(FAMILY));
544+
assertNotNull("Family absent in Get.toJSON()", familyInfo);
545+
assertEquals("Qualifier absent in Get.toJSON()", 1, familyInfo.size());
546+
assertEquals("Qualifier incorrect in Get.toJSON()", Bytes.toStringBinary(QUALIFIER),
547+
familyInfo.get(0));
548+
549+
assertEquals("maxVersions incorrect in Get.toJSON()", 5L, parsedJSON.get("maxVersions"));
550+
551+
assertEquals("storeLimit incorrect in Get.toJSON()", 5L, parsedJSON.get("storeLimit"));
552+
assertEquals("storeOffset incorrect in Get.toJSON()", 9L, parsedJSON.get("storeOffset"));
553+
554+
assertEquals("cacheBlocks incorrect in Get.toJSON()", true, parsedJSON.get("cacheBlocks"));
555+
556+
List trList = (List) parsedJSON.get("timeRange");
557+
assertEquals("timeRange incorrect in Get.toJSON()", 2, trList.size());
558+
assertEquals("timeRange incorrect in Get.toJSON()", "1000", trList.get(0));
559+
assertEquals("timeRange incorrect in Get.toJSON()", "2000", trList.get(1));
560+
561+
Map colFamTimeRange = (Map) parsedJSON.get("colFamTimeRangeMap");
562+
assertEquals("colFamTimeRangeMap incorrect in Get.toJSON()", 1L, colFamTimeRange.size());
563+
List testFamily = (List) colFamTimeRange.get("testFamily");
564+
assertEquals("colFamTimeRangeMap incorrect in Get.toJSON()", 2L, testFamily.size());
565+
assertEquals("colFamTimeRangeMap incorrect in Get.toJSON()", "2000", testFamily.get(0));
566+
assertEquals("colFamTimeRangeMap incorrect in Get.toJSON()", "3000", testFamily.get(1));
567+
568+
assertEquals("targetReplicaId incorrect in Get.toJSON()", 1L,
569+
parsedJSON.get("targetReplicaId"));
570+
assertEquals("consistency incorrect in Get.toJSON()", "STRONG", parsedJSON.get("consistency"));
571+
assertEquals("loadColumnFamiliesOnDemand incorrect in Get.toJSON()", true,
572+
parsedJSON.get("loadColumnFamiliesOnDemand"));
573+
574+
assertEquals("priority incorrect in Get.toJSON()", 10L, parsedJSON.get("priority"));
575+
assertEquals("checkExistenceOnly incorrect in Get.toJSON()", true,
576+
parsedJSON.get("checkExistenceOnly"));
577+
578+
}
507579
}

0 commit comments

Comments
 (0)