You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/09/26 09:48:24 UTC

[GitHub] [incubator-seatunnel] EricJoy2048 commented on a diff in pull request #2777: [Feature][Connector-v2] Neo4j source connector

EricJoy2048 commented on code in PR #2777:
URL: https://github.com/apache/incubator-seatunnel/pull/2777#discussion_r979770162


##########
seatunnel-connectors-v2/connector-neo4j/src/main/java/org/apache/seatunnel/connectors/seatunnel/neo4j/source/Neo4jSource.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.neo4j.source;
+
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_BEARER_TOKEN;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_DATABASE;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_KERBEROS_TICKET;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_MAX_CONNECTION_TIMEOUT;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_MAX_TRANSACTION_RETRY_TIME;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_NEO4J_URI;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_PASSWORD;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_QUERY;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.KEY_USERNAME;
+import static org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig.PLUGIN_NAME;
+
+import org.apache.seatunnel.api.common.PrepareFailException;
+import org.apache.seatunnel.api.source.Boundedness;
+import org.apache.seatunnel.api.source.SeaTunnelSource;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.config.CheckConfigUtil;
+import org.apache.seatunnel.common.config.CheckResult;
+import org.apache.seatunnel.common.constants.PluginType;
+import org.apache.seatunnel.connectors.seatunnel.common.schema.SeaTunnelSchema;
+import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitReader;
+import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitSource;
+import org.apache.seatunnel.connectors.seatunnel.common.source.SingleSplitReaderContext;
+import org.apache.seatunnel.connectors.seatunnel.neo4j.config.DriverBuilder;
+import org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.google.auto.service.AutoService;
+import org.neo4j.driver.AuthTokens;
+
+import java.net.URI;
+
+@AutoService(SeaTunnelSource.class)
+public class Neo4jSource extends AbstractSingleSplitSource<SeaTunnelRow> {
+
+    private final Neo4jSourceConfig neo4jSourceConfig = new Neo4jSourceConfig();
+    private SeaTunnelRowType rowType;
+
+    @Override
+    public String getPluginName() {
+        return PLUGIN_NAME;
+    }
+
+    @Override
+    public void prepare(Config pluginConfig) throws PrepareFailException {
+        neo4jSourceConfig.setDriverBuilder(prepareDriver(pluginConfig));
+
+        final CheckResult queryConfigCheck = CheckConfigUtil.checkAllExists(pluginConfig, KEY_QUERY);
+        if (!queryConfigCheck.isSuccess()) {
+            throw new PrepareFailException(Neo4jSourceConfig.PLUGIN_NAME, PluginType.SOURCE, queryConfigCheck.getMsg());
+        }
+        neo4jSourceConfig.setQuery(pluginConfig.getString(KEY_QUERY));
+
+        final CheckResult schemaConfigCheck = CheckConfigUtil.checkAllExists(pluginConfig, SeaTunnelSchema.SCHEMA);

Review Comment:
   Why not use `CheckConfigUtil.checkAllExists(pluginConfig, KEY_QUERY, SeaTunnelSchema.SCHEMA)`



##########
seatunnel-e2e/seatunnel-flink-connector-v2-e2e/connector-neo4j-flink-e2e/src/test/java/org/apache/seatunnel/e2e/flink/v2/neo4j/Neo4jIT.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.seatunnel.e2e.flink.v2.neo4j;
+
+import org.apache.seatunnel.e2e.flink.FlinkContainer;
+
+import com.google.common.collect.Lists;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.neo4j.driver.AuthTokens;
+import org.neo4j.driver.Driver;
+import org.neo4j.driver.GraphDatabase;
+import org.neo4j.driver.Result;
+import org.neo4j.driver.Session;
+import org.neo4j.driver.SessionConfig;
+import org.testcontainers.containers.Container;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.lifecycle.Startables;
+import org.testcontainers.shaded.org.awaitility.Awaitility;
+import org.testcontainers.utility.DockerImageName;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+@Slf4j
+public class Neo4jIT extends FlinkContainer {
+
+    private static final String CONTAINER_IMAGE = "neo4j:latest";
+    private static final String CONTAINER_HOST = "neo4j-host";
+    private static final int CONTAINER_PORT = 7687;
+    private static final String CONTAINER_NEO4J_USERNAME = "neo4j";
+    private static final String CONTAINER_NEO4J_PASSWORD = "1234";
+    private static final URI CONTAINER_URI = URI.create("neo4j://localhost:" + CONTAINER_PORT);
+
+    private GenericContainer<?> container;
+    private Driver neo4jDriver;
+    private Session neo4jSession;
+
+    @BeforeAll
+    public void init() {
+        DockerImageName imageName = DockerImageName.parse(CONTAINER_IMAGE);
+        container = new GenericContainer<>(imageName)
+            .withNetwork(NETWORK)
+            .withNetworkAliases(CONTAINER_HOST)
+            .withExposedPorts(CONTAINER_PORT)
+            .withEnv("NEO4J_AUTH", CONTAINER_NEO4J_USERNAME + "/" + CONTAINER_NEO4J_PASSWORD)
+            .withLogConsumer(new Slf4jLogConsumer(log));
+        container.setPortBindings(Lists.newArrayList(String.format("%s:%s", CONTAINER_PORT, CONTAINER_PORT)));
+        Startables.deepStart(Stream.of(container)).join();
+        log.info("container started");
+        Awaitility.given().ignoreExceptions()
+            .await()
+            .atMost(30, TimeUnit.SECONDS)
+            .untilAsserted(this::initConnection);
+
+    }
+
+    private void initConnection() {
+        neo4jDriver = GraphDatabase.driver(CONTAINER_URI, AuthTokens.basic(CONTAINER_NEO4J_USERNAME, CONTAINER_NEO4J_PASSWORD));
+        neo4jSession = neo4jDriver.session(SessionConfig.forDatabase("neo4j"));
+    }
+
+    @Test
+    public void testSink() throws IOException, InterruptedException {
+        // when
+        Container.ExecResult execResult = executeSeaTunnelFlinkJob("/neo4j/fake_to_neo4j.conf");
+
+        // then
+        Assertions.assertEquals(0, execResult.getExitCode());
+
+        final Result result = neo4jSession.run("MATCH (a:Person) RETURN a.name, a.age");

Review Comment:
   Please test all of the datatype.



##########
seatunnel-connectors-v2/connector-neo4j/src/main/java/org/apache/seatunnel/connectors/seatunnel/neo4j/source/Neo4jSourceReader.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.neo4j.source;
+
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.MapType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitReader;
+import org.apache.seatunnel.connectors.seatunnel.common.source.SingleSplitReaderContext;
+import org.apache.seatunnel.connectors.seatunnel.neo4j.config.Neo4jSourceConfig;
+
+import org.neo4j.driver.Driver;
+import org.neo4j.driver.Query;
+import org.neo4j.driver.Result;
+import org.neo4j.driver.Session;
+import org.neo4j.driver.SessionConfig;
+import org.neo4j.driver.Value;
+
+import java.io.IOException;
+import java.util.Objects;
+
+public class Neo4jSourceReader extends AbstractSingleSplitReader<SeaTunnelRow> {
+
+    private final SingleSplitReaderContext context;
+    private final Neo4jSourceConfig config;
+    private final SeaTunnelRowType rowType;
+    private final Driver driver;
+    private Session session;
+
+    public Neo4jSourceReader(SingleSplitReaderContext context, Neo4jSourceConfig config, SeaTunnelRowType rowType) {
+        this.context = context;
+        this.config = config;
+        this.driver = config.getDriverBuilder().build();
+        this.rowType = rowType;
+    }
+
+    @Override
+    public void open() throws Exception {
+        this.session = driver.session(SessionConfig.forDatabase(config.getDriverBuilder().getDatabase()));
+    }
+
+    @Override
+    public void close() throws IOException {
+        session.close();
+        driver.close();
+    }
+
+    @Override
+    public void pollNext(Collector<SeaTunnelRow> output) throws Exception {
+        final Query query = new Query(config.getQuery());
+        session.readTransaction(tx -> {
+            final Result result = tx.run(query);
+            result.stream()
+                .forEach(row -> {
+                    final Object[] fields = new Object[rowType.getTotalFields()];
+                    for (int i = 0; i < rowType.getTotalFields(); i++) {
+                        final String fieldName = rowType.getFieldName(i);
+                        final SeaTunnelDataType<?> fieldType = rowType.getFieldType(i);
+                        final Value value = row.get(fieldName);
+                        fields[i] = convertType(fieldType, value);
+                    }
+                    output.collect(new SeaTunnelRow(fields));
+                });
+            return null;
+        });
+        this.context.signalNoMoreElement();
+    }
+
+    public static Object convertType(SeaTunnelDataType<?> dataType, Value value) {
+        Objects.requireNonNull(dataType);
+        Objects.requireNonNull(value);
+
+        switch (dataType.getSqlType()) {
+            case STRING:

Review Comment:
   Can you case `Neo4j.List` to `SqlType.ARRAY`?



##########
seatunnel-e2e/seatunnel-spark-connector-v2-e2e/connector-neo4j-spark-e2e/src/test/java/org/apache/seatunnel/e2e/spark/v2/neo4j/Neo4jIT.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.seatunnel.e2e.spark.v2.neo4j;
+
+import org.apache.seatunnel.e2e.spark.SparkContainer;
+
+import com.google.common.collect.Lists;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.neo4j.driver.AuthTokens;
+import org.neo4j.driver.Driver;
+import org.neo4j.driver.GraphDatabase;
+import org.neo4j.driver.Result;
+import org.neo4j.driver.Session;
+import org.neo4j.driver.SessionConfig;
+import org.testcontainers.containers.Container;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.lifecycle.Startables;
+import org.testcontainers.shaded.org.awaitility.Awaitility;
+import org.testcontainers.utility.DockerImageName;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+@Slf4j
+public class Neo4jIT extends SparkContainer {
+
+    private static final String CONTAINER_IMAGE = "neo4j:latest";
+    private static final String CONTAINER_HOST = "neo4j-host";
+    private static final int CONTAINER_PORT = 7687;
+    private static final String CONTAINER_NEO4J_USERNAME = "neo4j";
+    private static final String CONTAINER_NEO4J_PASSWORD = "1234";
+    private static final URI CONTAINER_URI = URI.create("neo4j://localhost:" + CONTAINER_PORT);
+
+    private GenericContainer<?> container;
+    private Driver neo4jDriver;
+    private Session neo4jSession;
+
+    @BeforeAll
+    public void init() {
+        DockerImageName imageName = DockerImageName.parse(CONTAINER_IMAGE);
+        container = new GenericContainer<>(imageName)
+            .withExposedPorts(CONTAINER_PORT)
+            .withNetwork(NETWORK)
+            .withNetworkAliases(CONTAINER_HOST)
+            .withEnv("NEO4J_AUTH", CONTAINER_NEO4J_USERNAME + "/" + CONTAINER_NEO4J_PASSWORD)
+            .withLogConsumer(new Slf4jLogConsumer(log));
+        container.setPortBindings(Lists.newArrayList(String.format("%s:%s", CONTAINER_PORT, CONTAINER_PORT)));
+        Startables.deepStart(Stream.of(container)).join();
+        log.info("container started");
+        Awaitility.given().ignoreExceptions()
+            .await()
+            .atMost(30, TimeUnit.SECONDS)
+            .untilAsserted(this::initConnection);
+
+    }
+
+    private void initConnection() {
+        neo4jDriver = GraphDatabase.driver(CONTAINER_URI, AuthTokens.basic(CONTAINER_NEO4J_USERNAME, CONTAINER_NEO4J_PASSWORD));
+        neo4jSession = neo4jDriver.session(SessionConfig.forDatabase("neo4j"));
+    }
+
+    @Test
+    public void testSink() throws IOException, InterruptedException {
+        // when
+        Container.ExecResult execResult = executeSeaTunnelSparkJob("/neo4j/fake_to_neo4j.conf");
+
+        // then
+        Assertions.assertEquals(0, execResult.getExitCode());

Review Comment:
   Same as the flink e2e.



##########
seatunnel-e2e/seatunnel-flink-connector-v2-e2e/connector-neo4j-flink-e2e/src/test/java/org/apache/seatunnel/e2e/flink/v2/neo4j/Neo4jIT.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.seatunnel.e2e.flink.v2.neo4j;
+
+import org.apache.seatunnel.e2e.flink.FlinkContainer;
+
+import com.google.common.collect.Lists;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.neo4j.driver.AuthTokens;
+import org.neo4j.driver.Driver;
+import org.neo4j.driver.GraphDatabase;
+import org.neo4j.driver.Result;
+import org.neo4j.driver.Session;
+import org.neo4j.driver.SessionConfig;
+import org.testcontainers.containers.Container;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.lifecycle.Startables;
+import org.testcontainers.shaded.org.awaitility.Awaitility;
+import org.testcontainers.utility.DockerImageName;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+@Slf4j
+public class Neo4jIT extends FlinkContainer {
+
+    private static final String CONTAINER_IMAGE = "neo4j:latest";
+    private static final String CONTAINER_HOST = "neo4j-host";
+    private static final int CONTAINER_PORT = 7687;
+    private static final String CONTAINER_NEO4J_USERNAME = "neo4j";
+    private static final String CONTAINER_NEO4J_PASSWORD = "1234";
+    private static final URI CONTAINER_URI = URI.create("neo4j://localhost:" + CONTAINER_PORT);
+
+    private GenericContainer<?> container;
+    private Driver neo4jDriver;
+    private Session neo4jSession;
+
+    @BeforeAll
+    public void init() {
+        DockerImageName imageName = DockerImageName.parse(CONTAINER_IMAGE);
+        container = new GenericContainer<>(imageName)
+            .withNetwork(NETWORK)
+            .withNetworkAliases(CONTAINER_HOST)
+            .withExposedPorts(CONTAINER_PORT)
+            .withEnv("NEO4J_AUTH", CONTAINER_NEO4J_USERNAME + "/" + CONTAINER_NEO4J_PASSWORD)
+            .withLogConsumer(new Slf4jLogConsumer(log));
+        container.setPortBindings(Lists.newArrayList(String.format("%s:%s", CONTAINER_PORT, CONTAINER_PORT)));
+        Startables.deepStart(Stream.of(container)).join();
+        log.info("container started");
+        Awaitility.given().ignoreExceptions()
+            .await()
+            .atMost(30, TimeUnit.SECONDS)
+            .untilAsserted(this::initConnection);
+
+    }
+
+    private void initConnection() {
+        neo4jDriver = GraphDatabase.driver(CONTAINER_URI, AuthTokens.basic(CONTAINER_NEO4J_USERNAME, CONTAINER_NEO4J_PASSWORD));
+        neo4jSession = neo4jDriver.session(SessionConfig.forDatabase("neo4j"));
+    }
+
+    @Test
+    public void testSink() throws IOException, InterruptedException {
+        // when
+        Container.ExecResult execResult = executeSeaTunnelFlinkJob("/neo4j/fake_to_neo4j.conf");
+
+        // then
+        Assertions.assertEquals(0, execResult.getExitCode());
+
+        final Result result = neo4jSession.run("MATCH (a:Person) RETURN a.name, a.age");
+        Assertions.assertTrue(result.stream().findAny().isPresent());
+        Assertions.assertTrue(result.stream().anyMatch(record -> record.get("a.age").asInt() > 0));
+
+    }
+
+    @Test
+    public void testSource() throws IOException, InterruptedException {
+        // given
+        neo4jSession.run("CREATE (a:Person {name: 'foo', age: 10})");

Review Comment:
   Same as above.



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org