I need to wrap kcat
in a Go function to read a series of topic messages, so thought to use exec.Command()
for this is as follows:
package main
import (
"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))
}
However, this returns only the first line of the kcat
output:
/app/tools # ./five
Command String: /usr/bin/kcat -b kafka.kafka.svc.cluster.local:9092 -t messages -o 11000 -c 11333
Result Length: 58
Result Content: % Auto-selecting Producer mode (use -P or -C to override)
(NOTE: I'm running this within a docker container, however I don't think it makes a difference)
However, this works fine when run directly from the 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"}]
[{"Name":"newOrder", "ID":"9318","Time":"9318","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"1743","Time":"1743","Data":"new order", "Eventname":"newOrder"}]
There seems to be something unique about the kcat
command which breaks exec.Command()
in Go.
Questions:
- Is there any other way I could achieve the same effect in Go?
- Is this perhaps an issue with the way I'm using
exec.Command()
Ideally, I can use the the kcat
command in this case as I'd like to avoid using segmentios kafka-go
library in this instance.
[EDIT]
- Separating the arguments (as suggested by @onecricketeer):
cmd := exec.Command("kcat", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")
Result (same error):
/app/tools # ./code
Command String: /usr/bin/kcat -b kafka.kafka.svc.cluster.local:9092 -t messages -o 11000 -c 11333
Result Length: 58
Result Content: % Auto-selecting Producer mode (use -P or -C to override)
- Using BASH as the shell (suggested by maxm):
Same result, i.e only the first line of the kcat output is reported:
/app/tools # ./code
Command String: /bin/bash -c kcat -b kafka.kafka.svc.cluster.local:9092 -t messages -o 11000 -c 11333
Result Length: 58
Result Content: % Auto-selecting Producer mode (use -P or -C to override)
[EDIT]
NOTE: However, when I use Python's shell execution mechanism, it works fine, which leads me to wonder if there is something defective about Gos shell handling features:
import subprocess
process = subprocess.Popen(["kcat","-b","kafka.kafka.svc.cluster.local:9092","-t","messages","-o","1", "-c", "11"],
stdout=subprocess.PIPE,
universal_newlines=True)
while True:
output = process.stdout.readline()
print(output.strip())
# Do something else
return_code = process.poll()
if return_code is not None:
print('RETURN CODE', return_code)
# Process has finished, read rest of the output
for output in process.stdout.readlines():
print(output.strip())
break
Results:
/app/tools/python # python3 code.py
% Auto-selecting Consumer mode (use -P or -C to override)
%4|1641004616.232|OFFSET|rdkafka#consumer-1| [thrd:main]: messages [2]: offset reset (at offset 1) to END: fetch failed due to requested offset not available on the broker: Broker: Offset out of range
%4|1641004616.236|OFFSET|rdkafka#consumer-1| [thrd:main]: messages [1]: offset reset (at offset 1) to END: fetch failed due to requested offset not available on the broker: Broker: Offset out of range
[{"Name":"newOrder", "ID":"4512","Time":"4512","Data":"new order", "Eventname":"newOrder"}]
RETURN CODE 0
[{"Name":"newOrder", "ID":"2388","Time":"2388","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"8707","Time":"8707","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"1643","Time":"1643","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"2421","Time":"2421","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"7520","Time":"7520","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"1258","Time":"1258","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"1457","Time":"1457","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"2907","Time":"2907","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"9266","Time":"9266","Data":"new order", "Eventname":"newOrder"}]
[{"Name":"newOrder", "ID":"1547","Time":"1547","Data":"new order", "Eventname":"newOrder"}]
CodePudding user response:
The go command:
cmd := exec.Command("kcat", "-b kafka.kafka.svc.cluster.local:9092", "-t messages", "-o 11000", "-c 11333")
is the same as the shell command:
kcat "-b kafka.kafka.svc.cluster.local:9092" "-t messages" "-o 11000" "-c 11333"
You need to separate your arguments, the same as the shell is doing for you on every space by default:
cmd := exec.Command("kcat", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")
CodePudding user response:
As the output says, producer mode is being auto selected
Try using consumer mode with separated arguments
cmd := exec.Command("kcat", "-C", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")