简体   繁体   English

如何使用python在hadoop文件系统中创建json文件

[英]How to create json file in hadoop file system using python

 os.system('echo "%s" | hadoop fs -put -f - /app/hdp/logs/json/a.json' %(json_string))

json_string=json.dumps({"a":"b"})

The file got created in HDFS but with wrong json format.该文件是在 HDFS 中创建的,但 json 格式错误。 The format it stored was {a:b} with no doubles quotes which is not a proper json format.它存储的格式是{a:b} ,没有双引号,这不是正确的 json 格式。

What is wrong in this approach?这种方法有什么问题?

Try using the following:尝试使用以下方法:

import subprocess, json

json_string=json.dumps({"a":"b"})

proc = subprocess.Popen('echo "{0}" | hadoop fs -put -f - /app/hdp/logs/json/a.json'.format(json_string), shell=True)

The string should be formatted as "{"a":"b"}"字符串的格式应为"{"a":"b"}"

import subprocess, json
json_string=json.dumps({"a":"b"})
#json_string=json_string.replace('"','\"') try escaping quotes too
proc = subprocess.run('echo {0} | hadoop fs -put -f - /app/hdp/logs/json/a.json'.format(json_string), shell=True)

You will need to include escape characters您将需要包含转义字符

echo "{"a":"b"}" in shell terminal在 shell 终端中回显 "{"a":"b"}"

Output: {a:b}输出:{a:b}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM