用jq操作JSON [英] Manipulate JSON with jq

查看:70
本文介绍了用jq操作JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了json文件.我正在尝试使用jq

I have following the json file. I am trying to update a couple of properties in this file using jq

{
  "href" : "http://localhost:8080/api/v1/clusters/ambarihbase/configurations?type=hbase-site&tag=TOPOLOGY_RESOLVED",
  "items" : [
  {
  "href" : "http://localhost:8080/api/v1/clusters/ambarihbase/configurations?type=hbase-site&tag=TOPOLOGY_RESOLVED",
  "tag" : "TOPOLOGY_RESOLVED",
  "type" : "hbase-site",
  "version" : 2,
  "Config" : {
    "cluster_name" : "ambarihbase",
    "stack_id" : "HDP-2.5"
  },
  "properties" : {
    "dfs.domain.socket.path" : "/var/lib/hadoop-hdfs/dn_socket",
    "dfs.support.append" : "false",
    "hbase.bucketcache.combinedcache.enabled" : "true",
    "hbase.bucketcache.ioengine" : "file:/mnt/hbase/cache.data",
    "hbase.bucketcache.percentage.in.combinedcache" : "",
    "hbase.bucketcache.size" : "81920",
    "hbase.bulkload.staging.dir" : "/apps/hbase/staging",
    "hbase.client.keyvalue.maxsize" : "1048576",
    "hbase.client.retries.number" : "35",
    "hbase.client.scanner.caching" : "100",
    "hbase.cluster.distributed" : "true",
    "hbase.coprocessor.master.classes" : "",
    "hbase.coprocessor.region.classes" : "org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint",
    "hbase.defaults.for.version.skip" : "true",
    "hbase.fs.shutdown.hook.wait" : "600000",
    "hbase.hregion.majorcompaction" : "0",
    "hbase.hregion.majorcompaction.jitter" : "0.50",
    "hbase.hregion.max.filesize" : "3221225472",
    "hbase.hregion.memstore.block.multiplier" : "4",
    "hbase.hregion.memstore.flush.size" : "134217728",
    "hbase.hregion.memstore.mslab.enabled" : "true",
    "hbase.hstore.blockingStoreFiles" : "100",
    "hbase.hstore.compaction.max" : "10",
    "hbase.hstore.compaction.max.size" : "10737418240",
    "hbase.hstore.compactionThreshold" : "3",
    "hbase.local.dir" : "${hbase.tmp.dir}/local",
    "hbase.master.distributed.log.splitting" : "true",
    "hbase.master.info.bindAddress" : "0.0.0.0",
    "hbase.master.info.port" : "16010",
    "hbase.master.port" : "16000",
    "hbase.master.ui.readonly" : "false",
    "hbase.region.server.rpc.scheduler.factory.class" : "org.apache.hadoop.hbase.ipc.PhoenixRpcSchedulerFactory",
    "hbase.regionserver.global.memstore.size" : "0.4",
    "hbase.regionserver.handler.count" : "100",
    "hbase.regionserver.hlog.blocksize" : "134217728",
    "hbase.regionserver.info.port" : "16030",
    "hbase.regionserver.optionalcacheflushinterval" : "0",
    "hbase.regionserver.port" : "16020",
    "hbase.regionserver.wal.codec" : "org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec",
    "hbase.rest.port" : "8090",
    "hbase.rootdir" : "/hbase",
    "hbase.rpc.protection" : "authentication",
    "hbase.rpc.timeout" : "90000",
    "hbase.rs.cacheblocksonwrite" : "true",
    "hbase.security.authentication" : "simple",
    "hbase.security.authorization" : "false",
    "hbase.shutdown.hook" : "true",
    "hbase.superuser" : "hbase",
    "hbase.tmp.dir" : "/tmp/hbase-${user.name}",
    "hbase.zookeeper.property.clientPort" : "2181",
    "hbase.zookeeper.useMulti" : "true",
    "hfile.block.cache.size" : "0.40",
    "hfile.index.block.max.size" : "131072",
    "io.storefile.bloom.block.size" : "131072",
    "phoenix.functions.allowUserDefinedFunctions" : "true",
    "phoenix.query.timeoutMs" : "60000",
    "zookeeper.recovery.retry" : "6",
    "zookeeper.session.timeout" : "120000",
    "zookeeper.znode.parent" : "/hbase-unsecure"
  }
 }
]
}

我想更新hbase.hregion.max.filesize,所以我尝试了以下

I want to update hbase.hregion.max.filesize so I tried the following

cat initial.json | 
      jq 'to_entries | 
           map(if .items[0].properties.key == "hfile.block.cache.size" 
              then . + {"value":"0.20"} 
              else . 
              end
             ) | 
          from_entries'

哪个输出原始文件,但不输出更新的hfile.block.cache.size

Which outputs the original file but not with updated hfile.block.cache.size

谢谢

推荐答案

原始问题询问如何更改多个属性"之一.无需重复的操作关键是|=.这是一个例子:

The original question asks how to alter more than one of the "properties". The key to doing so without being repetitive is |=. Here's an illustration:

.items[0].properties |=
  ( .["hfile.block.cache.size"] = "newvalue1"
  | .["hbase.hregion.max.filesize"] = "newvalue2" )

就地更新

最初的问题还提到了更新文件中的属性.避免创建显式临时文件的一种可能性是使用sponge(例如brew install moreutils),大致如下:

In-place updates

The original question also mentioned updating properties within a file. One possibility that avoids having to create an explicit temporary file is to use sponge (e.g. brew install moreutils), along the lines of:

$ jq .... input.json | sponge input.json 

这篇关于用jq操作JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆