Skip to main content
Loading

Nested collection data type expressions

You can nest collection data type (CDT) expressions to access data elements within maps and lists.

The following Java code examples demonstrate queries that check to see if a particular map key matches a string specified by a regular expression. These examples use a record that contains a bin named bin4. The bin bin4 contains a list of four items, the last of which is a map. The map contains three key-value pairs, the last of which is another list mapped to the value "list1".

["string1", 8, ["foo", 87], {"a":1, 2:"string3", "list1":["string2", 5]}]

We can visualize the above record another way:

List
โ”œโ”€โ”€ "string1"
โ”œโ”€โ”€ 8
โ”œโ”€โ”€ List
โ”‚ โ”œโ”€โ”€ "foo"
โ”‚ โ””โ”€โ”€ 87
โ””โ”€โ”€ Map
โ”œโ”€โ”€ "a" -> 1
โ”œโ”€โ”€ 2 -> "string3"
โ””โ”€โ”€ "list1" -> List
โ”œโ”€โ”€ "string2"
โ””โ”€โ”€ 5

The following example compares the second map key of the fourth list element to the regex str.*3 by nesting the getByIndex and getByKey methods inside a regexCompare method. It returns a match, because "string3" matches the regex str.*3.

QueryPolicy policy = client.copyQueryPolicyDefault();
policy.filterExp = Exp.build(
// Query Predicate: bin4, map key 2 at list index 3 contains regex str.*3
Exp.regexCompare("str.*3", RegexFlag.ICASE | RegexFlag.NEWLINE,
MapExp.getByKey(MapReturnType.VALUE, Exp.Type.STRING, Exp.val(2),
ListExp.getByIndex(ListReturnType.VALUE, Exp.Type.MAP, Exp.val(3), Exp.listBin("bin4")))));

The following example uses a similar technique to examine the first element of the list inside the map. It checks to see if the map element with key "list1" contains a string matching the regex str.*2.

QueryPolicy policy = client.copyQueryPolicyDefault();
policy.filterExp = Exp.build(
// Query Predicate: bin4, inner list index 0, from map key 'list1' at outer list index 3 contains regex str.*2
Exp.regexCompare("str.*2", RegexFlag.ICASE | RegexFlag.NEWLINE,
ListExp.getByIndex(MapReturnType.VALUE, Exp.Type.STRING, Exp.val(0),
MapExp.getByKey(MapReturnType.VALUE, Exp.Type.LIST, Exp.val("list1"),
ListExp.getByIndex(ListReturnType.VALUE, Exp.Type.MAP, Exp.val(3), Exp.listBin("bin4"))))));