Skip to content

Commit

Permalink
learning+website: add Tee to java catalog
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Awnallah <[email protected]>
  • Loading branch information
mohamedawnallah committed Dec 10, 2024
1 parent 41aea91 commit fa580e9
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.apache.beam.learning.katas.coretransforms.tee;

import org.apache.beam.learning.katas.util.Log;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.*;
import org.apache.beam.sdk.values.PCollection;

public class Task {
public static void main(String[] args) {
PipelineOptions options = PipelineOptionsFactory.fromArgs(args).create();
Pipeline pipeline = Pipeline.create(options);

PCollection<Integer> inputData = pipeline.apply("Create Input", Create.of(1, 2, 3, 4, 5));

PCollection<Integer> output = applyTransform(inputData);

output.apply(Log.ofElements());

pipeline.run();
}


static PCollection<Integer> applyTransform(PCollection<Integer> data) {
Tee<Integer> tee = Tee.of(
consumer -> {
consumer.apply("Filter Even", Filter.by((Integer x) -> x % 2 == 0));
consumer.apply("Filter Odd", Filter.by((Integer x) -> x % 2 != 0));
}
);
return data
.apply("Tee Operations", tee)
.apply("Continue Pipeline", MapElements.via(new SimpleFunction<Integer, Integer>() {
@Override
public Integer apply(Integer input) {
return input * 10;
}
}));
}
}
29 changes: 29 additions & 0 deletions learning/katas/java/Core Transforms/Tee/Tee/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

type: edu
files:
- name: src/org/apache/beam/learning/katas/coretransforms/tee/Task.java
visible: true
placeholders:
- offset: 2294
length: 85
placeholder_text: TODO()
- name: test/org/apache/beam/learning/katas/coretransforms/tee/TaskTest.java
visible: false
39 changes: 39 additions & 0 deletions learning/katas/java/Core Transforms/Tee/Tee/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

Tee
-------

Tee is a Beam transform that allows for splitting the pipeline flow into
multiple branches while preserving the main pipeline. It is similar to the Unix
`tee` command, which outputs data to multiple destinations.

**Kata:** Implement a
[tee](https://beam.apache.org/releases/javadoc/current/org/apache/beam/sdk/transforms/Tee.html)
transform that applies side transformations to a PCollection without breaking the linear flow of the main pipeline.

<div class="hint">
Refer to <a href="https://beam.apache.org/releases/javadoc/current/org/apache/beam/sdk/transforms/Tee.html">
Tee</a> to solve this problem.
</div>

<div class="hint">
Refer to the Beam Programming Guide
<a href="https://beam.apache.org/documentation/programming-guide/#tee">
"Tee"</a> section for more information.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.beam.learning.katas.coretransforms.tee;

import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.values.PCollection;
import org.junit.Rule;
import org.junit.Test;

public class TaskTest {

@Rule
public final transient TestPipeline testPipeline = TestPipeline.create();

@Test
public void tee() {
PCollection<Integer> inputData = testPipeline.apply("Create Input", Create.of(1, 2, 3, 4, 5));

PCollection<Integer> results = Task.applyTransform(inputData);

PAssert.that(results)
.containsInAnyOrder(10, 20, 30, 40, 50);

testPipeline.run().waitUntilFinish();
}

}
21 changes: 21 additions & 0 deletions learning/katas/java/Core Transforms/Tee/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

content:
- Tee
1 change: 1 addition & 0 deletions learning/katas/java/Core Transforms/section-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ content:
- Branching
- Composite Transform
- DoFn Additional Parameters
- Tee
2 changes: 1 addition & 1 deletion learning/katas/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

buildscript {
ext {
beamVersion = '2.38.0'
beamVersion = '2.61.0'
guavaVersion = '31.0.1-jre'
jodaTimeVersion = '2.10.10'
slf4jVersion = '1.7.30'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: "Tee"
---
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
# Tee
<table align="left">
<a target="_blank" class="button"
href="https://beam.apache.org/releases/javadoc/current/index.html?org/apache/beam/sdk/transforms/Tee.html">
<img src="/images/logos/sdks/java.png" width="20px" height="20px"
alt="Javadoc" />
Javadoc
</a>
</table>
<br><br>


The `Tee` transform allows for splitting the pipeline flow into multiple branches,
enabling the application of side transformations while preserving the main pipeline.
This is similar to the Unix `tee` command, which duplicates input and sends it to
multiple outputs without interrupting the main flow.

See more information in the [Beam Programming Guide](/documentation/programming-guide/#tee).

## Examples

{{< playground height="700px" >}}
{{< playground_snippet language="java" path="PG_BEAMDOC_SDK_JAVA_Tee" show="main_section" >}}
{{< /playground >}}

0 comments on commit fa580e9

Please sign in to comment.