From ab5c5f783ced14ae81bf7f9d0ffa3ed11044d4e4 Mon Sep 17 00:00:00 2001 From: Josh Liburdi Date: Thu, 4 Jan 2024 20:37:12 +0000 Subject: [PATCH] docs(examples): array group --- .../transform/array/group/config.jsonnet | 28 +++++++++++++ .../config/transform/array/group/data.json | 1 + .../transform/array/to_object/config.jsonnet | 41 ------------------- .../transform/array/to_object/data.json | 1 - 4 files changed, 29 insertions(+), 42 deletions(-) create mode 100644 examples/build/config/transform/array/group/config.jsonnet create mode 100644 examples/build/config/transform/array/group/data.json delete mode 100644 examples/build/config/transform/array/to_object/config.jsonnet delete mode 100644 examples/build/config/transform/array/to_object/data.json diff --git a/examples/build/config/transform/array/group/config.jsonnet b/examples/build/config/transform/array/group/config.jsonnet new file mode 100644 index 00000000..d528a73b --- /dev/null +++ b/examples/build/config/transform/array/group/config.jsonnet @@ -0,0 +1,28 @@ +// This example groups an array of arrays into an array of objects +// based on index and configured keys. +local sub = import '../../../../../../build/config/substation.libsonnet'; + +local files_key = 'meta files'; + +{ + concurrency: 1, + transforms: [ + // This example sends data to stdout at each step to iteratively show + // how the data is transformed. + sub.tf.send.stdout(), + // Copy the object to metadata, where it is grouped. + sub.tf.obj.cp({ object: { target_key: files_key } }), + // Elements from the file_name array are transformed and derived file extensions + // are added to a new array. + sub.tf.meta.for_each({ + object: { source_key: sub.helpers.object.get_element(files_key, 'file_name'), target_key: sub.helpers.object.append(files_key, 'file_extension') }, + transform: sub.tf.string.capture(settings={ pattern: '\\.([^\\.]+)$' }), + }), + // The arrays grouped into an array of objects, then copied to the message's data field. + // For example: + // + // [{name: name1, type: type1, size: size1, extension: extension1}, {name: name2, type: type2, size: size2, extension: extension2}] + sub.tf.object.cp({ object: { source_key: files_key + '|@group' } }), + sub.tf.send.stdout(), + ], +} diff --git a/examples/build/config/transform/array/group/data.json b/examples/build/config/transform/array/group/data.json new file mode 100644 index 00000000..a0e2819a --- /dev/null +++ b/examples/build/config/transform/array/group/data.json @@ -0,0 +1 @@ +{"file_name":["foo.txt","bar.html"],"file_type":["text/plain","text/html"],"file_size":[100,500]} diff --git a/examples/build/config/transform/array/to_object/config.jsonnet b/examples/build/config/transform/array/to_object/config.jsonnet deleted file mode 100644 index adbf464a..00000000 --- a/examples/build/config/transform/array/to_object/config.jsonnet +++ /dev/null @@ -1,41 +0,0 @@ -// This example groups an array of arrays into an array of objects -// based on index and configured keys. -local sub = import '../../../../../../build/config/substation.libsonnet'; - -local files_key = 'files'; - -{ - concurrency: 1, - transforms: [ - // This example sends data to stdout at each step to iteratively show - // how the data is transformed. - sub.tf.send.stdout(), - // Each array is appended to the files array. This creates - // an array of arrays. For example: - // - // [[name1, name2], [type1, type2], [size1, size2]] - sub.tf.obj.cp(settings={ object: { source_key: '[file_names,file_types,file_sizes]', target_key: files_key } }), - sub.tf.send.stdout(), - // Elements of the file_names array are transformed, the file extension - // results are appended to the files array, and the arrays are zipped together. - // For example: - // - // [[name1, type1, size1, extension1], [name2, type2, size2, extension2]] - sub.tf.meta.for_each({ - object: { source_key: 'file_names', target_key: sub.helpers.object.append_array(files_key) }, - transform: sub.tf.string.capture(settings={ pattern: '\\.([^\\.]+)$' }), - }), - sub.tf.array.zip({ object: { source_key: files_key, target_key: files_key } }), - sub.tf.send.stdout(), - // The array of arrays is transformed into an array of objects based on - // index and configured keys. For example: - // - // [{name: name1, type: type1, size: size1, extension: extension1}, {name: name2, type: type2, size: size2, extension: extension2}] - sub.tf.array.to.object({ object: { source_key: files_key, target_key: files_key }, object_keys: ['name', 'type', 'size', 'extension'] }), - sub.tf.send.stdout(), - // The array of objects are transformed into new events. - sub.tf.obj.cp({ object: { source_key: files_key } }), - sub.tf.agg.from.array(), - sub.tf.send.stdout(), - ], -} diff --git a/examples/build/config/transform/array/to_object/data.json b/examples/build/config/transform/array/to_object/data.json deleted file mode 100644 index e98fde03..00000000 --- a/examples/build/config/transform/array/to_object/data.json +++ /dev/null @@ -1 +0,0 @@ -{"file_names":["foo.txt","bar.html"],"file_types":["text/plain","text/html"],"file_sizes":[100,500]}