【问题标题】:Xidel: Parse attributes into new objectXidel:将属性解析为新对象
【发布时间】:2015-11-10 02:21:05
【问题描述】:

Given 是来自任何 Java 虚拟机的详细 GC 日志(可以是任何 xml,因此不使用 java 标记):

<?xml version="1.0" ?>

<verbosegc version="versioninformation">

<af type="nursery" id="49383" timestamp="Jan 01 01:34:54 2015" intervalms="33.821">
  <tenured freebytes="769243504" totalbytes="1610416128" percent="47" >
    <soa freebytes="198858272" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
  <gc></gc>
  <tenured freebytes="768800232" totalbytes="1610416128" percent="47" >
    <soa freebytes="198415" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
</af>

<af type="nursery" id="49384" timestamp="Jan 01 01:35:54 2015" intervalms="40.877">
  <tenured freebytes="768800232" totalbytes="1610416128" percent="47" >
    <soa freebytes="198415" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
  <gc></gc>
  <tenured freebytes="768320928" totalbytes="1610416128" percent="47" >
    <soa freebytes="197935696" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
</af>

所以,我想创建一个新对象,该对象在每个垃圾回收周期中重复使用时间戳和使用的字节(总减去空闲)。计算工作得很好,但输出没有。这是我期望得到的:

[
  {
    "timestamp": "Jan 01 01:34:54 2015",
    "used": 8.41172624E8
  },
  {
    "timestamp": "Jan 01 01:35:54 2015",
    "used": 8.41615896E8
  },
]

我尝试了这个命令行,可悲的是它创建了一个空时间戳和一长串堆信息:

xidel --input-format=xml   -e "//af/tenured[1]/(heap:={used:=(@totalbytes - @freebytes):timestamp:=@timestamp})" gc.log --output-format=json-wrapped

输出如下所示:

[
  {
    "timestamp": [null],
    "used": [8.41172624E8, 8.41615896E8]
  }
]

显然不是我所期望的。

【问题讨论】:

    标签: json xml shell xpath xidel


    【解决方案1】:

    您实际上可以在提取表达式中编写整个 JSON 结构,并且不需要输出格式:

    xidel --input-format=xml   -e "[ //af/tenured[1]/{'used':(@totalbytes - @freebytes),'timestamp':../@timestamp} ]" gc.log 
    

    【讨论】:

    • 这正是我想要的!非常感谢。如果您认为这个问题有用,我会很高兴赞成。 :-)
    【解决方案2】:

    正确的想法是不要将对象分配给变量(heap:={}),而是直接在 xpath 中使用 json 表示法。第三,timestamp比tenured高一个级别,所以我把../放在前面。

    所以这个可行:

    xidel --input-format=xml   -e "//af/tenured[1]/('used':(@totalbytes - @freebytes),'timestamp':../@timestamp)" gc.log --output-format=json-wrapped
    

    输出:

    [
        [
            {
                "timestamp": "Jan 01 01:34:54 2015",
                "used": 841172624.0
            },
            {
                "timestamp": "Jan 01 01:35:54 2015",
                "used": 841615896.0
            },
        ]
    ]
    

    它提供了一个额外的数组,但我可以忍受。至少它是一个稳定的输出和可解析的。

    【讨论】:

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