【发布时间】:2021-11-15 10:33:35
【问题描述】:
在终端中,如果我执行
docker run -it --rm --name consumer --link zookeeper:zookeeper --link kafka:kafka \
debezium/kafka:1.1 watch-topic -a bankserver1.bank.holding > C:/Users/User/python_test \
/holding_pivot.txt
日志结果会像这样写入文件holding_pivot.txt
WARNING: Using default BROKER_ID=1, which is valid only for non-clustered installations.
Using ZOOKEEPER_CONNECT=172.17.0.3:2181
Using KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://172.17.0.6:9092
Using KAFKA_BROKER=172.17.0.4:9092
Contents of topic bankserver1.bank.holding:
{"schema":{"type":"struct","fields":[{"type":"struct","fiel...
{"schema":{"type":"struct","fields":[{"type":"struct","file...
我想要一个 python 文件在写入文件日志之前处理它(我不想要一个从日志中读取的 python 文件(因为我必须区分哪一行已经被读取和处理,哪一行没有))
代码的过程是这样的
RESULT -> Python 文件(句柄)-> 写入日志文件。
主要问题:哪个 Python 库支持它?你能给我一个示例代码吗?
我看到一个这样的文件:
docker run -it --rm --name consumer --link zookeeper:zookeeper --link kafka:kafka \
debezium/kafka:1.1 watch-topic -a bankserver1.bank.holding \
| grep --line-buffered '^{' | pipe > ./stream.py > ./holding_pivot.txt
====================== 更新======================= 根据@piertoni,我添加了这个
docker run -it --rm --name consumer --link zookeeper:zookeeper --link /
kafka:kafka debezium/kafka:1.1 watch-topic -a bankserver1.bank.holding /
| python C:/Users/User/python_test/test_pipe.py > C:/Users/User/python_test /
/holding_pivot.txt
这是我的尝试test_pipe.py
#!/usr/bin/env python3 -u
# Note: the -u denotes unbuffered (i.e output straing to stdout without buffering data and then writing to stdout)
#!/usr/bin/env python3
import fileinput
import json
import os
import sys
from datetime import datetime
for line in sys.stdin:
with open('log.txt', 'a') as wr:
wr.write("Pipe success")
with open('log.txt', 'a') as wr:
wr.write("Pipe success")
with fileinput.input() as f:
for line in f:
with open('log.txt', 'a') as wr:
wr.write(f"Argument List: {str(line)}")
但是,log.txt 文件中没有写入任何内容(我只需要 python 文件可以读取初始管道的结果)。
【问题讨论】:
-
如果我理解正确,您希望将结果通过管道传输到 python 脚本?如果这是正确的检查这个答案:stackoverflow.com/questions/66225702/…
-
感谢@piertoni,但它仍然无法正常工作,请查看我在帖子中的更新。还有其他建议吗?
-
'它不工作':什么不工作?你希望代码做什么?它实际上是做什么的?
-
为什么,顺便说一下,你的 python 脚本的标准输出重定向到一个文件,因为你的 python 脚本实际上从未向标准输出写入任何内容?
-
我是否认为您希望您的 python 脚本充当管道数据的 过滤器?所以它从标准输入读取,与行做一些事情,然后写入标准输出?
标签: python