-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Pulsar IO] Clarify usage of KVRecord on Sinks #10113
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
3 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
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
48 changes: 48 additions & 0 deletions
48
...r-functions/instance/src/main/java/org/apache/pulsar/functions/instance/SinkKVRecord.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,48 @@ | ||
| /** | ||
| * 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.pulsar.functions.instance; | ||
|
|
||
| import org.apache.pulsar.client.api.Schema; | ||
| import org.apache.pulsar.client.impl.schema.KeyValueSchema; | ||
| import org.apache.pulsar.common.schema.KeyValue; | ||
| import org.apache.pulsar.common.schema.KeyValueEncodingType; | ||
| import org.apache.pulsar.functions.api.KVRecord; | ||
| import org.apache.pulsar.functions.api.Record; | ||
|
|
||
| public class SinkKVRecord<K,V> extends SinkRecord<KeyValue<K,V>> implements KVRecord<K,V> { | ||
|
|
||
| public SinkKVRecord(Record<KeyValue<K, V>> sourceRecord, KeyValue<K, V> value) { | ||
| super(sourceRecord, value); | ||
| } | ||
|
|
||
| @Override | ||
| public Schema<K> getKeySchema() { | ||
| return ((KeyValueSchema) this.getSchema()).getKeySchema(); | ||
| } | ||
|
|
||
| @Override | ||
| public Schema<V> getValueSchema() { | ||
| return ((KeyValueSchema) this.getSchema()).getValueSchema(); | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValueEncodingType getKeyValueEncodingType() { | ||
| return ((KeyValueSchema) this.getSchema()).getKeyValueEncodingType(); | ||
| } | ||
| } |
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
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
60 changes: 60 additions & 0 deletions
60
...test-functions/src/main/java/org/apache/pulsar/tests/integration/io/TestKeyValueSink.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,60 @@ | ||
| /** | ||
| * 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.pulsar.tests.integration.io; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.pulsar.common.schema.KeyValue; | ||
| import org.apache.pulsar.common.schema.SchemaType; | ||
| import org.apache.pulsar.functions.api.KVRecord; | ||
| import org.apache.pulsar.functions.api.Record; | ||
| import org.apache.pulsar.io.core.Sink; | ||
| import org.apache.pulsar.io.core.SinkContext; | ||
| import org.apache.pulsar.io.core.Source; | ||
| import org.apache.pulsar.io.core.SourceContext; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| @Slf4j | ||
| public class TestKeyValueSink implements Sink<KeyValue<String, Integer>> { | ||
|
|
||
| @Override | ||
| public void open(Map<String, Object> config, SinkContext sourceContext) throws Exception { | ||
| } | ||
|
|
||
| public void write(Record<KeyValue<String, Integer>> record) { | ||
| log.info("write {} {} {}", record, record.getClass(), record.getSchema()); | ||
| if (!(record instanceof KVRecord)) { | ||
| throw new RuntimeException("Expected a KVRecord, but got a "+record.getClass()); | ||
| } | ||
| KVRecord<String, Integer> kvRecord = (KVRecord<String, Integer>) record; | ||
| if (kvRecord.getKeySchema().getSchemaInfo().getType() != SchemaType.STRING) { | ||
| throw new RuntimeException("Expected a String key schema but it was a "+kvRecord.getKeySchema()); | ||
| } | ||
|
|
||
| if (kvRecord.getValueSchema().getSchemaInfo().getType() != SchemaType.INT32) { | ||
| throw new RuntimeException("Expected a Integer key schema but it was a "+kvRecord.getValueSchema()); | ||
| } | ||
| } | ||
| @Override | ||
| public void close() throws Exception { | ||
|
|
||
| } | ||
| } |
161 changes: 161 additions & 0 deletions
161
...egration/src/test/java/org/apache/pulsar/tests/integration/io/PulsarKeyValueSinkTest.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,161 @@ | ||
| /** | ||
| * 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.pulsar.tests.integration.io; | ||
|
|
||
| import lombok.Cleanup; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.pulsar.client.admin.PulsarAdmin; | ||
| import org.apache.pulsar.client.api.Producer; | ||
| import org.apache.pulsar.client.api.PulsarClient; | ||
| import org.apache.pulsar.client.api.Schema; | ||
| import org.apache.pulsar.common.policies.data.SinkStatus; | ||
| import org.apache.pulsar.common.schema.KeyValue; | ||
| import org.apache.pulsar.tests.integration.docker.ContainerExecException; | ||
| import org.apache.pulsar.tests.integration.docker.ContainerExecResult; | ||
| import org.apache.pulsar.tests.integration.suites.PulsarStandaloneTestSuite; | ||
| import org.apache.pulsar.tests.integration.topologies.PulsarCluster; | ||
| import org.awaitility.Awaitility; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.apache.pulsar.tests.integration.functions.utils.CommandGenerator.JAVAJAR; | ||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
| import static org.testng.Assert.fail; | ||
|
|
||
| /** | ||
| * Test behaviour of simple sinks | ||
| */ | ||
| @Slf4j | ||
| public class PulsarKeyValueSinkTest extends PulsarStandaloneTestSuite { | ||
|
|
||
| @Test(groups = {"sink"}) | ||
| public void testSourceProperty() throws Exception { | ||
| String outputTopicName = "test-kv-sink-input-" + randomName(8); | ||
| String sinkName = "test-kv-sink-" + randomName(8); | ||
| submitSinkConnector(sinkName, outputTopicName, "org.apache.pulsar.tests.integration.io.TestKeyValueSink", JAVAJAR); | ||
|
|
||
| // get sink info | ||
| getSinkInfoSuccess(sinkName); | ||
|
|
||
| // get source status | ||
| getSinkStatus(sinkName); | ||
|
|
||
| @Cleanup PulsarClient client = PulsarClient.builder() | ||
| .serviceUrl(container.getPlainTextServiceUrl()) | ||
| .build(); | ||
| @Cleanup Producer<KeyValue<String, Integer>> producer = client.newProducer(Schema.KeyValue(String.class, Integer.class)) | ||
| .topic(outputTopicName) | ||
| .create(); | ||
|
|
||
| producer.send(new KeyValue<>("foo", 1234)); | ||
| producer.send(new KeyValue<>("foo", 1234)); | ||
| producer.send(new KeyValue<>("foo", 1234)); | ||
| producer.send(new KeyValue<>("foo", 1234)); | ||
|
|
||
| try (PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(container.getHttpServiceUrl()).build()) { | ||
|
|
||
| Awaitility.await().ignoreExceptions().untilAsserted(() -> { | ||
| SinkStatus status = admin.sinks().getSinkStatus("public", "default", sinkName); | ||
| assertEquals(status.getInstances().size(), 1); | ||
| assertTrue(status.getInstances().get(0).getStatus().numReadFromPulsar > 0); | ||
| assertTrue(status.getInstances().get(0).getStatus().numSinkExceptions == 0); | ||
| assertTrue(status.getInstances().get(0).getStatus().numSystemExceptions == 0); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| // delete source | ||
| deleteSink(sinkName); | ||
|
|
||
| getSinkInfoNotFound(sinkName); | ||
| } | ||
|
|
||
| private void submitSinkConnector(String sinkName, | ||
| String inputTopicName, | ||
| String className, | ||
| String archive) throws Exception { | ||
| String[] commands = { | ||
| PulsarCluster.ADMIN_SCRIPT, | ||
| "sinks", "create", | ||
| "--name", sinkName, | ||
| "-i", inputTopicName, | ||
| "--archive", archive, | ||
| "--classname", className | ||
| }; | ||
| log.info("Run command : {}", StringUtils.join(commands, ' ')); | ||
| ContainerExecResult result = container.execCmd(commands); | ||
| assertTrue( | ||
| result.getStdout().contains("\"Created successfully\""), | ||
| result.getStdout()); | ||
| } | ||
|
|
||
| private void getSinkInfoSuccess(String sinkName) throws Exception { | ||
| ContainerExecResult result = container.execCmd( | ||
| PulsarCluster.ADMIN_SCRIPT, | ||
| "sinks", | ||
| "get", | ||
| "--tenant", "public", | ||
| "--namespace", "default", | ||
| "--name", sinkName | ||
| ); | ||
| assertTrue(result.getStdout().contains("\"name\": \"" + sinkName + "\"")); | ||
| } | ||
|
|
||
| private void getSinkStatus(String sinkName) throws Exception { | ||
| ContainerExecResult result = container.execCmd( | ||
| PulsarCluster.ADMIN_SCRIPT, | ||
| "sinks", | ||
| "status", | ||
| "--tenant", "public", | ||
| "--namespace", "default", | ||
| "--name", sinkName | ||
| ); | ||
| assertTrue(result.getStdout().contains("\"running\" : true")); | ||
| } | ||
|
|
||
| private void deleteSink(String sinkName) throws Exception { | ||
| ContainerExecResult result = container.execCmd( | ||
| PulsarCluster.ADMIN_SCRIPT, | ||
| "sinks", | ||
| "delete", | ||
| "--tenant", "public", | ||
| "--namespace", "default", | ||
| "--name", sinkName | ||
| ); | ||
| assertTrue(result.getStdout().contains("successfully")); | ||
| result.assertNoStderr(); | ||
| } | ||
|
|
||
| private void getSinkInfoNotFound(String sinkName) throws Exception { | ||
| try { | ||
| container.execCmd( | ||
| PulsarCluster.ADMIN_SCRIPT, | ||
| "sinks", | ||
| "get", | ||
| "--tenant", "public", | ||
| "--namespace", "default", | ||
| "--name", sinkName); | ||
| fail("Command should have exited with non-zero"); | ||
| } catch (ContainerExecException e) { | ||
| assertTrue(e.getResult().getStderr().contains(sinkName + " doesn't exist")); | ||
| } | ||
| } | ||
| } | ||
|
|
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
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.
I don't understand why do you need this change here. What is the value for adding a SinkKVRecord? SinkRecord is a wrapper of the output object. The output object can be a KeyValue object.
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.
@sijie
these are the key points:
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.
@sijie the alternative is:
but this alternative is not good because: