-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kafka client Java API wrapper (#761)
- Loading branch information
Showing
19 changed files
with
1,672 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.apache.pulsar</groupId> | ||
<artifactId>pulsar</artifactId> | ||
<version>1.20.0-incubating-SNAPSHOT</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<artifactId>pulsar-client-kafka-compat</artifactId> | ||
<name>Pulsar Kafka compatibility</name> | ||
|
||
<packaging>pom</packaging> | ||
|
||
<modules> | ||
<module>pulsar-client-kafka</module> | ||
<module>pulsar-client-kafka-tests</module> | ||
</modules> | ||
</project> |
70 changes: 70 additions & 0 deletions
70
pulsar-client-kafka-compat/pulsar-client-kafka-tests/pom.xml
This file contains 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,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.apache.pulsar</groupId> | ||
<artifactId>pulsar-client-kafka-compat</artifactId> | ||
<version>1.20.0-incubating-SNAPSHOT</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<artifactId>pulsar-client-kafka-tests</artifactId> | ||
<name>Pulsar Kafka compatibility :: Tests</name> | ||
|
||
<description>Tests to verify the correct shading configuration for the pulsar-client-kafka wrapper</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>pulsar-client-kafka</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>pulsar-broker</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>pulsar-broker</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
<type>test-jar</type> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>managed-ledger</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
<type>test-jar</type> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
58 changes: 58 additions & 0 deletions
58
...a-tests/src/test/java/org/apache/pulsar/client/kafka/compat/examples/ConsumerExample.java
This file contains 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,58 @@ | ||
/** | ||
* 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.client.kafka.compat.examples; | ||
|
||
import java.util.Arrays; | ||
import java.util.Properties; | ||
|
||
import org.apache.kafka.clients.consumer.Consumer; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.serialization.IntegerDeserializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ConsumerExample { | ||
public static void main(String[] args) { | ||
String topic = "persistent://sample/standalone/ns/my-topic"; | ||
|
||
Properties props = new Properties(); | ||
props.put("bootstrap.servers", "pulsar://localhost:6650"); | ||
props.put("group.id", "my-subscription-name"); | ||
props.put("enable.auto.commit", "false"); | ||
props.put("key.deserializer", IntegerDeserializer.class.getName()); | ||
props.put("value.deserializer", StringDeserializer.class.getName()); | ||
|
||
Consumer<Integer, String> consumer = new KafkaConsumer<>(props); | ||
consumer.subscribe(Arrays.asList(topic)); | ||
|
||
while (true) { | ||
ConsumerRecords<Integer, String> records = consumer.poll(100); | ||
records.forEach(record -> { | ||
log.info("Received record: {}", record); | ||
}); | ||
|
||
// Commit last offset | ||
consumer.commitSync(); | ||
} | ||
} | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ConsumerExample.class); | ||
} |
52 changes: 52 additions & 0 deletions
52
...a-tests/src/test/java/org/apache/pulsar/client/kafka/compat/examples/ProducerExample.java
This file contains 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,52 @@ | ||
/** | ||
* 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.client.kafka.compat.examples; | ||
|
||
import java.util.Properties; | ||
|
||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.Producer; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.serialization.IntegerSerializer; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ProducerExample { | ||
public static void main(String[] args) { | ||
String topic = "persistent://sample/standalone/ns/my-topic"; | ||
|
||
Properties props = new Properties(); | ||
props.put("bootstrap.servers", "pulsar://localhost:6650"); | ||
|
||
props.put("key.serializer", IntegerSerializer.class.getName()); | ||
props.put("value.serializer", StringSerializer.class.getName()); | ||
|
||
Producer<Integer, String> producer = new KafkaProducer<>(props); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
producer.send(new ProducerRecord<Integer, String>(topic, i, Integer.toString(i))); | ||
log.info("Message {} sent successfully", i); | ||
} | ||
|
||
producer.close(); | ||
} | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ProducerExample.class); | ||
} |
106 changes: 106 additions & 0 deletions
106
...t-kafka-tests/src/test/java/org/apache/pulsar/client/kafka/compat/tests/KafkaApiTest.java
This file contains 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,106 @@ | ||
/** | ||
* 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.client.kafka.compat.tests; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.apache.kafka.clients.consumer.Consumer; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.Producer; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
import org.apache.kafka.common.serialization.IntegerDeserializer; | ||
import org.apache.kafka.common.serialization.IntegerSerializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.apache.pulsar.broker.service.BrokerTestBase; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
public class KafkaApiTest extends BrokerTestBase { | ||
@BeforeClass | ||
@Override | ||
protected void setup() throws Exception { | ||
super.baseSetup(); | ||
} | ||
|
||
@AfterClass | ||
@Override | ||
protected void cleanup() throws Exception { | ||
super.internalCleanup(); | ||
} | ||
|
||
@Test(timeOut = 30000) | ||
public void testSimpleProducerConsumer() throws Exception { | ||
String topic = "persistent://sample/standalone/ns/testSimpleProducerConsumer"; | ||
|
||
Properties producerProperties = new Properties(); | ||
producerProperties.put("bootstrap.servers", brokerUrl.toString()); | ||
producerProperties.put("key.serializer", IntegerSerializer.class.getName()); | ||
producerProperties.put("value.serializer", StringSerializer.class.getName()); | ||
Producer<Integer, String> producer = new KafkaProducer<>(producerProperties); | ||
|
||
Properties consumerProperties = new Properties(); | ||
consumerProperties.put("bootstrap.servers", brokerUrl.toString()); | ||
consumerProperties.put("group.id", "my-subscription-name"); | ||
consumerProperties.put("key.deserializer", IntegerDeserializer.class.getName()); | ||
consumerProperties.put("value.deserializer", StringDeserializer.class.getName()); | ||
consumerProperties.put("enable.auto.commit", "true"); | ||
Consumer<Integer, String> consumer = new KafkaConsumer<>(consumerProperties); | ||
consumer.subscribe(Arrays.asList(topic)); | ||
|
||
List<Long> offsets = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
RecordMetadata md = producer.send(new ProducerRecord<Integer, String>(topic, i, "hello-" + i)).get(); | ||
offsets.add(md.offset()); | ||
log.info("Published message at {}", Long.toHexString(md.offset())); | ||
} | ||
|
||
producer.flush(); | ||
producer.close(); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
ConsumerRecords<Integer, String> records = consumer.poll(1000); | ||
assertEquals(records.count(), 1); | ||
|
||
int idx = i; | ||
records.forEach(record -> { | ||
log.info("Received record: {}", record); | ||
assertEquals(record.key().intValue(), idx); | ||
assertEquals(record.value(), "hello-" + idx); | ||
assertEquals(record.offset(), offsets.get(idx).longValue()); | ||
}); | ||
} | ||
|
||
consumer.close(); | ||
} | ||
|
||
private static final Logger log = LoggerFactory.getLogger(KafkaApiTest.class); | ||
} |
Oops, something went wrong.