当前位置: 首页>>代码示例>>Golang>>正文


Golang FetchRequest.AddBlock方法代码示例

本文整理汇总了Golang中github.com/Shopify/sarama.FetchRequest.AddBlock方法的典型用法代码示例。如果您正苦于以下问题:Golang FetchRequest.AddBlock方法的具体用法?Golang FetchRequest.AddBlock怎么用?Golang FetchRequest.AddBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/Shopify/sarama.FetchRequest的用法示例。


在下文中一共展示了FetchRequest.AddBlock方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: testSarama

func testSarama(topic string, partition int32, seconds int) {
	stop := false

	config := sarama.NewClientConfig()
	client, err := sarama.NewClient("siesta", []string{"localhost:9092"}, config)
	if err != nil {
		panic(err)
	}

	messageChannel := make(chan *sarama.MessageSet, 10000)
	count := 0
	go func() {
		for {
			set := <-messageChannel
			count += len(set.Messages)
		}
	}()

	broker, err := client.Leader(topic, partition)

	//warm up
	fmt.Println("warming up")
	for i := 0; i < 5; i++ {
		fetchRequest := new(sarama.FetchRequest)
		fetchRequest.MinBytes = 1
		fetchRequest.MaxWaitTime = 100
		fetchRequest.AddBlock(topic, partition, 0, 500)

		broker.Fetch("siesta", fetchRequest)
	}
	fmt.Println("warm up finished, starting")

	go func() {
		time.Sleep(time.Duration(seconds) * time.Second)
		stop = true
	}()

	offset := int64(0)
	if err != nil {
		panic(err)
	}
	for !stop {
		fetchRequest := new(sarama.FetchRequest)
		fetchRequest.MinBytes = 1
		fetchRequest.MaxWaitTime = 100
		fetchRequest.AddBlock(topic, partition, offset, 500)

		response, err := broker.Fetch("siesta", fetchRequest)
		if err != nil {
			panic(err)
		}
		set := response.Blocks[topic][partition].MsgSet
		messageChannel <- &set
		offset = set.Messages[len(set.Messages)-1].Offset
	}

	fmt.Printf("%d within %d secnods\n", count, seconds)
	fmt.Printf("%d average\n", count/seconds)
}
开发者ID:pkoro,项目名称:go-kafka,代码行数:59,代码来源:bench.go

示例2: Fetch

// This will be called each time the fetch request to Kafka should be issued. Topic, partition and offset are self-explanatory.
// Returns slice of Messages and an error if a fetch error occurred.
func (this *SaramaClient) Fetch(topic string, partition int32, offset int64) ([]*Message, error) {
	leader, err := this.client.Leader(topic, partition)
	if err != nil {
		this.client.RefreshMetadata(topic)
		return nil, err
	}

	fetchRequest := new(sarama.FetchRequest)
	fetchRequest.MinBytes = this.config.FetchMinBytes
	fetchRequest.MaxWaitTime = this.config.FetchWaitMaxMs
	Debugf(this, "Adding block: topic=%s, partition=%d, offset=%d, fetchsize=%d", topic, partition, offset, this.config.FetchMessageMaxBytes)
	fetchRequest.AddBlock(topic, partition, offset, this.config.FetchMessageMaxBytes)

	response, err := leader.Fetch(fetchRequest)
	if err != nil {
		this.client.RefreshMetadata(topic)
		return nil, err
	}

	messages := make([]*Message, 0)
	if response != nil {
		Debug(this, "Processing fetch response")
		for topic, partitionAndData := range response.Blocks {
			for partition, data := range partitionAndData {
				switch data.Err {
				case sarama.ErrNoError:
					{
						if len(data.MsgSet.Messages) > 0 {
							this.filterPartitionData(data, offset)
							messages = this.collectMessages(data, topic, partition)
							if this.config.Debug {
								timestamp := time.Now().UnixNano() / int64(time.Millisecond)
								for _, message := range messages {
									message.DecodedKey = []int64{timestamp}
								}
							}
						} else {
							Debugf(this, "No messages in %s:%d at offset %d", topic, partition, offset)
						}
					}
				default:
					{
						this.client.RefreshMetadata(topic)
						return nil, data.Err
					}
				}
			}
		}
	}

	return messages, nil
}
开发者ID:echupriyanov,项目名称:go_kafka_client,代码行数:54,代码来源:low_level_client.go

示例3: processPartition

func processPartition(command string, topic string, partition int32, controlChannel chan []int64, offsetManager sarama.OffsetManager) {
	pom, err := offsetManager.ManagePartition(topic, int32(partition))
	if err != nil {
		exit(err)
	}
	consumerOffset, _ := pom.NextOffset()
	offset, err := kafkaClient.GetOffset(topic, int32(partition), sarama.OffsetNewest)
	//fmt.Println(topic, partition, consumerOffset, offset)
	if err != nil {
		exit(err)
	}
	var response = make([]int64, 0)
	var timelag int64
	lag := offset - consumerOffset + 1
	response = append(response, lag)
	if command == "lag_and_time" {
		broker, err := kafkaClient.Leader(topic, partition)
		if err != nil {
			exit(err)
		}
		fetchRequest := sarama.FetchRequest{MaxWaitTime: 10000, MinBytes: 0}
		fetchRequest.AddBlock(topic, partition, consumerOffset-2, 500000)
		fetchResponse, err := broker.Fetch(&fetchRequest)
		if err != nil {
			exit(err)
		}
		block := fetchResponse.GetBlock(topic, partition)
		messages := block.MsgSet.Messages
		if len(messages) > 0 {
			msg := messages[0].Messages()[0].Msg.Value
			var decodedData map[string]interface{}
			codec.NewDecoderBytes(msg, &msgpack).Decode(&decodedData)
			timestamp := decodedData["timestamp"]
			switch timestamp := timestamp.(type) {
			case uint64:
				timelag = int64(timestamp)
			default:
				fmt.Println(timestamp)
				exit(errors.New("message is missing timestamp"))
			}
		} else {
			timelag = 0
		}
		response = append(response, timelag)
	}
	controlChannel <- response
}
开发者ID:oruen,项目名称:kafka-consumer-lag,代码行数:47,代码来源:main.go


注:本文中的github.com/Shopify/sarama.FetchRequest.AddBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。