forked from Axual/ksml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
62 changed files
with
5,832 additions
and
676 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# $schema: https://raw.githubusercontent.com/Axual/ksml/refs/heads/main/docs/ksml-language-spec.json | ||
|
||
# This example shows how to generate data and have it sent to a target topic in a given format. | ||
|
||
functions: | ||
generate_sensordata_message: | ||
type: generator | ||
globalCode: | | ||
import time | ||
import random | ||
sensorCounter = 0 | ||
code: | | ||
global sensorCounter | ||
key = "sensor"+str(sensorCounter) # Set the key to return ("sensor0" to "sensor9") | ||
sensorCounter = (sensorCounter+1) % 10 # Increase the counter for next iteration | ||
# Generate some random sensor measurement data | ||
types = { 0: { "type": "AREA", "unit": random.choice([ "m2", "ft2" ]), "value": str(random.randrange(1000)) }, | ||
1: { "type": "HUMIDITY", "unit": random.choice([ "g/m3", "%" ]), "value": str(random.randrange(100)) }, | ||
2: { "type": "LENGTH", "unit": random.choice([ "m", "ft" ]), "value": str(random.randrange(1000)) }, | ||
3: { "type": "STATE", "unit": "state", "value": random.choice([ "off", "on" ]) }, | ||
4: { "type": "TEMPERATURE", "unit": random.choice([ "C", "F" ]), "value": str(random.randrange(-100, 100)) } | ||
} | ||
# Build the result value using any of the above measurement types | ||
value = { "name": key, "timestamp": str(round(time.time()*1000)), **random.choice(types) } | ||
value["color"] = random.choice([ "black", "blue", "red", "yellow", "white" ]) | ||
value["owner"] = random.choice([ "Alice", "Bob", "Charlie", "Dave", "Evan" ]) | ||
value["city"] = random.choice([ "Amsterdam", "Xanten", "Utrecht", "Alkmaar", "Leiden" ]) | ||
if random.randrange(10) == 0: | ||
key = None | ||
if random.randrange(10) == 0: | ||
value = None | ||
expression: (key, value) # Return a message tuple with the key and value | ||
resultType: (string, json) # Indicate the type of key and value | ||
|
||
producers: | ||
# Produce 10k messages in batches of 100 messages with a 1ms interval | ||
sensordata_avro_producer: | ||
generator: generate_sensordata_message | ||
interval: 1 | ||
count: 10000 | ||
batchSize: 100 | ||
to: | ||
topic: ksml_sensordata_avro | ||
keyType: string | ||
valueType: avro:SensorData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# $schema: https://raw.githubusercontent.com/Axual/ksml/refs/heads/main/docs/ksml-language-spec.json | ||
|
||
# This example shows how to generate data and have it sent to a target topic in a given format. | ||
|
||
functions: | ||
generate_sensordata_message: | ||
type: generator | ||
globalCode: | | ||
import time | ||
import random | ||
sensorCounter = 0 | ||
code: | | ||
global sensorCounter | ||
key = "sensor"+str(sensorCounter) # Set the key to return ("sensor0" to "sensor9") | ||
sensorCounter = (sensorCounter+1) % 10 # Increase the counter for next iteration | ||
# Generate some random sensor measurement data | ||
types = { 0: { "type": "AREA", "unit": random.choice([ "m2", "ft2" ]), "value": str(random.randrange(1000)) }, | ||
1: { "type": "HUMIDITY", "unit": random.choice([ "g/m3", "%" ]), "value": str(random.randrange(100)) }, | ||
2: { "type": "LENGTH", "unit": random.choice([ "m", "ft" ]), "value": str(random.randrange(1000)) }, | ||
3: { "type": "STATE", "unit": "state", "value": random.choice([ "off", "on" ]) }, | ||
4: { "type": "TEMPERATURE", "unit": random.choice([ "C", "F" ]), "value": str(random.randrange(-100, 100)) } | ||
} | ||
# Build the result value using any of the above measurement types | ||
value = { "name": key, "timestamp": str(round(time.time()*1000)), **random.choice(types) } | ||
value["color"] = random.choice([ "black", "blue", "red", "yellow", "white" ]) | ||
value["owner"] = random.choice([ "Alice", "Bob", "Charlie", "Dave", "Evan" ]) | ||
value["city"] = random.choice([ "Amsterdam", "Xanten", "Utrecht", "Alkmaar", "Leiden" ]) | ||
if random.randrange(10) == 0: | ||
key = None | ||
if random.randrange(10) == 0: | ||
value = None | ||
expression: (key, value) # Return a message tuple with the key and value | ||
resultType: (string, json) # Indicate the type of key and value | ||
|
||
producers: | ||
# This example uses the otherAvro notation, using the Apicurio Registry API and serializers. | ||
# See the ksml-data-generator.yaml for the notation definition | ||
sensordata_avro_producer_binary: | ||
generator: generate_sensordata_message | ||
interval: 3s | ||
to: | ||
topic: ksml_sensordata_avro_binary | ||
keyType: string | ||
valueType: otherAvro:SensorData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# $schema: https://raw.githubusercontent.com/Axual/ksml/refs/heads/main/docs/ksml-language-spec.json | ||
|
||
# This example behaves similarly to example 2 (copy) but includes Python code to measure KSML's performance. It does | ||
# so by storing the message count and startup timestamp in global variables, and outputting log statements every 100 | ||
# #messages, containing #messages processed, #seconds running since first message came in, and average #msg/sec. | ||
|
||
streams: | ||
sensor_source: | ||
topic: ksml_sensordata_avro | ||
keyType: string | ||
valueType: avro:SensorData | ||
offsetResetPolicy: latest | ||
sensor_copy: | ||
topic: ksml_sensordata_copy | ||
keyType: string | ||
valueType: avro:SensorData | ||
|
||
pipelines: | ||
main: | ||
from: sensor_source | ||
via: | ||
# Use a PEEK operation to initialize the global messageCount and startTime | ||
- type: peek | ||
forEach: | ||
globalCode: | | ||
from datetime import datetime | ||
messageCount, startTime = 0, 0 | ||
code: | | ||
# Declare global variables, since we are updating them below | ||
global messageCount, startTime | ||
if messageCount == 0: | ||
startTime = datetime.now() | ||
messageCount += 1 | ||
# Output performance thus far, done in separate PEEK to allow easy insertion of other operations above | ||
- type: peek | ||
forEach: | ||
code: | | ||
# No need to include the global statement here, since we only read and don't update the global variables | ||
# For every 100 messages that we process, we output a log statement with performance indication so far | ||
if messageCount % 100 == 0: | ||
# Prevent division by zero by using 1 second as minimum | ||
runtime = max(1, (datetime.now() - startTime).total_seconds()) | ||
log.warn("Processed {} messages in {} seconds = {} msg/sec", messageCount, runtime, round(messageCount / runtime, 2)) | ||
to: sensor_copy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.