eolivelli commented on code in PR #16179:
URL: https://github.com/apache/pulsar/pull/16179#discussion_r993261206


##########
pulsar-io/cassandra/src/main/java/org/apache/pulsar/io/cassandra/util/CassandraConnector.java:
##########
@@ -0,0 +1,135 @@
+/**
+ * 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.io.cassandra.util;
+
+import com.datastax.driver.core.*;
+import com.datastax.driver.core.Session;
+import org.apache.pulsar.io.cassandra.CassandraSinkConfig;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CassandraConnector {
+
+    private Cluster cluster;
+    private Session session;

Review Comment:
   we have to handle concurrent access properly to these fields



##########
pulsar-io/cassandra/pom.xml:
##########
@@ -51,6 +51,39 @@
     <dependency>
       <groupId>com.datastax.cassandra</groupId>
       <artifactId>cassandra-driver-core</artifactId>
+      <version>${cassandra.version}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>io.dropwizard.metrics</groupId>
+          <artifactId>metrics-core</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
+      <groupId>commons-beanutils</groupId>
+      <artifactId>commons-beanutils</artifactId>
+      <version>1.9.4</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.pulsar</groupId>
+      <artifactId>pulsar-functions-local-runner-original</artifactId>
+      <version>2.11.0-SNAPSHOT</version>

Review Comment:
   please use project.version



##########
pulsar-io/cassandra/src/main/java/org/apache/pulsar/io/cassandra/util/TableMetadataProvider.java:
##########
@@ -0,0 +1,102 @@
+/**
+ * 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.io.cassandra.util;
+
+import com.datastax.driver.core.*;

Review Comment:
   please do not use star imports



##########
pulsar-io/cassandra/src/main/java/org/apache/pulsar/io/cassandra/util/StringRecordWrapper.java:
##########
@@ -0,0 +1,47 @@
+/**
+ * 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.io.cassandra.util;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class StringRecordWrapper extends RecordWrapper<String> {
+
+    private Map<String, Object> valuesMap;
+
+    public StringRecordWrapper(String jsonString) {
+        super(jsonString);
+        valuesMap = new Gson().fromJson(jsonString,

Review Comment:
   I think that we can cache this "new Gson()" instance in a static field



##########
pulsar-io/cassandra/src/test/java/org/apache/pulsar/io/cassandra/producers/InputTopicProducerThread.java:
##########
@@ -0,0 +1,83 @@
+/**
+ * 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.io.cassandra.producers;
+
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.api.Schema;
+
+import java.util.Random;
+
+public abstract class InputTopicProducerThread<T> implements Runnable {
+
+    private Random rnd = new Random();
+
+    final String inputTopic;
+    final String brokerUrl;
+    PulsarClient client;
+    Producer<T> producer;
+
+    public InputTopicProducerThread(String brokerUrl, String inputTopic) {
+        this.brokerUrl = brokerUrl;
+        this.inputTopic = inputTopic;
+    }
+
+    @Override
+    public void run() {
+        for (int idx = 0; idx < 100; idx++) {
+            try {
+                
getProducer().newMessage().key(getKey()).value(getValue()).send();
+            } catch (PulsarClientException e) {
+                e.printStackTrace();

Review Comment:
   please use logger



##########
pulsar-io/cassandra/src/test/java/org/apache/pulsar/io/cassandra/CassandraSinkExec.java:
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.io.cassandra;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.pulsar.common.io.SinkConfig;
+import org.apache.pulsar.functions.LocalRunner;
+import org.apache.pulsar.io.cassandra.producers.InputTopicProducerThread;
+import org.apache.pulsar.io.cassandra.producers.InputTopicStringProducer;
+import org.apache.pulsar.io.cassandra.producers.ReadingSchemaRecordProducer;
+import org.yaml.snakeyaml.Yaml;
+
+/**
+ * Useful for testing within IDE.
+ *
+ */
+public class CassandraSinkExec {
+
+    public static final String BROKER_URL = "pulsar://localhost:6650";
+    public static final String INPUT_TOPIC = 
"persistent://public/default/air-quality-reading-generic";
+    // public static final String INPUT_TOPIC = 
"persistent://public/default/air-quality-reading-string";
+    public static final String CONFIG_FILE = "cassandra-sink-config.yaml";
+
+    public static void main(String[] args) throws Exception {
+
+        SinkConfig config = getSinkConfig();
+
+        final LocalRunner localRunner =
+                LocalRunner.builder()
+                        .brokerServiceUrl(BROKER_URL)
+                        .sinkConfig(config)
+                        .build();
+
+        localRunner.start(false);
+
+        sendData();
+        TimeUnit.MINUTES.sleep(10);
+
+        localRunner.stop();
+
+        System.exit(0);
+    }
+
+    private static SinkConfig getSinkConfig() throws FileNotFoundException {
+        SinkConfig sinkConfig = SinkConfig.builder()
+                .autoAck(true)
+                .cleanupSubscription(Boolean.TRUE)
+                .configs(getConfigs())
+                .className(CassandraGenericRecordSink.class.getName())
+                .inputs(Collections.singletonList(INPUT_TOPIC))
+                .name("CassandraSink")
+                .build();
+
+        return sinkConfig;
+    }
+
+    private static Map<String, Object> getConfigs() throws 
FileNotFoundException {
+        Map<String, Object> configs = new HashMap<String, Object>();
+
+        ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+        File file = new File(classLoader.getResource(CONFIG_FILE).getFile());
+
+        Yaml yaml = new Yaml();
+        configs = yaml.load(new FileInputStream(file));
+
+        return configs;
+    }
+
+    private static final void sendData() throws InterruptedException {
+        TimeUnit.SECONDS.sleep(10);

Review Comment:
   do we need to sleep a fixed amount of time ?



##########
pulsar-io/cassandra/src/test/java/org/apache/pulsar/io/cassandra/CassandraSinkExec.java:
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.io.cassandra;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.pulsar.common.io.SinkConfig;
+import org.apache.pulsar.functions.LocalRunner;
+import org.apache.pulsar.io.cassandra.producers.InputTopicProducerThread;
+import org.apache.pulsar.io.cassandra.producers.InputTopicStringProducer;
+import org.apache.pulsar.io.cassandra.producers.ReadingSchemaRecordProducer;
+import org.yaml.snakeyaml.Yaml;
+
+/**
+ * Useful for testing within IDE.
+ *
+ */
+public class CassandraSinkExec {
+
+    public static final String BROKER_URL = "pulsar://localhost:6650";
+    public static final String INPUT_TOPIC = 
"persistent://public/default/air-quality-reading-generic";
+    // public static final String INPUT_TOPIC = 
"persistent://public/default/air-quality-reading-string";
+    public static final String CONFIG_FILE = "cassandra-sink-config.yaml";
+
+    public static void main(String[] args) throws Exception {
+
+        SinkConfig config = getSinkConfig();
+
+        final LocalRunner localRunner =
+                LocalRunner.builder()
+                        .brokerServiceUrl(BROKER_URL)
+                        .sinkConfig(config)
+                        .build();
+
+        localRunner.start(false);
+
+        sendData();
+        TimeUnit.MINUTES.sleep(10);
+
+        localRunner.stop();
+
+        System.exit(0);
+    }
+
+    private static SinkConfig getSinkConfig() throws FileNotFoundException {
+        SinkConfig sinkConfig = SinkConfig.builder()
+                .autoAck(true)
+                .cleanupSubscription(Boolean.TRUE)
+                .configs(getConfigs())
+                .className(CassandraGenericRecordSink.class.getName())
+                .inputs(Collections.singletonList(INPUT_TOPIC))
+                .name("CassandraSink")
+                .build();
+
+        return sinkConfig;
+    }
+
+    private static Map<String, Object> getConfigs() throws 
FileNotFoundException {
+        Map<String, Object> configs = new HashMap<String, Object>();
+
+        ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+        File file = new File(classLoader.getResource(CONFIG_FILE).getFile());
+
+        Yaml yaml = new Yaml();
+        configs = yaml.load(new FileInputStream(file));

Review Comment:
   please close the stream



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to