【问题标题】:Manipulate JSON with jq用 jq 操作 JSON
【发布时间】:2016-12-15 14:15:46
【问题描述】:

我已经关注了 json 文件。我正在尝试使用 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所以我尝试了以下

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

谢谢

【问题讨论】:

    标签: json jq


    【解决方案1】:

    原始问题询问如何更改多个“属性”。这样做而不重复的关键是|=。这是一个插图:

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

    就地更新

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

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

    【讨论】:

      【解决方案2】:

      设法做到这一点

      jq '.items[0].properties."hfile.block.cache.size"="0.2"' initial.json > 1.json
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 1970-01-01
        相关资源
        最近更新 更多