You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/09/01 02:12:25 UTC

[GitHub] [inlong] woofyzhao commented on a diff in pull request #5389: [INLONG-4976][Manager] The Manager module supports the use of Kafka

woofyzhao commented on code in PR #5389:
URL: https://github.com/apache/inlong/pull/5389#discussion_r960159516


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/queue/kafka/KafkaOperator.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.queue.kafka;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.inlong.manager.pojo.cluster.kafka.KafkaClusterInfo;
+import org.apache.inlong.manager.service.cluster.InlongClusterServiceImpl;
+import org.apache.kafka.clients.admin.AdminClient;
+import org.apache.kafka.clients.admin.CreateTopicsResult;
+import org.apache.kafka.clients.admin.DeleteTopicsResult;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.common.PartitionInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+/**
+ * kafka operator, supports creating topics and creating subscription.
+ */
+@Service
+public class KafkaOperator {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(InlongClusterServiceImpl.class);
+
+    /**
+     * Create Kafka topic
+     */
+    public void createTopic(KafkaClusterInfo kafkaClusterInfo, String topicName)
+            throws InterruptedException, ExecutionException {
+        AdminClient adminClient = KafkaUtils.getAdminClient(kafkaClusterInfo);
+        NewTopic topic = new NewTopic(topicName,
+                kafkaClusterInfo.getNumPartitions(),
+                kafkaClusterInfo.getReplicationFactor());
+        CreateTopicsResult result = adminClient.createTopics(Collections.singletonList(topic));
+        // To prevent the client from disconnecting too quickly and causing the Topic to not be created successfully
+        Thread.sleep(500);
+        LOGGER.info("success to create kafka topic={}, with={} numPartitions",
+                topicName,
+                result.numPartitions(topicName).get());
+    }
+
+    /**
+     * Force delete Kafka topic
+     */
+    public void forceDeleteTopic(KafkaClusterInfo kafkaClusterInfo, String topicName) {
+        AdminClient adminClient = KafkaUtils.getAdminClient(kafkaClusterInfo);
+        DeleteTopicsResult result = adminClient.deleteTopics(Collections.singletonList(topicName));
+        LOGGER.info("success to delete topic={}", topicName);
+    }
+
+    public boolean topicIsExists(KafkaClusterInfo kafkaClusterInfo, String topic)
+            throws ExecutionException, InterruptedException {
+        AdminClient adminClient = KafkaUtils.getAdminClient(kafkaClusterInfo);
+        Set<String> topicList = adminClient.listTopics().names().get();
+        return topicList.contains(topic);
+    }
+
+    public void createSubscription(KafkaClusterInfo kafkaClusterInfo, String subscription) {
+

Review Comment:
   abundant blank 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