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/19 05:56:44 UTC

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

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


##########
seatunnel-connectors-v2/connector-neo4j/src/main/java/org/apache/seatunnel/connectors/seatunnel/neo4j/config/Neo4jSourceConfig.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.config;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.Serializable;
+
+@Getter
+@Setter
+public class Neo4jSourceConfig implements Serializable {
+
+    public static final String PLUGIN_NAME = "Neo4j";
+
+    public static final String KEY_NEO4J_URI = "uri";
+    public static final String KEY_USERNAME = "username";
+    public static final String KEY_PASSWORD = "password";
+    public static final String KEY_BEARER_TOKEN = "bearer_token";
+    public static final String KEY_KERBEROS_TICKET = "kerberos_ticket"; // Base64 encoded
+
+    public static final String KEY_DATABASE = "database";
+    public static final String KEY_QUERY = "query";
+    public static final String KEY_MAX_TRANSACTION_RETRY_TIME = "max_transaction_retry_time";
+    public static final String KEY_MAX_CONNECTION_TIMEOUT = "max_connection_timeout";

Review Comment:
   Extract common configuration to `Neo4jCommonConfig`?



##########
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.LocalTimeType;
+import org.apache.seatunnel.api.table.type.MapType;
+import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType;
+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);
+
+        if (dataType.equals(BasicType.STRING_TYPE)) {
+            return value.asString();
+        } else if (dataType.equals(BasicType.BOOLEAN_TYPE)) {
+            return value.asBoolean();
+        } else if (dataType.equals(BasicType.LONG_TYPE)) {
+            return value.asLong();
+        } else if (dataType.equals(BasicType.DOUBLE_TYPE)) {
+            return value.asDouble();
+        } else if (dataType.equals(BasicType.VOID_TYPE)) {
+            return null;
+        } else if (dataType.equals(PrimitiveByteArrayType.INSTANCE)) {
+            return value.asByteArray();
+        } else if (dataType.equals(LocalTimeType.LOCAL_DATE_TYPE)) {
+            return value.asLocalDate();
+        } else if (dataType.equals(LocalTimeType.LOCAL_TIME_TYPE)) {
+            return value.asLocalTime();
+        } else if (dataType.equals(LocalTimeType.LOCAL_DATE_TIME_TYPE)) {
+            return value.asLocalDateTime();
+        } else if (dataType instanceof MapType) {
+            if (!((MapType<?, ?>) dataType).getKeyType().equals(BasicType.STRING_TYPE)) {
+                throw new IllegalArgumentException("Key Type of MapType must String type");
+            }
+            return value.asMap();
+        } else if (dataType.equals(BasicType.INT_TYPE)) {
+            return value.asInt();
+        } else if (dataType.equals(BasicType.FLOAT_TYPE)) {
+            return value.asFloat();
+        } else {
+            throw new IllegalArgumentException("not supported data type: " + dataType);
+        }

Review Comment:
   Use `switch(dataType.getSqlType())` ?



-- 
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