“exec.Command()”在运行“kcat”时没有输出
来源:stackoverflow
2024-02-15 10:42:25
0浏览
收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《“exec.Command()”在运行“kcat”时没有输出》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
问题内容
我需要将 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"}]
kcat 命令似乎有一些独特之处,它会破坏 go 中的 exec.command() 。
问题:
- 还有其他方法可以在 go 中实现相同的效果吗?
- 这可能是我使用
exec.command()方式的问题
理想情况下,我可以在这种情况下使用 kcat 命令,因为我想避免在这种情况下使用segmentios kafka-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 作为 shell(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"}]正确答案
正如输出所示,正在自动选择生产者模式
尝试使用带有分隔参数的消费者模式
cmd := exec.command("kcat", "-c", "-b", "kafka.kafka.svc.cluster.local:9092", "-t", "messages", "-o", "11000", "-c", "11333")
go 命令:
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")
到这里,我们也就讲完了《“exec.Command()”在运行“kcat”时没有输出》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
聚合多个消息并发送到一个 Go 通道
- 上一篇
- 聚合多个消息并发送到一个 Go 通道
- 下一篇
- 解析多部分表单数据的 RFC 1867
查看更多
最新文章
-
- Golang · Go问答 | 2天前 | 错误处理 · go · 性能 · bytes.Buffer · Go 1.26 · io.EOF 版本迁移 Go 1.26 bytes.Buffer.Peek 缓冲区预览
- Go 1.26 bytes.Buffer.Peek 怎么迁移:非消费式预览、EOF 与兼容边界
- 428浏览 收藏
-
- Golang · Go问答 | 2天前 | go · 版本管理 · 持续集成 · Go CI GOTOOLCHAIN
- Go 项目怎么在 CI 里固定工具链:GOTOOLCHAIN、go.mod 与版本矩阵
- 488浏览 收藏
-
- Golang · Go问答 | 2天前 |
- Go JSON 接口如何拒绝未知字段:DisallowUnknownFields、兼容升级与错误定位
- 160浏览 收藏
-
- Golang · Go问答 | 2天前 |
- Go 服务为什么该从 log.Printf 迁移到 slog:结构化字段、级别与采样边界
- 158浏览 收藏
-
- Golang · Go问答 | 2天前 | golang · 连接池 · database/sql · Go问答 · 数据库事务 · 连接池 事务 DBStats rows.Close Go database/sql
- Go database/sql 忘记 Rows.Close 为什么会拖垮连接池:事务收尾与排查
- 374浏览 收藏
-
- Golang · Go问答 | 2天前 |
- Go 回调接口为什么不该统一返回 error:同步确认、异步投递与错误所有权
- 382浏览 收藏
-
- Golang · Go问答 | 2天前 |
- JSON 零值字段怎么省略:omitzero 与 omitempty 的差异和验收
- 158浏览 收藏
-
- Golang · Go问答 | 3天前 |
- Go 1.25 Flight Recorder 怎么抓短时故障:启动、导出与回放边界
- 279浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- ljg-skills
- ljg-skills 是李继刚开源的 AI 技能与提示词集合,面向大模型使用者整理了一批可复用的 prompt、角色设定和任务技能模板,适合用于学习提示词设计、搭建个人 AI 工作流和沉淀团队常用智能体能力。
- 4838次使用
-
- MELO音乐
- MELO音乐是一站式AI视频与音乐制作助手,对标suno, udio的高品质体验。提供伴奏生成、原创写词、无损导出、哼唱识曲、混音变声等全套音频与短视频编辑工具。无论是流行Kpop、电音说唱、民谣古风、摇滚儿歌还是商用轻音乐,MELO为你免费谱曲,轻松做同款!
- 4429次使用
-
- UniScribe
- UniScribe 是一款 AI 音视频转文字与内容整理工具,支持上传音频、视频文件或粘贴 YouTube 链接,自动生成转写文本、摘要、思维导图和关键问题,并支持多格式导出,适合会议记录、课程学习、访谈整理和内容创作复盘。
- 4369次使用
-
- 剧云
- 剧云是专业中文剧本创作平台,安全稳定运行十余年,集成AI编剧、剧本医生审核、人物小传、剧情关系图、大纲编写、多人协作、Word导入导出、版权管控功能,数据安全防护,轻松高效创作剧本。
- 4601次使用
-
- 万象有声
- 万象有声,一个专为有声创作者打造的新一代智能有声内容创作平台。平台提供专业的智能拆章、智能画本编辑、AI配音、AI生成音效、后期制作、智能对轨、智能审听等有声创作全流程工具,可以帮助创作者高效、低成本创作出引人入胜的有声作品。立即体验,让有声书制作更简单!
- 4557次使用
查看更多
相关文章
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go bufio.Scanner 遇到 token too long 怎么办:大日志行的长度上限与内存取舍
- 2026-07-22 501浏览
-
- 从不同的 go 例程将数据写入同一通道无需等待组即可正常工作
- 2024-04-29 501浏览
-
- Golang rsa-oaep解密失败,前端使用webcrypto
- 2024-04-26 501浏览

