如何在Ballerina Service API中执行一些Kafka命令

时间:2018-10-08 10:25:29

标签: apache-kafka ballerina ballerina-kafka

有可能在芭蕾舞演员中实现吗

  1. 要在芭蕾舞演员中创建新的kafka主题
  2. 列出芭蕾舞女演员中可用的主题
  3. 订阅芭蕾舞女演员中创建的主题

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码订阅主题:

import ballerina/log;
import wso2/kafka;
import ballerina/internal;

// Kafka consumer endpoint
endpoint kafka:SimpleConsumer consumer {
    bootstrapServers: "localhost:9092, localhost:9093",
    // Consumer group ID
    groupId: "test-group",
    // Listen from topic 'test'
    topics: ["test"],
    // Poll every 1 second
    pollingInterval:1000
};

// Kafka service that listens from the topic 'product-price'
// 'inventoryControlService' subscribed to new product price updates from
// the product admin and updates the Database.
service<kafka:Consumer> kafkaService bind consumer {
    // Triggered whenever a message added to the subscribed topic
    onMessage(kafka:ConsumerAction consumerAction, kafka:ConsumerRecord[] records) {
        // Dispatched set of Kafka records to service, We process each one by one.
        foreach entry in records {
            byte[] serializedMsg = entry.value;
            // Convert the serialized message to string message
            string msg = internal:byteArrayToString(serializedMsg, "UTF-8");
            log:printInfo("New message received from the product admin");
            // log the retrieved Kafka record
            log:printInfo("Topic: " + entry.topic + "; Received Message: " + msg);
            // Mock logic
            // Update the database with the new price for the specified product
            log:printInfo("Database updated with the new price of the product");
        }
    }
}

This Github repo对您可能很有用。它包含了面向消费者和生产者的各种示例。

关于创建和列出主题的问题,如果您不需要从芭蕾舞女演员那里执行这些操作,则可以从命令行执行:

bin/kafka-topics.sh --zookeeper localhost:2181 --topic test --from-beginning
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor <number_of_replicas> --partitions <number_of_partitions> --topic test

答案 1 :(得分:0)

编辑:更新示例代码以符合最新的芭蕾舞女演员版本(从V0.990.0起)。

您可以

  1. 创建一个新主题
  

如果使用Kafka producer发送数据,它将把数据发布到该特定主题,如果该主题不可用,它将创建该主题并发布。   考虑您要从生产者发布到主题test。您可以创建一个名为sampleProducer的生产者终结点,然后使用send()函数将数据发送到特定主题。

kafka:SimpleProducer sampleProducer = new ({
  bootstrapServers: "localhost:9090",
  acks: "all",
});

string topic = "test";
string msg = "Your Message";
byte[] messageToPublish = msg.toByteArray("UTF-8");
sampleProducer->send(messageToPublish, topic);`
  

现在,如果在test上托管的Kafka经纪人有一个名为localhost:9090的主题,它将发布该主题的消息,或者创建一个主题(如果没有)存在。

  1. 订阅新主题
  

您可以使用Kafka:SimpleConsumer.subscribe()通话来订阅主题。

listener kafka:SimpleConsumer sampleConsumer = new ({
  bootstrapServers: "localhost:9090",
  groupId: "test-consumers",
  autoCommit: false
});

string topic = "test";
string[] topics = [topic];
sampleConsumer->subscribe(topics);
  

请注意,subscribe()string[]作为输入参数,因此您应该向其中传递一个string[]

     

还有其他功能,例如subscribeToPattern()subscribeWithPartitionRebalance(),也可以用于使消费者订阅主题,您可以在API Documentation中找到有关它们的更多信息。

但是要列出可用主题,您需要从Zookeeper本身获取主题列表。但是,您可以使用芭蕾舞女演员获得主题列表,该主题列表当前已由特定消费者订阅。

string[] subscribedTopics;
var result = sampleConsumer->getSubscription();
if (result is error) {
  // Your logic for handling the error
} else {
    subscribedTopics = result;
}
  

请确保在此处处理错误,因为getSubscription()可以返回string[]error。芭蕾舞女演员式的后卫可以帮到您。