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:
- Tree
- Expanded
List
โโโ "string1"
โโโ 8
โโโ List
โ โโโ "foo"
โ โโโ 87
โโโ Map
โโโ "a" -> 1
โโโ 2 -> "string3"
โโโ "list1" -> List
โโโ "string2"
โโโ 5
[
"string1",
8,
["foo",87],
{
"a":1,
2:"string3",
"list1":
["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
.
- Command
- Expanded
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")))));
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
.
- Command
- Expanded
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"))))));
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")
)
)
)
)
);