public class FindByKeyOperator extends Object
Say you have a json resource file called "map.json" containing the following json:
[
{ "city" : "paris",
"user" : "bob",
"datacenter" : "dc1",
},
{ "city" : "london",
"user" : ["ted", "bob"],
"datacenter" : "dc7",
},
{ "city" : "madrid",
"user" : "sophie",
}
]
Say you want to retrieve all elements matching "city" == "paris". Write :
Tuple result;
findByKey(map, "city").on("paris").into(result);
print(result);
You'll get :
[ { "city": "paris", "user": "bob", "datacenter": "dc1"} ]
Whenever several matches are found, you can get the result as a plain array, or as a union. With union, each property is returned as a set, so that you have immediately the matches at hand without the need for additional scans. For example:
findByKey(map, "user").on("bob").into(result);
produces
[
{ "city": "paris", "user": "bob", "datacenter": "dc1" },
{ "city": "london", "user": ["ted","bob"],"datacenter": "dc7"}
]
that is, a plain array with all matched elements from your original tuple. Now if instead you do
findByKey(map, "user").union().on("bob").into(result);
you will get
{ "city": [ "paris", "london"],
"user": ["bob", ["ted", "bob"]],
"datacenter": ["dc1","dc7"]
}
This example is available here : finding.punch. To run it, use the json files from the json directory.
Constructor and Description |
---|
FindByKeyOperator(Tuple sourceMap,
String... key)
Return a new FindByKeyOperatorRefactor, ready to answer queries with one
or several lookup values.
|
Modifier and Type | Method and Description |
---|---|
FindByKeyOperator |
defaultMatch(String defaultMatch)
Set a default value if no match is found.
|
boolean |
into(Tuple into)
Fire the operator
|
FindByKeyOperator |
on(String... values)
Take the lookup keys from Strings
|
FindByKeyOperator |
on(Tuple... tuples)
Take the lookup key from an array of tuples
|
FindByKeyOperator |
union()
Make the result be computed as a union.
|
FindByKeyOperator |
where(String key,
String value)
Return an operator to find a named element which matches some value
|
FindByKeyOperator |
where(String key,
Tuple value)
Return an operator to find a named element which matches some tuple value.
|
public FindByKeyOperator(Tuple sourceMap, String... key)
sourceMap
- the input documentkey
- one or several key identifying the properties on which you
will want to query.public FindByKeyOperator union()
public FindByKeyOperator on(Tuple... tuples)
tuples
- the tuple containing the lookup search valuespublic FindByKeyOperator where(String key, String value)
key
- the target key namevalue
- the searched valuepublic FindByKeyOperator where(String key, Tuple value)
Watch out the tuple must contain a String.
key
- the target key namevalue
- the searched valuepublic FindByKeyOperator on(String... values)
values
- the keypublic FindByKeyOperator defaultMatch(String defaultMatch)
defaultMatch
- the default valuepublic boolean into(Tuple into)
into
- where to put the resultCopyright © 2023. All rights reserved.