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

[GitHub] [inlong] featzhang opened a new pull request, #7274: [INLONG-7273][Manager] Support creating table in Kudu cluster

featzhang opened a new pull request, #7274:
URL: https://github.com/apache/inlong/pull/7274

   ### Prepare a Pull Request
   
   - [INLONG-7273][Manager] Support creating table in Kudu cluster
   
   - Fixes #7273 
   
   ### Motivation
   
   
   ### Modifications
   
   
   ### Verifying this change
   
   *(Please pick either of the following options)*
   
   - [x] This change is a trivial rework/code cleanup without any test coverage.
   
   - [x] This change is already covered by existing tests, such as:
     *(please describe tests)*
   
   - [ ] This change added tests and can be verified as follows:
   
     *(example:)*
     - *Added integration tests for end-to-end deployment with large payloads (10MB)*
     - *Extended integration test for recovery after broker failure*
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
     - If a feature is not applicable for documentation, explain why?
     - If a feature is not documented yet in this PR, please create a follow-up issue for adding the documentation
   


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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1141377783


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/kudu/KuduResourceClient.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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 static final int DEFAULT_BUCKETS = 6;
+
+    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);

Review Comment:
   throwed



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


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

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
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


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

Posted by "healchow (via GitHub)" <gi...@apache.org>.
healchow commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1089157319


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/kudu/KuduResourceClient.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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 java.util.List;
+
+public class KuduResourceClient {
+
+    public KuduResourceClient(String metastoreUri, String dbName) {
+
+    }
+
+    public void open() {

Review Comment:
   Excuse me, but why those methods were empty?



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


[GitHub] [inlong] healchow merged pull request #7274: [INLONG-7273][Manager] Support creating table in Kudu cluster

Posted by "healchow (via GitHub)" <gi...@apache.org>.
healchow merged PR #7274:
URL: https://github.com/apache/inlong/pull/7274


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


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

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
fuweng11 commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1090109990


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/kudu/KuduResourceOperator.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.commons.collections.CollectionUtils;
+import org.apache.inlong.manager.common.consts.InlongConstants;
+import org.apache.inlong.manager.common.consts.SinkType;
+import org.apache.inlong.manager.common.enums.SinkStatus;
+import org.apache.inlong.manager.common.exceptions.WorkflowException;
+import org.apache.inlong.manager.dao.entity.StreamSinkFieldEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSinkFieldEntityMapper;
+import org.apache.inlong.manager.pojo.sink.SinkInfo;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduColumnInfo;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduSinkDTO;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduTableInfo;
+import org.apache.inlong.manager.service.node.DataNodeOperateHelper;
+import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator;
+import org.apache.inlong.manager.service.sink.StreamSinkService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * Kudu resource operator
+ */
+@Service
+public class KuduResourceOperator implements SinkResourceOperator {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(KuduResourceOperator.class);
+
+    @Autowired
+    private StreamSinkService sinkService;
+    @Autowired
+    private StreamSinkFieldEntityMapper sinkFieldMapper;
+    @Autowired
+    private DataNodeOperateHelper dataNodeHelper;
+
+    @Override
+    public Boolean accept(String sinkType) {
+        return SinkType.KUDU.equals(sinkType);
+    }
+
+    /**
+     * Create Kudu table according to the sink config
+     */

Review Comment:
   @Override



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


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

Posted by "fuweng11 (via GitHub)" <gi...@apache.org>.
fuweng11 commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1090109990


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/kudu/KuduResourceOperator.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.commons.collections.CollectionUtils;
+import org.apache.inlong.manager.common.consts.InlongConstants;
+import org.apache.inlong.manager.common.consts.SinkType;
+import org.apache.inlong.manager.common.enums.SinkStatus;
+import org.apache.inlong.manager.common.exceptions.WorkflowException;
+import org.apache.inlong.manager.dao.entity.StreamSinkFieldEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSinkFieldEntityMapper;
+import org.apache.inlong.manager.pojo.sink.SinkInfo;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduColumnInfo;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduSinkDTO;
+import org.apache.inlong.manager.pojo.sink.kudu.KuduTableInfo;
+import org.apache.inlong.manager.service.node.DataNodeOperateHelper;
+import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator;
+import org.apache.inlong.manager.service.sink.StreamSinkService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * Kudu resource operator
+ */
+@Service
+public class KuduResourceOperator implements SinkResourceOperator {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(KuduResourceOperator.class);
+
+    @Autowired
+    private StreamSinkService sinkService;
+    @Autowired
+    private StreamSinkFieldEntityMapper sinkFieldMapper;
+    @Autowired
+    private DataNodeOperateHelper dataNodeHelper;
+
+    @Override
+    public Boolean accept(String sinkType) {
+        return SinkType.KUDU.equals(sinkType);
+    }
+
+    /**
+     * Create Kudu table according to the sink config
+     */

Review Comment:
   `@Override`



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


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

Posted by "featzhang (via GitHub)" <gi...@apache.org>.
featzhang commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1141251727


##########
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:
   Add a parameter configuration on the front-end page to specify buckets.
   
   https://github.com/apache/inlong/pull/7644



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


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

Posted by "healchow (via GitHub)" <gi...@apache.org>.
healchow commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1141279636


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/kudu/KuduResourceClient.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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 static final int DEFAULT_BUCKETS = 6;
+
+    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);

Review Comment:
   Why not throw an exception here?



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


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

Posted by "healchow (via GitHub)" <gi...@apache.org>.
healchow commented on code in PR #7274:
URL: https://github.com/apache/inlong/pull/7274#discussion_r1135023870


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/kudu/KuduResourceClient.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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);
+        }
+
+        // Create table by KuduClient.
+        client.createTable(tableName, schema, options);
+    }
+
+    private ColumnSchema buildColumnSchema(KuduColumnInfo columnInfo) {
+        String name = columnInfo.getFieldName();
+        String type = columnInfo.getFieldType();
+        String desc = columnInfo.getFieldComment();
+
+        String kuduType = KuduType.forType(type).kuduType();
+        Type typeForName = Type.getTypeForName(kuduType);
+
+        String partitionStrategy = columnInfo.getPartitionStrategy();
+
+        ColumnSchemaBuilder builder = new ColumnSchemaBuilder(name, typeForName)
+                .comment(desc);
+        if (PARTITION_STRATEGY_HASH.equalsIgnoreCase(partitionStrategy)
+                || PARTITION_STRATEGY_PRIMARY_KEY.equalsIgnoreCase(partitionStrategy)) {
+            builder.key(true);
+        } else {
+            builder.nullable(true);
+        }
+        return builder.build();
+    }
+
+    private ColumnSchema buildColumnSchema(
+            String name,
+            String desc,
+            Type typeForName) {
+
+        ColumnSchemaBuilder builder = new ColumnSchemaBuilder(name, typeForName)
+                .comment(desc)
+                .nullable(true);
+        return builder.build();
+    }
+
+    public List<KuduColumnInfo> getColumns(String tableName) throws KuduException {
+        // Open table and get column information.
+        KuduTable kuduTable = client.openTable(tableName);
+        List<ColumnSchema> columns = kuduTable.getSchema().getColumns();
+        return columns
+                .stream()
+                .map(columnSchema -> {
+                    String comment = columnSchema.getComment();
+                    Type type = columnSchema.getType();
+                    String name = columnSchema.getName();
+
+                    String javaType = KuduType.forKuduType(type.getName()).getType();
+
+                    KuduColumnInfo columnInfo = new KuduColumnInfo();
+                    columnInfo.setFieldName(name);
+                    columnInfo.setFieldType(javaType);
+                    columnInfo.setFieldComment(comment);
+
+                    return columnInfo;
+                })
+                .collect(Collectors.toList());
+    }
+
+    public void addColumns(String tableName,
+            List<KuduColumnInfo> needAddColumns)
+            throws KuduException {
+        // Create Kudu client and open table
+        KuduTable table = client.openTable(tableName);
+
+        // Add new column to table
+        AlterTableOptions alterOptions = new AlterTableOptions();
+        for (KuduColumnInfo columnInfo : needAddColumns) {
+            String name = columnInfo.getFieldName();
+            String type = columnInfo.getFieldType();
+            String desc = columnInfo.getFieldComment();
+
+            String kuduType = KuduType.forType(type).kuduType();
+            Type typeForName = Type.getTypeForName(kuduType);
+
+            ColumnSchema columnSchema = buildColumnSchema(name, desc, typeForName);
+
+            alterOptions.addColumn(columnSchema);
+        }
+        alterOptions.addColumn("new_column_name", Type.STRING, "default_value");

Review Comment:
   Excuse me, what is the function of this line?



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