我需要包裝kcat一個 Go 函數來讀取一系列主題消息,因此考慮使用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 容器中運行它,但我認為這沒有什么不同)但是,當直接從 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庫。
2 回答

慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
正如輸出所說,生產者模式正在自動選擇
嘗試使用帶有單獨參數的消費者模式
cmd := exec.Command("kcat", "-C", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")

胡子哥哥
TA貢獻1825條經驗 獲得超6個贊
執行命令:
cmd := exec.Command("kcat", "-b kafka.kafka.svc.cluster.local:9092", "-t messages", "-o 11000", "-c 11333")
與shell命令相同:
kcat "-b kafka.kafka.svc.cluster.local:9092" "-t messages" "-o 11000" "-c 11333"
你需要分開你的參數,就像默認情況下shell在每個空間上為你做的一樣:
cmd := exec.Command("kcat", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")
- 2 回答
- 0 關注
- 236 瀏覽
添加回答
舉報
0/150
提交
取消