我需要包裝kcat一個 Go 函數(shù)來讀取一系列主題消息,因此考慮使用exec.Command()如下:package mainimport ( "fmt" "os/exec")func main() { cmd := exec.Command("kcat", "-b kafka.kafka.svc.cluster.local:9092", "-t messages", "-o 11000", "-c 11333") fmt.Println("Command String:", cmd.String()) out, err := cmd.CombinedOutput() if err != nil { fmt.Println("Error Accessing kafka topic messages ", err.Error(), string(out)) return } fmt.Println("Result Length:", len(out)) fmt.Println("Result Content:", string(out))}但是,這僅返回輸出的第一行kcat:/app/tools # ./fiveCommand String: /usr/bin/kcat -b kafka.kafka.svc.cluster.local:9092 -t messages -o 11000 -c 11333Result Length: 58Result Content: % Auto-selecting Producer mode (use -P or -C to override)(注意:我在 docker 容器中運行它,但我認為這沒有什么不同)但是,當(dāng)直接從 CLI 運行時,這可以正常工作:/app/tools # /app/tools # kcat -b kafka.kafka.svc.cluster.local:9092 -t messages -o 10 -c 15% Auto-selecting Consumer mode (use -P or -C to override)%4|1640957136.462|OFFSET|rdkafka#consumer-1| [thrd:main]: messages [1]: offset reset (at offset 10) to END: fetch failed due to requested offset not available on the broker: Broker: Offset out of range%4|1640957136.483|OFFSET|rdkafka#consumer-1| [thrd:main]: messages [2]: offset reset (at offset 10) to END: fetch failed due to requested offset not available on the broker: Broker: Offset out of range[{"Name":"newOrder", "ID":"9266","Time":"9266","Data":"new order", "Eventname":"newOrder"}][{"Name":"newOrder", "ID":"1547","Time":"1547","Data":"new order", "Eventname":"newOrder"}][{"Name":"newOrder", "ID":"9179","Time":"9179","Data":"new order", "Eventname":"newOrder"}][{"Name":"newOrder", "ID":"8740","Time":"8740","Data":"new order", "Eventname":"newOrder"}]在 Gokcat中中斷的命令似乎有一些獨特之處。exec.Command()問題:有沒有其他方法可以在 Go 中達到相同的效果?這可能是我使用方式的問題嗎exec.Command()理想情況下,我可以在這種情況下使用該kcat命令,因為我想避免在這種情況下使用 segmentioskafka-go庫。
運行 `kcat` 時,`exec.Command()` 不產(chǎn)生輸出
慕標(biāo)5832272
2022-10-24 09:47:50