public class FetchJsonOperator extends FetchOperator<FetchJsonOperator>
Tuple resource = fetchJson("https://w3c.github.io/csvw/tests/test001.json").asTuple();
This code will fetch the test001.csv file from the w3c web server, cache it and make it available to your punchlet.
You can use the refresh option to make the operator regularly refresh your resource. Here is a plian example to fetch a json document as a resource tuple.
Tuple resource = fetchJson("https://w3c.github.io/csvw/tests/test001.json")
.asTuple();
And here is an example to reload that json every 60 seconds.
Tuple resource = fetchJson("https://w3c.github.io/csvw/tests/test001.json")
.refresh("0/60 0/1 * 1/1 * ? *")
.asTuple();
This operaror extends the FetchOperator
.
The fetch operator can also be used with local punchlet file resources. Here is an example of a punchline punch node settings:
{
"type": "punchlet_node"
"settings": {
"punchlet_file_resources": [
"standard/resources/enrichment.json"
],
"punchlet": [
"punchlet.punch"
]
}
}
You can then simply write :
Tuple resource = fetchJson(standard/resources/enrichment.json).asTuple();
As just illustrated you can define JSON files. Refer to the fFetchCsvOperator
variant for CSV files.
It is common to have json arrays. For example :
[
{"id":"id32","name":"dimi","age":56},
{"id":"id45","name":"phil","age":22},
{"id":"id55","name":"sofia","age":22}
]
You can convert this array into a dictionary as follows:
[result] = fetchJson("resource.json").hashKey("id").asTuple();
This will return to you :
"id32": { "id":"id32", "age": 56, "name": "dimi" },
"id45": { "id":"id32", "age": 22, "name": "phil" },
"id55": { "id":"id32", "age": 22, "name": "sofia" }
You can also make your dictionary keys lower case using the lowerCaseKeys() method.
Tuple
, that provides
you will all the methods. Using these to hold large hash table is not memory efficient though.
If your files are big (more than several tenth of Mbs), you can use the compact option as illustrated next.
Tuple resource = fetchJson("https://your.resource.server/resource.json")
.refresh("0/60 0/1 * 1/1 * ? *")
.hashKey("ip")
.compact()
.asTuple();
This option use a more compact strategy to represent the resource in memory.
The returned tuple provides you only with the get(String key) method.
Watch out : if your resource file contains a non Json content, (for example if put some CSV content into a JSON file), the file content will be returned to you as a plain String (leaf) value.
compactionType, hashKey, logger, lowerCaseKeys, requiredResource, runtimeContext, s3Bucket, s3endpoint, s3KeyPath, s3Object, s3SecretPath, silent, url, uuid
Constructor and Description |
---|
FetchJsonOperator(RuntimeContext r)
Create a fetch operator to load JSON resources
|
FetchJsonOperator(RuntimeContext r,
String url)
Create a fetch operator for a remote resource.
|
Modifier and Type | Method and Description |
---|---|
Tuple |
asTuple() |
IResourceBuilder |
getResourceBuilder()
Implemented by subclasses to return the adequate resource builder.
|
bestEffort, compact, compactDirect, compactInPlace, getResource, hashKey, loadAtStartup, lowerCaseKeys, refresh, required, s3AccessKey, s3AccessKeyPath, s3Bucket, s3Endpoint, s3Object, s3Secret, silent, url
public FetchJsonOperator(RuntimeContext r, String url)
r
- the punchlet runtime context.url
- an HTTP or file URI.public FetchJsonOperator(RuntimeContext r)
r
- the punchlet runtime contextpublic Tuple asTuple()
public IResourceBuilder getResourceBuilder()
FetchOperator
FetchCsvOperator
returns a builder to construct a tuple from a CSV document
while the FetchJsonOperator
returns builders to deal with JSON files.
This is called only once at init time.
getResourceBuilder
in class FetchOperator<FetchJsonOperator>
Copyright © 2022. All rights reserved.