You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by GitBox <gi...@apache.org> on 2022/06/14 06:38:44 UTC

[GitHub] [incubator-hugegraph-toolchain] imbajin commented on a diff in pull request #291: feat: Introduce HugeGraphFlinkCDCLoader

imbajin commented on code in PR #291:
URL: https://github.com/apache/incubator-hugegraph-toolchain/pull/291#discussion_r896416882


##########
hugegraph-loader/assembly/static/bin/get-params.sh:
##########
@@ -0,0 +1,24 @@
+#!/bin/bash
+function get_params() {
+  echo "params: $*"
+  engine_params=""
+  hugegraph_params=""
+  while (("$#")); do
+    case "$1" in
+    -–file | --graph | --schema | --host | --port | --username | --token | --protocol | \
+      --trust-store-file | --trust-store-password | --clear-all-data | --clear-timeout | \

Review Comment:
   shall line8 align with line9?



##########
hugegraph-loader/src/main/java/com/baidu/hugegraph/loader/spark/HugeGraphSparkLoader.java:
##########
@@ -209,7 +208,7 @@ private void parse(Row row,
         graphElements.addAll(elements);
     }
 
-    private void sink(Map.Entry<ElementBuilder, List<GraphElement>> builderMap,
+    private void flush(Map.Entry<ElementBuilder, List<GraphElement>> builderMap,
                       GraphManager g, boolean isCheckVertex) {

Review Comment:
   `GraphManger` aline with `MapX`?



##########
hugegraph-loader/src/main/java/com/baidu/hugegraph/loader/flink/HugeGraphFlinkCDCLoader.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2017 HugeGraph Authors
+ *
+ * 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 com.baidu.hugegraph.loader.flink;
+
+import java.net.URISyntaxException;
+import java.util.List;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.streaming.api.datastream.DataStreamSource;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.http.client.utils.URIBuilder;
+import org.slf4j.Logger;
+
+import com.baidu.hugegraph.loader.exception.LoadException;
+import com.baidu.hugegraph.loader.executor.LoadOptions;
+import com.baidu.hugegraph.loader.mapping.InputStruct;
+import com.baidu.hugegraph.loader.mapping.LoadMapping;
+import com.baidu.hugegraph.loader.source.jdbc.JDBCSource;
+import com.baidu.hugegraph.loader.util.Printer;
+import com.baidu.hugegraph.util.Log;
+import com.ververica.cdc.connectors.mysql.source.MySqlSource;
+
+public class HugeGraphFlinkCDCLoader {
+
+    public static final Logger LOG = Log.logger(HugeGraphFlinkCDCLoader.class);
+    private static final String JDBC_PREFIX = "jdbc:";
+    private final LoadOptions loadOptions;
+    private final String[] options;
+
+    public HugeGraphFlinkCDCLoader(String[] args) {
+        this.options = args;
+        this.loadOptions = LoadOptions.parseOptions(args);
+    }
+
+    public static void main(String[] args) {
+        HugeGraphFlinkCDCLoader loader;
+        try {
+            loader = new HugeGraphFlinkCDCLoader(args);
+        } catch (Throwable e) {
+            Printer.printError("Failed to start loading", e);
+            return;
+        }
+        loader.load();
+    }
+
+    public void load() {
+        LoadMapping mapping = LoadMapping.of(this.loadOptions.file);
+        List<InputStruct> structs = mapping.structs();
+        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+
+        for (InputStruct struct : structs) {
+

Review Comment:
   seems extra line



##########
hugegraph-loader/src/main/java/com/baidu/hugegraph/loader/flink/HugeGraphFlinkCDCLoader.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2017 HugeGraph Authors
+ *
+ * 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 com.baidu.hugegraph.loader.flink;
+
+import java.net.URISyntaxException;
+import java.util.List;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.streaming.api.datastream.DataStreamSource;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.http.client.utils.URIBuilder;
+import org.slf4j.Logger;
+
+import com.baidu.hugegraph.loader.exception.LoadException;
+import com.baidu.hugegraph.loader.executor.LoadOptions;
+import com.baidu.hugegraph.loader.mapping.InputStruct;
+import com.baidu.hugegraph.loader.mapping.LoadMapping;
+import com.baidu.hugegraph.loader.source.jdbc.JDBCSource;
+import com.baidu.hugegraph.loader.util.Printer;
+import com.baidu.hugegraph.util.Log;
+import com.ververica.cdc.connectors.mysql.source.MySqlSource;
+
+public class HugeGraphFlinkCDCLoader {
+
+    public static final Logger LOG = Log.logger(HugeGraphFlinkCDCLoader.class);
+    private static final String JDBC_PREFIX = "jdbc:";
+    private final LoadOptions loadOptions;
+    private final String[] options;
+
+    public HugeGraphFlinkCDCLoader(String[] args) {
+        this.options = args;
+        this.loadOptions = LoadOptions.parseOptions(args);
+    }
+
+    public static void main(String[] args) {
+        HugeGraphFlinkCDCLoader loader;
+        try {
+            loader = new HugeGraphFlinkCDCLoader(args);
+        } catch (Throwable e) {
+            Printer.printError("Failed to start loading", e);
+            return;
+        }
+        loader.load();
+    }
+
+    public void load() {
+        LoadMapping mapping = LoadMapping.of(this.loadOptions.file);
+        List<InputStruct> structs = mapping.structs();
+        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+
+        for (InputStruct struct : structs) {
+
+            MySqlSource<String> mysqlSource = buildMysqlSource(struct);
+            DataStreamSource<String> source =
+                    env.fromSource(mysqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source");
+
+            HugeGraphOutputFormat<Object> format = new HugeGraphOutputFormat<>(struct, options);
+            source.addSink(new HugeGraphSinkFunction<>(format)).setParallelism(1);
+        }
+        env.enableCheckpointing(3000);
+        try {
+            env.execute("flink-cdc-hugegraph");
+        } catch (Exception e) {
+            throw new LoadException("Failed to execute flink", e);
+        }
+    }
+
+    private MySqlSource<String> buildMysqlSource(InputStruct struct) {
+        JDBCSource input = (JDBCSource) struct.input();
+        String url = input.url();
+        String host;
+        int port;
+        try {
+            URIBuilder uriBuilder = new URIBuilder(url.substring(JDBC_PREFIX.length()));
+            host = uriBuilder.getHost();
+            port = uriBuilder.getPort();
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException(
+                    String.format("Failed to parse Url(%s) to get hostName and port", url), e);

Review Comment:
   `url` or `URL`?



##########
hugegraph-loader/src/main/java/com/baidu/hugegraph/loader/flink/HugeGraphFlinkCDCLoader.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2017 HugeGraph Authors
+ *
+ * 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 com.baidu.hugegraph.loader.flink;
+
+import java.net.URISyntaxException;
+import java.util.List;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.streaming.api.datastream.DataStreamSource;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.http.client.utils.URIBuilder;
+import org.slf4j.Logger;
+
+import com.baidu.hugegraph.loader.exception.LoadException;
+import com.baidu.hugegraph.loader.executor.LoadOptions;
+import com.baidu.hugegraph.loader.mapping.InputStruct;
+import com.baidu.hugegraph.loader.mapping.LoadMapping;
+import com.baidu.hugegraph.loader.source.jdbc.JDBCSource;
+import com.baidu.hugegraph.loader.util.Printer;
+import com.baidu.hugegraph.util.Log;
+import com.ververica.cdc.connectors.mysql.source.MySqlSource;
+
+public class HugeGraphFlinkCDCLoader {
+
+    public static final Logger LOG = Log.logger(HugeGraphFlinkCDCLoader.class);
+    private static final String JDBC_PREFIX = "jdbc:";
+    private final LoadOptions loadOptions;
+    private final String[] options;
+
+    public HugeGraphFlinkCDCLoader(String[] args) {
+        this.options = args;
+        this.loadOptions = LoadOptions.parseOptions(args);
+    }
+
+    public static void main(String[] args) {
+        HugeGraphFlinkCDCLoader loader;
+        try {
+            loader = new HugeGraphFlinkCDCLoader(args);
+        } catch (Throwable e) {
+            Printer.printError("Failed to start loading", e);
+            return;
+        }
+        loader.load();
+    }
+
+    public void load() {
+        LoadMapping mapping = LoadMapping.of(this.loadOptions.file);
+        List<InputStruct> structs = mapping.structs();
+        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+
+        for (InputStruct struct : structs) {
+
+            MySqlSource<String> mysqlSource = buildMysqlSource(struct);
+            DataStreamSource<String> source =
+                    env.fromSource(mysqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source");
+
+            HugeGraphOutputFormat<Object> format = new HugeGraphOutputFormat<>(struct, options);
+            source.addSink(new HugeGraphSinkFunction<>(format)).setParallelism(1);

Review Comment:
   why we set the parallelism to `1` for default
   
   BTW, we could add a option if it influences loader speed a lot



-- 
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@hugegraph.apache.org

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