Rune is a dynamic scripting language with a Rust-like syntax. The Rune Script Node allows you to create scripts directly within a graph. You can use the Rune Script Node to perform custom operations on your data that are not supported by the built-in nodes.
// define a simple function that adds two numbers
pub fn add(a, b) { a + b }
// define the main function that will be called when the node is executed, passing in the node object
pub async fn main(node) {
node.log_info(format!("Hello from Rune. 4+7={}", add(4,7)));
}
The node object provides access to the node's inputs, outputs, and other properties.
/// Run forever, mirroring the input to the output
pub async fn main(node) {
// Wait until the node's input is connected and available
let input = node.get_input(0).await?;
loop {
// Wait for the next value from the input
let value = input.next().await?;
// Print the received value to the node log
node.log_info(format!("Received value: {:?}", value));
match value {
NodeInputEvent::Payload(payload) => {
// Send the value to the output
node.send_to_output(0, payload).await?;
}
NodeInputEvent::EndOfStream => {
}
NodeInputEvent::Connection(connection_specification) => {
// The Connection Specification tells nodes how to interpret the data
node.output_specification_set(0, connection_specification);
}
NodeInputEvent::Disconnect => {
}
}
}
}
Many nodes in DataCogs use Apache Arrow as the data format. The Rune Script Node provides a way to work with Arrow data.
pub async fn main(node) {
// Wait until the node's input is connected and available
let input = node.get_input(0).await?;
// Wrap the input in an ArrowReader
let reader = ArrowReader::new(input);
loop {
// Wait for the next RecordBatch
let record = reader.get_records().await?;
// If we received a RecordBatch, print out information about it
if let Some(record) = record {
node.log_info(format!("Record: {:?}", record));
let schema = record.schema();
let fields = schema.fields();
node.log_info(format!("Schema: {:?}", schema));
node.log_info(format!("Fields: {:?}", fields));
// Print out information about each field
node.log_info("Fields:");
for field in fields {
if let Some(data_type) = field.data_type() {
node.log_info(
format!("\t{} {:?} {:?}", field.name(), data_type, field.metadata())
);
}
}
// Print out information about each column in the record
node.log_info("Columns:");
for column in record.get_columns() {
node.log_info(
format!("\t{:?}", column)
);
// Print out the first value in the column
let value = column.get(0);
node.log_info(
format!("\tValue[0]: {:?}", value)
);
}
} else {
break;
}
}
}
You can create a custom user interface for the Rune Script Node using HTML. The HTML is configured directly in the node, and can include JavaScript, forms, HTML canvas, and more.
A MessagePort is automatically created for the Rune context (running in a Web Worker) and HTML context to communicate with each other.