Skip to content

Commit

Permalink
fix(docs): add full examples for remaining plugin-scripts (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutimantri authored Sep 6, 2024
1 parent 34c48f0 commit b6bda90
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,56 @@
full = true,
title = "Make an API call and pass request body to a Groovy script.",
code = """
id: api-request-to-groovy
namespace: company.team
tasks:
- id: request
type: io.kestra.plugin.core.http.Request
uri: "https://dummyjson.com/products/1"
- id: groovy
type: io.kestra.plugin.scripts.groovy.Eval
script: |
logger.info('{{ outputs.request.body }}')
- id: download
type: io.kestra.plugin.core.http.Download
uri: "https://dummyjson.com/products/1"
- id: runContextGroovy
type: io.kestra.plugin.scripts.groovy.Eval
script: |
// logger.info('Vars: {}', runContext.getVariables())
URI uri = new URI(runContext.variables.outputs.download.uri)
InputStream istream = runContext.storage().getFile(uri)
logger.info('Content: {}', istream.text)
"""
id: api_request_to_groovy
namespace: company.team
tasks:
- id: request
type: io.kestra.plugin.core.http.Request
uri: "https://dummyjson.com/products/1"
- id: groovy
type: io.kestra.plugin.scripts.groovy.Eval
script: |
logger.info('{{ outputs.request.body }}')
- id: download
type: io.kestra.plugin.core.http.Download
uri: "https://dummyjson.com/products/1"
- id: run_context_groovy
type: io.kestra.plugin.scripts.groovy.Eval
script: |
// logger.info('Vars: {}', runContext.getVariables())
URI uri = new URI(runContext.variables.outputs.download.uri)
InputStream istream = runContext.storage().getFile(uri)
logger.info('Content: {}', istream.text)
"""
),
@Example(
code = {
"outputs:",
" - out",
" - map",
"script: |",
" import io.kestra.core.models.executions.metrics.Counter",
" ",
" logger.info('executionId: {}', runContext.render('{{ execution.id }}'))",
" runContext.metric(Counter.of('total', 666, 'name', 'bla'))",
" ",
" map = Map.of('test', 'here')",
" File tempFile = runContext.workingDir().createTempFile().toFile()",
" var output = new FileOutputStream(tempFile)",
" output.write('555\\n666\\n'.getBytes())",
" ",
" out = runContext.storage().putFile(tempFile)"
}
code = """
id: groovy_eval
namespace: company.team
tasks:
- id: eval
type: io.kestra.plugin.scripts.groovy.Eval
outputs:
- out
- map
script: |
import io.kestra.core.models.executions.metrics.Counter
logger.info('executionId: {}', runContext.render('{{ execution.id }}'))
runContext.metric(Counter.of('total', 666, 'name', 'bla'))
map = Map.of('test', 'here')
File tempFile = runContext.workingDir().createTempFile().toFile()
var output = new FileOutputStream(tempFile)
output.write('555\\n666\\n'.getBytes())
out = runContext.storage().putFile(tempFile
"""
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,100 @@
examples = {
@Example(
title = "Convert row by row of a file from Kestra's internal storage.",
code = {
"from: \"{{ outputs['avro-to-gcs'] }}\"",
"script: |",
" logger.info('row: {}', row)",
"",
" if (row.get('name') == 'richard') {",
" row = null",
" } else {",
" row.put('email', row.get('name') + '@kestra.io')",
" }"
}
full = true,
code = """
id: groovy_file_transform
namespace: company.team
inputs:
- id: file
type: FILE
tasks:
- id: file_transform
type: io.kestra.plugin.scripts.groovy.FileTransform
from: "{{ inputs.file }}"
script: |
logger.info('row: {}', row)
if (row.get('name') == 'richard') {
row = null
} else {
row.put('email', row.get('name') + '@kestra.io')
}
"""
),
@Example(
title = "Create multiple rows from one row.",
code = {
"from: \"{{ outputs['avro-to-gcs'] }}\"",
"script: |",
" logger.info('row: {}', row)",
" rows = [[\"action\", \"insert\"], row]"
}
full = true,
code = """
id: groovy_file_transform
namespace: company.team
inputs:
- id: file
type: FILE
tasks:
- id: file_transform
type: io.kestra.plugin.scripts.groovy.FileTransform
from: "{{ inputs.file }}"
script: |
logger.info('row: {}', row)
rows = [["action", "insert"], row]
"""
),
@Example(
title = "Transform a JSON string to a file.",
code = {
"from: \"[{\\\"name\\\":\\\"jane\\\"}, {\\\"name\\\":\\\"richard\\\"}]\"",
"script: |",
" logger.info('row: {}', row)",
"",
" if (row.get('name') == 'richard') {",
" row = null",
" } else {",
" row.put('email', row.get('name') + '@kestra.io')",
" }"
}
full = true,
code = """
id: groovy_file_transform
namespace: company.team
inputs:
- id: json
type: JSON
defaults: [{"name":"jane"}, {"name":"richard"}]
tasks:
- id: file_transform
type: io.kestra.plugin.scripts.groovy.FileTransform
from: "{{ inputs.json }}"
script: |
logger.info('row: {}', row)
if (row.get('name') == 'richard') {
row = null
} else {
row.put('email', row.get('name') + '@kestra.io')
}
"""
),
@Example(
title = "JSON transformations using jackson library",
full = true,
code = """
id: json_transform_using_jackson
namespace: company.team
tasks:
- id: file_transform
type: io.kestra.plugin.scripts.groovy.FileTransform
from: "[{\"name\":\"John Doe\", \"age\":99, \"embedded\":{\"foo\":\"bar\"}}]"
script: |
import com.fasterxml.jackson.*
def mapper = new databind.ObjectMapper();
def jsonStr = mapper.writeValueAsString(row);
logger.info('input in json str: {}', jsonStr)
def typeRef = new core.type.TypeReference<HashMap<String,Object>>() {};
data = mapper.readValue(jsonStr, typeRef);
logger.info('json object: {}', data);
logger.info('embedded field: {}', data.embedded.foo)
"""
id: json_transform_using_jackson
namespace: company.team
tasks:
- id: file_transform
type: io.kestra.plugin.scripts.groovy.FileTransform
from: "[{\"name\":\"John Doe\", \"age\":99, \"embedded\":{\"foo\":\"bar\"}}]"
script: |
import com.fasterxml.jackson.*
def mapper = new databind.ObjectMapper();
def jsonStr = mapper.writeValueAsString(row);
logger.info('input in json str: {}', jsonStr)
def typeRef = new core.type.TypeReference<HashMap<String,Object>>() {};
data = mapper.readValue(jsonStr, typeRef);
logger.info('json object: {}', data);
logger.info('embedded field: {}', data.embedded.foo)
"""
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@
@Example(
title = "Execute JBang command to execute a JAR file.",
full = true,
code = {
code = """
id: jbang_commands
namespace: company.team
tasks:
- id: commands
type: io.kestra.plugin.scripts.jbang.Commands
commands:
- jbang --quiet --main picocli.codegen.aot.graalvm.ReflectionConfigGenerator info.picocli:picocli-codegen:4.6.3
"""
id: jbang
namespace: company.team
tasks:
- id: hello-jar
type: io.kestra.plugin.scripts.jbang.Commands
commands:
- jbang --quiet --main picocli.codegen.aot.graalvm.ReflectionConfigGenerator info.picocli:picocli-codegen:4.6.3"""
}
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
title = "Execute a script written in Java",
full = true,
code = """
id: jbang
id: jbang_script
namespace: company.team
tasks:
- id: hello-java
- id: script
type: io.kestra.plugin.scripts.jbang.Script
script: |
class helloworld {
Expand All @@ -51,49 +51,52 @@ public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}
}"""
}
"""
),
@Example(
title = "Execute a script written in Java with dependencies",
full = true,
code = """
id: jbang
id: jbang_script
namespace: company.team
tasks:
- id: hello-dependency
type: io.kestra.plugin.scripts.jbang.Script
script: |
//DEPS ch.qos.reload4j:reload4j:1.2.19
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
class classpath_example {
static final Logger logger = Logger.getLogger(classpath_example.class);
public static void main(String[] args) {
BasicConfigurator.configure();\s
logger.info("Hello World");
- id: script_with_dependency
type: io.kestra.plugin.scripts.jbang.Script
script: |
//DEPS ch.qos.reload4j:reload4j:1.2.19
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
class classpath_example {
static final Logger logger = Logger.getLogger(classpath_example.class);
public static void main(String[] args) {
BasicConfigurator.configure();\s
logger.info("Hello World");
}
}
}"""
"""
),
@Example(
title = "Execute a script written in Kotlin",
title = "Execute a script written in Kotlin.",
full = true,
code = """
id: jbang
id: jbang_script
namespace: company.team
tasks:
- id: hello-kotlin
- id: script_kotlin
type: io.kestra.plugin.scripts.jbang.Script
extension: .kt
script: |
public fun main() {
println("Hello World");
}"""
}
"""
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
full = true,
title = "Create a Julia script, install required packages and execute it. Note that instead of defining the script inline, you could create the Julia script in the embedded VS Code editor and point to its location by path. If you do so, make sure to enable namespace files by setting the `enabled` flag of the `namespaceFiles` property to `true`.",
code = """
id: script
id: julia_commands
namespace: company.team
tasks:
- id: bash
- id: commands
type: io.kestra.plugin.scripts.julia.Commands
warningOnStdErr: false
inputFiles:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
full = true,
title = "Create a Julia script, install required packages and execute it. Note that instead of defining the script inline, you could create the Julia script in the embedded VS Code editor and read its content using the `{{ read('your_script.jl') }}` function.",
code = """
id: script
id: julia_script
namespace: company.team
tasks:
- id: bash
- id: script
type: io.kestra.plugin.scripts.julia.Script
warningOnStdErr: false
script: |
Expand Down
Loading

0 comments on commit b6bda90

Please sign in to comment.