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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
