Golang - Kafka操作

写入

package main

import (
	"fmt"
	"github.com/Shopify/sarama"
	"os"
	"time"
)

func main() {
	config := sarama.NewConfig()
	config.Producer.Return.Successes = true
	config.Producer.Timeout = 5 * time.Second

	p, err := sarama.NewSyncProducer([]string{"xxx.xxx.xxx.xxx:9092"}, config)
	if err != nil {
		fmt.Fprintf(os.Stdout, "sarama.NewSyncProducer err, message=%v \n", err)
		return
	}
	defer p.Close()

	for i := 0; i < 10; i++ {
		msg := &sarama.ProducerMessage{
			Topic: "test",
			Value: sarama.ByteEncoder(fmt.Sprintf("sync: this is a message. index=%d", i)),
		}

		if part, offset, err := p.SendMessage(msg); err != nil {
			fmt.Fprintf(os.Stdout, "发送失败,Error: %v \n", err)
		} else {
			fmt.Fprintf(os.Stdout, "发送成功,partition=%d, offset=%d \n", part, offset)
		}
	}
}

读取

package main

import (
	"fmt"
	"github.com/Shopify/sarama"
	"os"
)

func main() {
	config := sarama.NewConfig()
	config.Consumer.Return.Errors = true
	config.Version = sarama.V0_11_0_2

	c, err := sarama.NewConsumer([]string{"xxx.xxx.xxx.xxx:9092"}, config)
	if err != nil {
		fmt.Fprintf(os.Stdout, "sarama.NewConsumer err, message=%v \n", err)
		return
	}
	defer c.Close()

	cp, err := c.ConsumePartition("test", 0, sarama.OffsetOldest)
	if err != nil {
		fmt.Fprintf(os.Stdout, "try create partition_consumer err, message=%v \n", err)
		return
	}
	defer cp.Close()

	for {
		select {
		case msg := <-cp.Messages():
			fmt.Fprintf(os.Stdout, "msg offset: %d, partition: %d, timestamp: %s, value: %s \n", msg.Offset, msg.Partition, msg.Timestamp.String(), string(msg.Value))
		case err := <-cp.Errors():
			fmt.Fprintf(os.Stdout, "err :%v \n", err)
		}
	}
}

你可能感兴趣的:(Golang)