-
Notifications
You must be signed in to change notification settings - Fork 15.1k
MINOR: putting back kstream stateful transform methods #292
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
streams/src/main/java/org/apache/kafka/streams/kstream/Transformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /** | ||
| * 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.kafka.streams.kstream; | ||
|
|
||
| import org.apache.kafka.streams.processor.ProcessorContext; | ||
|
|
||
| public interface Transformer<K, V, R> { | ||
|
|
||
| /** | ||
| * Initialize this transformer with the given context. The framework ensures this is called once per processor when the topology | ||
| * that contains it is initialized. | ||
| * <p> | ||
| * If this tranformer is to be {@link #punctuate(long) called periodically} by the framework, then this method should | ||
| * {@link ProcessorContext#schedule(long) schedule itself} with the provided context. | ||
| * | ||
| * @param context the context; may not be null | ||
| */ | ||
| void init(ProcessorContext context); | ||
|
|
||
| /** | ||
| * Transform the message with the given key and value. | ||
| * | ||
| * @param key the key for the message | ||
| * @param value the value for the message | ||
| * @return new value | ||
| */ | ||
| R transform(K key, V value); | ||
|
|
||
| /** | ||
| * Perform any periodic operations, if this processor {@link ProcessorContext#schedule(long) schedule itself} with the context | ||
| * during {@link #init(ProcessorContext) initialization}. | ||
| * | ||
| * @param timestamp the stream time when this method is being called | ||
| */ | ||
| void punctuate(long timestamp); | ||
|
|
||
| /** | ||
| * Close this processor and clean up any resources. | ||
| */ | ||
| void close(); | ||
|
|
||
| } |
24 changes: 24 additions & 0 deletions
24
streams/src/main/java/org/apache/kafka/streams/kstream/TransformerSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * 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.kafka.streams.kstream; | ||
|
|
||
| public interface TransformerSupplier<K, V, R> { | ||
|
|
||
| Transformer<K, V, R> get(); | ||
|
|
||
| } |
56 changes: 56 additions & 0 deletions
56
streams/src/main/java/org/apache/kafka/streams/kstream/ValueTransformer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** | ||
| * 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.kafka.streams.kstream; | ||
|
|
||
| import org.apache.kafka.streams.processor.ProcessorContext; | ||
|
|
||
| public interface ValueTransformer<V, R> { | ||
|
|
||
| /** | ||
| * Initialize this transformer with the given context. The framework ensures this is called once per processor when the topology | ||
| * that contains it is initialized. | ||
| * <p> | ||
| * If this tranformer is to be {@link #punctuate(long) called periodically} by the framework, then this method should | ||
| * {@link ProcessorContext#schedule(long) schedule itself} with the provided context. | ||
| * | ||
| * @param context the context; may not be null | ||
| */ | ||
| void init(ProcessorContext context); | ||
|
|
||
| /** | ||
| * Transform the message with the given key and value. | ||
| * | ||
| * @param value the value for the message | ||
| * @return new value | ||
| */ | ||
| R transform(V value); | ||
|
|
||
| /** | ||
| * Perform any periodic operations, if this processor {@link ProcessorContext#schedule(long) schedule itself} with the context | ||
| * during {@link #init(ProcessorContext) initialization}. | ||
| * | ||
| * @param timestamp the stream time when this method is being called | ||
| */ | ||
| void punctuate(long timestamp); | ||
|
|
||
| /** | ||
| * Close this processor and clean up any resources. | ||
| */ | ||
| void close(); | ||
|
|
||
| } |
24 changes: 24 additions & 0 deletions
24
streams/src/main/java/org/apache/kafka/streams/kstream/ValueTransformerSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * 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.kafka.streams.kstream; | ||
|
|
||
| public interface ValueTransformerSupplier<V, R> { | ||
|
|
||
| ValueTransformer<V, R> get(); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
streams/src/main/java/org/apache/kafka/streams/kstream/internals/KStreamTransform.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * 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.kafka.streams.kstream.internals; | ||
|
|
||
| import org.apache.kafka.streams.kstream.KeyValue; | ||
| import org.apache.kafka.streams.kstream.Transformer; | ||
| import org.apache.kafka.streams.kstream.TransformerSupplier; | ||
| import org.apache.kafka.streams.processor.Processor; | ||
| import org.apache.kafka.streams.processor.ProcessorContext; | ||
| import org.apache.kafka.streams.processor.ProcessorSupplier; | ||
|
|
||
| public class KStreamTransform<K1, V1, K2, V2> implements ProcessorSupplier<K1, V1> { | ||
|
|
||
| private final TransformerSupplier<K1, V1, KeyValue<K2, V2>> transformerSupplier; | ||
|
|
||
| public KStreamTransform(TransformerSupplier<K1, V1, KeyValue<K2, V2>> transformerSupplier) { | ||
| this.transformerSupplier = transformerSupplier; | ||
| } | ||
|
|
||
| @Override | ||
| public Processor<K1, V1> get() { | ||
| return new KStreamTransformProcessor(transformerSupplier.get()); | ||
| } | ||
|
|
||
| public static class KStreamTransformProcessor<K1, V1, K2, V2> implements Processor<K1, V1> { | ||
|
|
||
| private final Transformer<K1, V1, KeyValue<K2, V2>> transformer; | ||
| private ProcessorContext context; | ||
|
|
||
| public KStreamTransformProcessor(Transformer<K1, V1, KeyValue<K2, V2>> transformer) { | ||
| this.transformer = transformer; | ||
| } | ||
|
|
||
| @Override | ||
| public void init(ProcessorContext context) { | ||
| transformer.init(context); | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public void process(K1 key, V1 value) { | ||
| KeyValue<K2, V2> pair = transformer.transform(key, value); | ||
| context.forward(pair.key, pair.value); | ||
| } | ||
|
|
||
| @Override | ||
| public void punctuate(long timestamp) { | ||
| transformer.punctuate(timestamp); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| transformer.close(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functionality of process() now is completely covered by transform: users can define a transform function with return type R be "Void" and add a dummy "return null" in the end of the function. And then in KStream we can add
public void transform(TransformerSupplier<K, V, Void>)
to replace the "process()" call. Having both process() and transform() might be confusing to users, so I would suggest we just remove process() here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would require a separate KStreamVoidTransformProcessor, but I feel it worth the internal cost for simpler public APIs.