cbq导出的JSON无法被cbimport导入,如何通过cbq命令自动适配格式?
Couchbase cbq查询结果适配cbimport导入解决方案
问题根因
cbq默认返回的是完整的查询响应结构,包含requestID、metrics、status等控制字段,业务数据仅嵌套在results数组中,且每一条查询结果外层会包裹桶名作为顶层键,同时默认输出美化格式化的JSON结构,不符合cbimport的格式要求。
解决方案
通过调整cbq命令搭配jq参数,可单步导出符合要求的导入文件:
Linux/macOS 环境命令
cbq -u Administrator -p Administrator -e "http://localhost:8093" \ --script="SELECT * FROM \`sample\` where customer.id=\"12345\"" -q \ | jq -c '.results[] | .sample' > temp.json
Windows PowerShell 环境命令
cbq -u Administrator -p Administrator -e "http://localhost:8093" ` --script="SELECT * FROM ``sample`` where customer.id=""12345""" -q ` | jq -c '.results[] | .sample' > temp.json
参数说明
jq -c:开启压缩输出模式,直接生成非美化的单行JSON,避免格式化带来的解析异常.results[]:遍历cbq响应中的业务数据数组,剥离所有外层控制字段.sample:剥离每一条结果外层的桶名包裹,直接输出原生业务数据(如果你的桶名不是sample,替换为实际桶名即可)
如果需要适配cbimport的-f list格式要求(输入为JSON数组),将jq部分修改为jq -c '[.results[] | .sample]'即可。
导入验证
生成的temp.json可直接使用原导入命令执行导入:
cbimport json -c http://{target-cluster}:8091 -u Administrator -p Administrator -b sample -d file://C:\Users\{myusername}\Desktop\temp.json -f list -g %docId%
内容的提问来源于stack exchange,提问作者sagar verma
相关产品推荐
相关产品推荐

