You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by "fuweng11 (via GitHub)" <gi...@apache.org> on 2023/03/14 07:38:09 UTC

[GitHub] [inlong] fuweng11 commented on a diff in pull request #7274: [INLONG-7273][Manager] Support creating table in Kudu cluster

fuweng11 commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1135102122


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/kudu/KuduResourceClient.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.inlong.manager.service.resource.sink.kudu;
+
+import org.apache.inlong.manager.pojo.sink.kudu.KuduColumnInfo;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduTableInfo;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduType;
+import org.apache.kudu.ColumnSchema;
+import org.apache.kudu.ColumnSchema.ColumnSchemaBuilder;
+import org.apache.kudu.Schema;
+import org.apache.kudu.Type;
+import org.apache.kudu.client.AlterTableOptions;
+import org.apache.kudu.client.CreateTableOptions;
+import org.apache.kudu.client.KuduClient;
+import org.apache.kudu.client.KuduException;
+import org.apache.kudu.client.KuduTable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * The resourceClient for Kudu.
+ */
+public class KuduResourceClient {
+
+    private static final Logger LOG = LoggerFactory.getLogger(KuduResourceClient.class);
+
+    private static final String PARTITION_STRATEGY_HASH = "HASH";
+    private static final String PARTITION_STRATEGY_PRIMARY_KEY = "PrimaryKey";
+
+    private final KuduClient client;
+
+    public KuduResourceClient(String kuduMaster) {
+        this.client = new KuduClient.KuduClientBuilder(kuduMaster).build();
+    }
+
+    public boolean tableExist(String tableName) {
+        try {
+            return client.tableExists(tableName);
+        } catch (KuduException e) {
+            LOG.error("Can not properly query kudu!", e);
+        }
+        return false;
+    }
+
+    /**
+     * Create table with given tableName and tableInfo.
+     */
+    public void createTable(String tableName, KuduTableInfo tableInfo) throws KuduException {
+        // Parse column from tableInfo.
+        List<KuduColumnInfo> columns = tableInfo.getColumns();
+        List<ColumnSchema> kuduColumns = columns.stream()
+                .sorted(Comparator.comparing(KuduColumnInfo::getPartitionStrategy))
+                .map(this::buildColumnSchema)
+                .collect(Collectors.toList());
+
+        // Build schema.
+        Schema schema = new Schema(kuduColumns);
+        // Create table options: like partitions
+        CreateTableOptions options = new CreateTableOptions();
+        List<String> parCols = columns.stream()
+                .filter(column -> PARTITION_STRATEGY_HASH.equalsIgnoreCase(column.getPartitionStrategy()))
+                .map(KuduColumnInfo::getFieldName).collect(Collectors.toList());
+        if (!parCols.isEmpty()) {
+            options.addHashPartitions(parCols, 6);

Review Comment:
   Please explain what this 6 does.



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

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