• golang kafka


    golang kafka – hello world

    https://github.com/Shopify/sarama

    https://shopify.github.io/sarama/

    consumer.go

    package main
     
    import (
        "fmt"
        "github.com/Shopify/sarama"
        "log"
        "os"
        "strings"
        "sync"
    )
     
    var (
        wg     sync.WaitGroup
        logger = log.New(os.Stderr, "[srama]"log.LstdFlags)
    )
     
    func main() {
     
        sarama.Logger = logger
     
        consumer, err := sarama.NewConsumer(strings.Split("localhost:9092"","), nil)
        if err != nil {
            logger.Println("Failed to start consumer: %s", err)
        }
     
        partitionList, err := consumer.Partitions("hello")
        if err != nil {
            logger.Println("Failed to get the list of partitions: ", err)
        }
     
        for partition := range partitionList {
            pc, err := consumer.ConsumePartition("hello", int32(partition), sarama.OffsetNewest)
            if err != nil {
                logger.Printf("Failed to start consumer for partition %d: %s ", partition, err)
            }
            defer pc.AsyncClose()
     
            wg.Add(1)
     
            go func(sarama.PartitionConsumer) {
                defer wg.Done()
                for msg := range pc.Messages() {
                    fmt.Printf("Partition:%d, Offset:%d, Key:%s, Value:%s", msg.Partition, msg.Offset, string(msg.Key), string(msg.Value))
                    fmt.Println()
                }
            }(pc)
        }
     
        wg.Wait()
     
        logger.Println("Done consuming topic hello")
        consumer.Close()
    }

    producer.go

    package main
     
    import (
        "github.com/Shopify/sarama"
        "log"
        "os"
        "strings"
    )
     
    var (
        logger = log.New(os.Stderr, "[srama]"log.LstdFlags)
    )
     
    func main() {
        sarama.Logger = logger
     
        config := sarama.NewConfig()
        config.Producer.RequiredAcks = sarama.WaitForAll
        config.Producer.Partitioner = sarama.NewRandomPartitioner
     
        msg := &sarama.ProducerMessage{}
        msg.Topic = "hello"
        msg.Partition = int32(-1)
        msg.Key = sarama.StringEncoder("key")
        msg.Value = sarama.ByteEncoder("你好, 世界!")
     
        producer, err := sarama.NewSyncProducer(strings.Split("localhost:9092"","), config)
        if err != nil {
            logger.Println("Failed to produce message: %s", err)
            os.Exit(500)
        }
        defer producer.Close()
     
        partition, offset, err := producer.SendMessage(msg)
        if err != nil {
            logger.Println("Failed to produce message: ", err)
        }
        logger.Printf("partition=%d, offset=%d ", partition, offset)
    }

     

    此条目发表在GolangLinux分类目录。将固定链接加入收藏夹。
  • 相关阅读:
    codeforces 665C C. Simple Strings(乱搞)
    codeforces 665B B. Shopping(水题)
    codeforces 665A A. Buses Between Cities(水题)
    hdu-2647 Reward && hdu-2049产生冠军 &&hdu-3342Legal or Not(拓扑排序)
    codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
    hdu-5596 GTW likes gt(模拟+优先队列)
    codeforces 664C C. International Olympiad(数学)
    hdu-5003 Osu!(水题)
    hdu-5000 Clone(dp)
    组合数学中的常见定理&组合数的计算&取模
  • 原文地址:https://www.cnblogs.com/simbadan/p/5487095.html
Copyright © 2020-2023  润新知