0

我需要包装kcat一个 Go 函数来读取一系列主题消息,因此考虑使用exec.Command()如下:

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))

}

但是,这仅返回输出的第一行kcat

/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)

(注意:我在 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"}]

[{"Name":"newOrder", "ID":"9318","Time":"9318","Data":"new order", "Eventname":"newOrder"}]

[{"Name":"newOrder", "ID":"1743","Time":"1743","Data":"new order", "Eventname":"newOrder"}]

在 Gokcat中中断的命令似乎有一些独特之处。exec.Command()

问题:

  • 有没有其他方法可以在 Go 中达到相同的效果?
  • 这可能是我使用方式的问题吗exec.Command()

理想情况下,我可以在这种情况下使用该kcat命令,因为我想避免在这种情况下使用 segmentioskafka-go库。

[编辑]

  • 分离论点(如@onecricketeer 所建议):
cmd := exec.Command("kcat", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")

结果(同样的错误):

/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)
  • 使用 BASH 作为外壳(由 maxm 建议):

结果相同,即只报告了 kcat 输出的第一行:

/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)

[编辑]

注意:但是,当我使用 Python 的 shell 执行机制时,它运行良好,这让我想知道 Gos shell 处理功能是否存在缺陷:

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

结果:

/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"}]



4

2 回答 2

1

执行命令:

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")
于 2021-12-31T15:54:09.050 回答
1

正如输出所说,生产者模式正在自动选择

尝试使用带有单独参数的消费者模式

cmd := exec.Command("kcat", "-C", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")
于 2022-01-01T13:22:41.647 回答