Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: start whats'new #287

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdf/_embeds/install-sdf.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fvm install sdf-beta3
7 changes: 4 additions & 3 deletions sdf/cli/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ description: Stateful Dataflows Command Line Interface.
sidebar_position: 10
---

import CodeBlock from '@theme/CodeBlock';
import InstallFvm from '!!raw-loader!../_embeds/install-sdf.bash';

Stateful Dataflows (sdf) is a binary, shipped with fluvio, that helps developers build, test, and deploy packages and dataflows.

### Install SDF
Expand All @@ -16,9 +19,7 @@ $ curl -fsS https://hub.infinyon.cloud/install/install.sh | bash

2. Install the preview release:

```bash copy="fl"
$ fvm install sdf-beta2
```
<CodeBlock language="bash">{InstallFvm}</CodeBlock>

### SDF Commands

Expand Down
296 changes: 3 additions & 293 deletions sdf/composition/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -91,172 +91,10 @@ The sections are as follows:
* `functions` - defines functions in the package
* `dev` - defines substitutions used during development and test


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is "Build and Test a Package" removed?

Copy link
Contributor Author

@morenol morenol Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is duplicated in Quickstart

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep in package and remove in Quickstart.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to follow the current docs.

First did https://www.fluvio.io/sdf/composition/overview and then https://www.fluvio.io/sdf/composition/quickstart

When I was in quickstart I got annoyed because it was exactly the same of the last part of overview. Are you suggesting to remove quickstart and only keep all in overview?

Copy link
Contributor

@ajhunyady ajhunyady Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see. Yes, the Overview should be concepts and no code (no step-by-step). I didn't realize that part was duplicated.

Copy link
Contributor Author

@morenol morenol Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is the quickstart for composition, not the quickstart for sdf, Should I remove it? since in composition, inline is not allowed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated my comment above ☝️

#### Build and Test a Package

We'll use the [SDF] command line tool with the package definition file to generate the WebAssembly glue code and the placeholder for the custom logic.

```bash copy="fl"
$ sdf -h
Stateful Dataflow Command Line Interface

Usage: sdf <COMMAND>

Commands:
clean Clean generated artifacts such as state and internal objects
build Build package (requires: sdf-package.yaml)
generate Generate package (requires: sdf-package.yaml)
update Update package (requires: sdf-package.yaml)
test Test command shell (requires sdf-package.yaml)
run Run dataflow (requires: dataflow.yaml)
setup Setup pre-requisites for Stateful Dataflows
version Prints binary version
log Print dataflow logs
```

Let's start with an example. Create a package that split sentences into words and counts the number of characters in each word.

##### 1. Create the Package file

Create a fresh project directory `split-sentence` with two subdirectories: `packages` and `sentence`:

```bash
$ mkdir -p split-sentence/packages/sentence
$ cd split-sentence/packages/sentence
```

Inside the `sentence` directory and create the `sdf-package.yaml` and add the following content:

```yaml
#sdf-package.yaml
apiVersion: 0.5.0

meta:
name: sentence-pkg
version: 0.1.0
namespace: example

functions:

sentence-to-words:
operator: flat-map
inputs:
- name: sentence
type: string
output:
type: string

augment-count:
operator: map
inputs:
- name: word
type: string
output:
type: string

dev:
converter: raw
```

##### 2. Generate the Package Project

Use SDF generate command to build the project:

```bash copy="fl"
$ sdf generate
```

The generator created several directories and files that we'll edit next.


##### 3. Add the Custom Code

First, let's update the first function, `sentence-to-words`. Open `rust/sentence-to-words/src/lib.rs` and update the function body with the following code:

```rust
pub(crate) fn sentence_to_words(sentence: String) -> Result<Vec<String>> {
Ok(sentence.split_whitespace().map(String::from).collect())
}
```

Next update `augment_count`. Open `rust/augment-count/src/lib.rs` and replace the function body:

```rust
pub(crate) fn augment_count(word: String) -> Result<String> {
Ok(format!("{}({})", word, word.chars().count()))
}
```

Let's add some tests as well:

```rust
#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_augment_count() {
let input = "Hello".to_string();
let output = augment_count(input);
assert_eq!(output.unwrap(), "Hello(5)");
}
}
```

We've implemented both functions; it's time to compile and test our work.


##### 4. Build and Test the Package

To build the package, run:

```bash
$ sdf build
```

Use `sdf test` interactive shell to test the code:

```bash
$ sdf test
```

In the test shell, you can view the functions available for testing:

```bash copy="fl"
>> show functions
sentence-to-words
augment-count
```

Let's test `sentence-to-words` first:

```bash copy="fl"
>> test function sentence-to-words --input "Hello World"
Hello
World
```

Next, test `augment-count`:

```bash copy="fl"
>> test function augment-count --input "Hello"
Hello(5)
```

You may also test the `rust` code via Cargo:

```bash
$ cd rust
$ cargo test
```

The tests passed, and the package is now ready to use in the dataflow file.

## The Dataflow

The **dataflow** imports functions, types, and states from one more package. Packages may also import components from others; however, dataflow maintains the final composition.


#### `dataflow.yaml`

The [SDF] command line tool uses the dataflow definition file `dataflow.yaml` to assemble the data application, and it has the following hierarchy:
Expand Down Expand Up @@ -326,144 +164,16 @@ The sections are as follows:

The dataflow file has other variants such as `window` and `partitions`, which are omitted for simplicity. For additional details check the [Dataflow file] section.

# Getting started

#### Create a Dataflow

We'll import the package built in the [previous](#build-and-test-a-package) section to create the `split-sentence` dataflow.

##### 1. Create the Dataflow file

Create a dataflow file in the directory `split-sentence` directory:

```bash
$ cd split-sentence
```

Create the `dataflow.yaml` and add the following content:

```yaml name="dataflow.yaml"
apiVersion: 0.5.0

meta:
name: split-sentence
version: 0.1.0
namespace: example

imports:
- pkg: example/[email protected]
functions:
- name: sentence-to-words
- name: augment-count

config:
converter: raw

topics:
sentence:
schema:
value:
type: string
converter: raw
words:
schema:
value:
type: string
converter: raw

services:
sentence-words:
sources:
- type: topic
id: sentence

transforms:
- operator: flat-map
uses: sentence-to-words
- operator: map
uses: augment-count

sinks:
- type: topic
id: words

dev:
imports:
- pkg: example/[email protected]
path: ./packages/sentence
```

##### 2. Run the Dataflow

Use `sdf` command line tool to run the dataflow:

```bash copy="fl"
$ sdf run --dev
```

Use `--dev` to ask the engine to change the path to the local package. Without this flag, the engine will look for the package in `InfinyOn Hub`.


##### 3. Test the Dataflow

1. Produce sentences to in `sentence` topic:

```bash copy="fl"
$ fluvio produce sentence
```

```bash
Hello world
Hi there
```

Consume from `words` to retrieve the result:

```bash
$ fluvio consume words -Bd
```
See [next section] for a quickstart.

```bash
Hello(5)
world(5)
Hi(2)
there(5)
```

##### 4. Show State

The dataflow collects runtime metrics that you can inspect in the runtime terminal.

Check the `sentence-to-words` counters:

```bash copy="fl"
>> show state sentence-words/sentence-to-words/metrics --table
Key Window succeeded failed
stats * 2 0
```

Check the `augment-count` counters:

```bash copy="fl"
>> show state sentence-words/augment-count/metrics --table
Key Window succeeded failed
stats * 4 0
```

Congratulations! You've successfully built and run a composable dataflow! The project is available for download in [github].

##### 5. Clean-up

Exit the `sdf` terminal and remove the topics:

```bash
fluvio topic delete sentence
fluvio topic delete words
```

### References

* [Example Workflows in github]

[next section]: quickstart.mdx
[Inline Dataflows]: sdf/quickstart.mdx
[Dataflow file]: sdf/concepts/dataflow-yaml.mdx
[SDF]: sdf/cli/index.mdx
Expand Down
Loading