惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
B
Blog
博客园_首页
云风的 BLOG
云风的 BLOG
S
SegmentFault 最新的问题
罗磊的独立博客
Jina AI
Jina AI
C
Check Point Blog
Martin Fowler
Martin Fowler
J
Java Code Geeks
博客园 - 司徒正美
美团技术团队
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
小众软件
小众软件

NickChenyx's Blog

Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish
Create_Rubbish
nickChen · 2018-04-18 · via NickChenyx's Blog

本地文件处理工作流整理,待归档到一个合适的目录

通过 dsq join 文件条件查找

安装工具

// 文件 a.json
[
    {"type": "tool", "id": "1"},
    {"type": "tool", "id": "2"},
    {"type": "tool", "id": "3"},
    {"type": "book", "id": "1"},
    {"type": "none", "id": "1"}
]
// 文件 b.json
[
    {"author": "nickchen", "type":"tool", "tag":[1,2,3]},
    {"author": "whoru", "type":"none"},
    {"author": "teacher", "type":"book"}
]

需求打印出 author、type、id ,且 id=1 的组合

  1. 因为 dsq 没法解析内嵌列表,所有 b.json 中的 tag 字段必须要被删除
  2. 需要 dsq join a.json 和 b.json,且判断 id = 1
  3. 打印结果确认是否符合要求
  4. 输出一个格式化的 json array

开始执行:

  1. 清理 b.json 中的 tag 字段
    • jq 的最外侧 [] 是列表构造器,会生成一份列表
    • jq 的 .[] 代表迭代器,迭代当前这个 json array
    • jq 的 | 是 pipe 管道,将数据流转到下一个程序
    • jq 的 del() 是内建函数,可以删除某个字段
➜  cat b.json | jq '[.[]|del(.tag)]' > c.json
or
➜  cat b.json | jq '[.[]|{author:.author,type:.type}]' > c.json
  1. dsq join 查询 & 打印结果
    • dsq 的 --pretty 可以输出表格结果
    • dsq 输入多个文件,可以按照进行 join 操作,表名即文件顺序
➜  dsq --pretty a.json c.json "select {0}.id, {1}.type, {1}.author from {0} join {1} on {0}.type = {1}.type where {0}.id = 1"
+----------+----+------+
|  author  | id | type |
+----------+----+------+
| nickchen |  1 | tool |
| teacher  |  1 | book |
| whoru    |  1 | none |
+----------+----+------+
  1. 输出一个格式化的 json array
➜  dsq a.json c.json "select {0}.id, {1}.type, {1}.author from {0} join {1} on {0}.type = {1}.type where {0}.id = 1" | jq .
[
  {
    "id": "1",
    "type": "tool",
    "author": "nickchen"
  },
  {
    "type": "book",
    "author": "teacher",
    "id": "1"
  },
  {
    "id": "1",
    "type": "none",
    "author": "whoru"
  }
]

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nickchenyx@gmail.com