Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 从单个输入生成多个JQ输出文档,修改每个结果_Json_Shell_Loops_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Jq - Fatal编程技术网 elasticsearch,jq,Json,Shell,Loops,elasticsearch,Jq" /> elasticsearch,jq,Json,Shell,Loops,elasticsearch,Jq" />

Json 从单个输入生成多个JQ输出文档,修改每个结果

Json 从单个输入生成多个JQ输出文档,修改每个结果,json,shell,loops,elasticsearch,jq,Json,Shell,Loops,elasticsearch,Jq,我想在JSON文件中执行2个操作。我试着用JQ和SHELL做这件事 第一个:我想将parents元素转换为纯文本值 第二个:我想删除JSON树中的一个特定级别 输入: { "template_first": { "order": 0, "index_patterns": [ "first" ], "settings": { &qu

我想在JSON文件中执行2个操作。我试着用JQ和SHELL做这件事

第一个:我想将parents元素转换为纯文本值

第二个:我想删除JSON树中的一个特定级别

输入:

    {
  "template_first": {
    "order": 0,
    "index_patterns": [
      "first"
    ],
    "settings": {
      "index": {
        "codec": "best_compression",
        "refresh_interval": "30s",
        "analysis": {
          "normalizer": {
            "norm_case_insensitive": {
              "filter": "lowercase",
              "type": "custom"
            }
          }
        },
        "number_of_shards": "1",
        "number_of_replicas": "1"
      }
    },
    "mappings": {
      "_doc": {
        "dynamic": true,
        "dynamic_templates": [
          {
            "strings": {
              "mapping": {
                "type": "keyword"
              },
              "match_mapping_type": "string"
            }
          }
        ],
        "properties": {
          "log.id": {
            "type": "keyword"
          },
          "host.indexer.hostname": {
            "type": "keyword"
          },
          "ts_indexer": {
            "format": "strict_date_optional_time||epoch_millis",
            "type": "date"
          }
        }
      }
    }
  },
  "template_second": {
    "order": 0,
    "index_patterns": [
      "second"
    ],
    "settings": {
      "index": {
        "codec": "best_compression",
        "refresh_interval": "30s",
        "analysis": {
          "normalizer": {
            "norm_case_insensitive": {
              "filter": "lowercase",
              "type": "custom"
            }
          }
        },
        "number_of_shards": "1",
        "number_of_replicas": "1"
      }
    },
    "mappings": {
      "_doc": {
        "dynamic": true,
        "dynamic_templates": [
          {
            "strings": {
              "mapping": {
                "type": "keyword"
              },
              "match_mapping_type": "string"
            }
          }
        ],
        "properties": {
          "log.id": {
            "type": "keyword"
          },
          "host.indexer.hostname": {
            "type": "keyword"
          },
          "ts_indexer": {
            "format": "strict_date_optional_time||epoch_millis",
            "type": "date"
          }
        }
      }
    }
  }
}
您可以看到文件中有两个JSON对象

    {
    "template_first" : { ...},
    "template_second" : { ... }
     }
第一次修改来自此命令的出现

放置模板/模板编号

而不是第一个JSON对象的键

那么预期的结果呢

PUT _template/template_first
  {...}
PUT _template/template_second
  {...}
  "mappings": {
    "dynamic": true,
    "dynamic_templates": [
      {
        "strings": {
          "mapping": {
            "type": "keyword"
          },
          "match_mapping_type": "string"
        }
      }
    ],
    "properties": {
      "log.id": {
        "type": "keyword"
      },
      "host.indexer.hostname": {
        "type": "keyword"
      },
      "ts_indexer": {
        "format": "strict_date_optional_time||epoch_millis",
        "type": "date"
      }
    }
  }
第二个变化是删除了_doclevel

之前:

  "mappings": {
    "_doc": {
      "dynamic": true,
      "dynamic_templates": [
        {
          "strings": {
            "mapping": {
              "type": "keyword"
            },
            "match_mapping_type": "string"
          }
        }
      ],
      "properties": {
        "log.id": {
          "type": "keyword"
        },
        "host.indexer.hostname": {
          "type": "keyword"
        },
        "ts_indexer": {
          "format": "strict_date_optional_time||epoch_millis",
          "type": "date"
        }
      }
    }
  }
预期结果

PUT _template/template_first
  {...}
PUT _template/template_second
  {...}
  "mappings": {
    "dynamic": true,
    "dynamic_templates": [
      {
        "strings": {
          "mapping": {
            "type": "keyword"
          },
          "match_mapping_type": "string"
        }
      }
    ],
    "properties": {
      "log.id": {
        "type": "keyword"
      },
      "host.indexer.hostname": {
        "type": "keyword"
      },
      "ts_indexer": {
        "format": "strict_date_optional_time||epoch_millis",
        "type": "date"
      }
    }
  }
所以实际结果是这样的

PUT _template/template_first
  {
  "order": 0,
  "index_patterns": [
    "first"
  ],
  "settings": {
    "index": {
      "codec": "best_compression",
      "refresh_interval": "30s",
      "analysis": {
        "normalizer": {
          "norm_case_insensitive": {
            "filter": "lowercase",
            "type": "custom"
          }
        }
      },
      "number_of_shards": "1",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "dynamic": true,
    "dynamic_templates": [
      {
        "strings": {
          "mapping": {
            "type": "keyword"
          },
          "match_mapping_type": "string"
        }
      }
    ],
    "properties": {
      "log.id": {
        "type": "keyword"
      },
      "host.indexer.hostname": {
        "type": "keyword"
      },
      "ts_indexer": {
        "format": "strict_date_optional_time||epoch_millis",
        "type": "date"
      }
    }
  }
}
PUT _template/template_second
  {
  "order": 0,
  "index_patterns": [
    "second"
  ],
  "settings": {
    "index": {
      "codec": "best_compression",
      "refresh_interval": "30s",
      "analysis": {
        "normalizer": {
          "norm_case_insensitive": {
            "filter": "lowercase",
            "type": "custom"
          }
        }
      },
      "number_of_shards": "1",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "dynamic": true,
    "dynamic_templates": [
      {
        "strings": {
          "mapping": {
            "type": "keyword"
          },
          "match_mapping_type": "string"
        }
      }
    ],
    "properties": {
      "log.id": {
        "type": "keyword"
      },
      "host.indexer.hostname": {
        "type": "keyword"
      },
      "ts_indexer": {
        "format": "strict_date_optional_time||epoch_millis",
        "type": "date"
      }
    }
  }
}
我实现了第二个更改:使用命令删除JSON数组的一个级别

jq  'keys[] as $k | map( .mappings =.mappings._doc   )' template.json
但我不知道如何在同一时间做第一次和第二次改变

我试图像这样循环到数组中,但没有成功

for row in $(jq 'keys[] as $k | "\($k)"' template.json); do
    _jq() {
     echo ${row} 
    }
   echo $(_jq '.name')
done

只调用一次
jq
,并让它编写一个以NUL分隔的模板名称/修改的模板内容对列表(bash
在读取
循环时可以对其进行迭代):

而IFS=read-r-d“”模板名称&&IFS=read-r-d“”模板内容;做
echo“我们希望将以下内容放入_template/$template_name”
printf“%s\n”$template\u content

只调用
jq
一次,并让它编写一个以NUL分隔的模板名称/修改后的模板内容对列表(读取时bash
循环可以迭代该列表):

而IFS=read-r-d“”模板名称&&IFS=read-r-d“”模板内容;做
echo“我们希望将以下内容放入_template/$template_name”
printf“%s\n”$template\u content

完成<我在
完成<方面遇到了一些问题,我在
完成<…顺便说一句,你能为你试图完成的操作建立一个简单的例子吗?ES映射定义相当大/笨拙;指导原则要求示例尽量少——在不做任何更改的情况下,尽可能短地运行,以展示您的问题或测试解决方案。感谢Charles提供的解决方案。我正在尝试将6.8版中的一个集群中的数千个模板导出到7.8版中的另一个集群中。我正在尝试将其自动化,因为我没有找到一种简单的方法从v6.8 ES群集导出所有模板…顺便说一句,您能否构建一个您试图完成的操作的简单示例?ES映射定义相当大/笨拙;指导原则要求示例尽量少——在不做任何更改的情况下,尽可能短地运行,以展示您的问题或测试解决方案。感谢Charles提供的解决方案。我正在尝试将6.8版中的一个集群中的数千个模板导出到7.8版中的另一个集群中。我正在尝试将其自动化,因为我没有找到一种简单的方法从v6.8 ES Cluster导出所有模板。流程替换要求您的shell实际上是bash,而不是以名称
sh
开头。检查您是否正在运行
sh您的脚本
或以
开头/bin/sh
shebang。(选择进程替换方法的原因是中描述的问题)。进程替换要求您的shell实际上是bash,而不是以名称
sh
开头。检查您是否正在运行
sh您的脚本
或以
开头/bin/sh
shebang.(选择流程替代方法的原因是中描述的问题)。