Extracting Data From Kafka REST Proxy in Go

huangapple go评论123阅读模式
英文:

Extracting Data From Kafka REST Proxy in Go

问题

我正在使用Kafka的REST代理实例来生产和消费消息。我正在使用API获取新消息,但是我无法将这些消息转换为Go语言中的结构模型。例如:

// 获取记录
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(FETCH_CONSUMER, URL, GROUP, CONSUMER), nil)
if err != nil {
panic(err)
}
req.Header.Add("Accept", CONTENT_TYPE)
respRecords, err := client.Do(req)
if err != nil {
panic(err)
}
defer respRecords.Body.Close()
fmt.Printf("Response %s\n", respRecords.Status)
fmt.Println(respRecords.Body)
recordsBodyResp := bufio.NewScanner(respRecords.Body)

for recordsBodyResp.Scan() {
fmt.Printf("<--Body %s\n", recordsBodyResp.Text())
}

返回的值的格式如下:

[{"topic":"backward","key":null,"value":{"AdoptionID":"abcd123","IPAddress":"8.8.8.8","Port":"80","Status":"requested"},"partition":0,"offset":7}]

由于它是一个对象数组,我想提取键"value"的值部分到一个结构体中。
这就是我卡住的地方。

英文:

I am using the REST proxy instance of Kafka for producing and consuming messages.Using the API to get new messages but I am not able to convert those messages to a struct model in Go. For example:

  1. // Get records
  2. req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(FETCH_CONSUMER, URL, GROUP, CONSUMER), nil)
  3. if err != nil {
  4. panic(err)
  5. }
  6. req.Header.Add(&quot;Accept&quot;, CONTENT_TYPE)
  7. respRecords, err := client.Do(req)
  8. if err != nil {
  9. panic(err)
  10. }
  11. defer respRecords.Body.Close()
  12. fmt.Printf(&quot;Response %s\n&quot;, respRecords.Status)
  13. fmt.Println(respRecords.Body)
  14. recordsBodyResp := bufio.NewScanner(respRecords.Body)
  15. for recordsBodyResp.Scan() {
  16. fmt.Printf(&quot;&lt;--Body %s\n&quot;, recordsBodyResp.Text())
  17. }

The value returned is in the following format:

  1. [{&quot;topic&quot;:&quot;backward&quot;,&quot;key&quot;:null,&quot;value&quot;:{&quot;AdoptionID&quot;:&quot;abcd123&quot;,&quot;IPAddress&quot;:&quot;8.8.8.8&quot;,&quot;Port&quot;:&quot;80&quot;,&quot;Status&quot;:&quot;requested&quot;},&quot;partition&quot;:0,&quot;offset&quot;:7}]

Since it's an array of objects, I want to extract the value portion of the key "value" into a struct.
That is where I am stuck.

答案1

得分: 2

你可以创建一个类似这样的结构体:

  1. type AutoGenerated []struct {
  2. Topic string `json:"topic"`
  3. Key interface{} `json:"key"`
  4. Value Value `json:"value"`
  5. Partition int `json:"partition"`
  6. Offset int `json:"offset"`
  7. }
  8. type Value struct {
  9. AdoptionID string `json:"AdoptionID"`
  10. IPAddress string `json:"IPAddress"`
  11. Port string `json:"Port"`
  12. Status string `json:"Status"`
  13. }

然后在该结构体中进行解析。

参考这个示例代码

  1. package main
  2. import (
  3. "fmt"
  4. "encoding/json"
  5. )
  6. func main() {
  7. type Value struct {
  8. AdoptionID string `json:"AdoptionID"`
  9. IPAddress string `json:"IPAddress"`
  10. Port string `json:"Port"`
  11. Status string `json:"Status"`
  12. }
  13. type AutoGenerated []struct {
  14. Topic string `json:"topic"`
  15. Key interface{} `json:"key"`
  16. Value Value `json:"value"`
  17. Partition int `json:"partition"`
  18. Offset int `json:"offset"`
  19. }
  20. byt := []byte(`[{"topic":"backward","key":null,"value":{"AdoptionID":"abcd123","IPAddress":"8.8.8.8","Port":"80","Status":"requested"},"partition":0,"offset":7}]`)
  21. var dat AutoGenerated
  22. if err := json.Unmarshal(byt, &dat); err != nil {
  23. panic(err)
  24. }
  25. fmt.Printf("%#v", dat)
  26. }
英文:

You can create a struct like:

  1. type AutoGenerated []struct {
  2. Topic string `json:&quot;topic&quot;`
  3. Key interface{} `json:&quot;key&quot;`
  4. Value Value `json:&quot;value&quot;`
  5. Partition int `json:&quot;partition&quot;`
  6. Offset int `json:&quot;offset&quot;`
  7. }
  8. type Value struct {
  9. AdoptionID string `json:&quot;AdoptionID&quot;`
  10. IPAddress string `json:&quot;IPAddress&quot;`
  11. Port string `json:&quot;Port&quot;`
  12. Status string `json:&quot;Status&quot;`
  13. }

And Unmarshal in that struct.

See this sample code:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;encoding/json&quot;
  5. )
  6. func main() {
  7. type Value struct {
  8. AdoptionID string `json:&quot;AdoptionID&quot;`
  9. IPAddress string `json:&quot;IPAddress&quot;`
  10. Port string `json:&quot;Port&quot;`
  11. Status string `json:&quot;Status&quot;`
  12. }
  13. type AutoGenerated []struct {
  14. Topic string `json:&quot;topic&quot;`
  15. Key interface{} `json:&quot;key&quot;`
  16. Value Value `json:&quot;value&quot;`
  17. Partition int `json:&quot;partition&quot;`
  18. Offset int `json:&quot;offset&quot;`
  19. }
  20. byt := []byte(`[{&quot;topic&quot;:&quot;backward&quot;,&quot;key&quot;:null,&quot;value&quot;:{&quot;AdoptionID&quot;:&quot;abcd123&quot;,&quot;IPAddress&quot;:&quot;8.8.8.8&quot;,&quot;Port&quot;:&quot;80&quot;,&quot;Status&quot;:&quot;requested&quot;},&quot;partition&quot;:0,&quot;offset&quot;:7}]`)
  21. var dat AutoGenerated
  22. if err := json.Unmarshal(byt, &amp;dat); err != nil {
  23. panic(err)
  24. }
  25. fmt.Printf(&quot;%#v&quot;, dat)
  26. }

huangapple
  • 本文由 发表于 2021年9月28日 21:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/69362308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定