SmartModule for processing json records using jq syntax. The smartmodule is based on the rust version of jq named jaq.
Use the jaq playground to test Jq expressions before using them in a SmartModule.
Download a pre-compiled version of this SmartModule from the InfinyOn Hub, create a topic, and to run the tests.
fluvio hub smartmodule download infinyon/[email protected]
Create a topic called jaq
:
fluvio topic create jaq
Run the tests:
-
Produce:
echo '["zero", "one", "two"]' | fluvio produce jaq
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".[]"
-
Result:
["zero","one","two"]
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".[1]"
-
Result:
"one"
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".[7]"
-
Result:
null
Numbers are computed:
-
Produce:
echo '{"x": 5, "y": 10}' | fluvio produce jaq
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".x + .y"
-
Result:
15
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".x * .y"
-
Result:
50
Arrays are concatenated.
-
Produce:
echo '{"x": [1, 2], "y": [3, 4]}' | fluvio produce jaq
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".x + .y"
-
Result:
[1,2,3,4]
Strings are joined together.
-
Produce:
echo '{"x": "foo", "y": "bar"}' | fluvio produce jaq
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".x + .y"
-
Result:
"foobar"
Objects are merged together.
-
Produce:
echo '{"x": {"foo": "bar"}, "y": {"hello": "world"}}' | fluvio produce jaq
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter=".x + .y"
-
Result:
{"foo":"bar","hello":"world"}
We'll use cars.json data set to test filtering.
-
Produce:
fluvio produce jaq -f test-data/cars.json --raw
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter='.[] | select(.Year == "1972-01-01")' -O json
-
Result:
[ { "Acceleration": 16.5, "Cylinders": 4, "Displacement": 97, "Horsepower": 88, "Miles_per_Gallon": 27, "Name": "toyota corolla 1600 (sw)", "Origin": "Japan", "Weight_in_lbs": 2100, "Year": "1972-01-01" } ... ]
Map from one object to another.
-
Consume:
fluvio consume jaq -dT=1 --smartmodule infinyon/[email protected] -e filter='[ .[] | select(.Year == "1972-01-01") | {name: .Name, performance: {horsepower: .Horsepower, acceleration: .Acceleration, cylinders: .Cylinders, displacement: .Displacement}, efficiency: {milesPerGallon: .Miles_per_Gallon, weightInLbs: .Weight_in_lbs}, build: {year: .Year, origin: .Origin}} ]' -O json
-
Result:
[ { "build": { "origin": "Japan", "year": "1972-01-01" }, "efficiency": { "milesPerGallon": 27, "weightInLbs": 2100 }, "name": "toyota corolla 1600 (sw)", "performance": { "acceleration": 16.5, "cylinders": 4, "displacement": 97, "horsepower": 88 } } ... ]
Smartmodule can be used in a connector.
Download Connector from the Hub:
cdk hub download infinyon/[email protected]
Create a connector yaml file named quotes.yaml
:
apiVersion: 0.1.0
meta:
version: 0.4.3
name: quotes-connector
type: http-source
topic: quotes
http:
endpoint: https://demo-data.infinyon.com/api/quote
interval: 10s
transforms:
- uses: infinyon/[email protected]
with:
filter: ".quote"
Run the connector in the cloud
fluvio cloud connector create --config quotes.yaml
Or use cdk
to deploy locally.
Compile and test the smartmodule using the smdk
tool.
Compile the smartmodule:
smdk build
1. Use test-data/fruit-input.json to test simple lookup:
smdk test --file test-data/fruit-input.json --raw -e filter=.fruit
{"name":"apple","color":"green","price":1.2}
2. Use test-data/creatures-input.json to test array lookup:
smdk test --file test-data/creatures-input.json --raw -e filter=".[] | .name"
["Sammy","Bubbles","Splish","Splash"]