简体   繁体   中英

groovy - extracting section from json

I need to extract from JSON such a section:

[
name: Hadoop: service=hiveserver2,
name=UgiMetrics,
]

there is a lot of other things below and above, but I need this only one section..

I am using a piece of code now:

import groovy.json.JsonSlurper
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import java.security.PrivilegedAction;



def getJson(String URI, String creds){
    def connection = new URL( URI ).openConnection() as HttpURLConnection;
    connection.setRequestProperty( 'User-Agent', 'Logicmonitor' );
    connection.setRequestProperty( 'Authorization', 'Basic '+ creds.bytes.encodeBase64());
    def json = new JsonSlurper().parseText(connection.inputStream.text.replaceAll("NaN","null"));
    //return connection.inputStream.text.replaceAll("NaN","null")
    return json;
}



def json = getJson('http://192.30.110.13:10002/jmx' ,'a:b');

println json

json.'beans'.each { entry ->
    if (entry.name.contains("name: Hadoop: service=hiveserver2")) {
        println entry
    }
}

but in result I have a lot of things below and above, how can I extract this only one section?

this is answer for dagget comment, I need to extract metrics for hive only

rootLogger.level=${
  sys: hive.log.level
}rootLogger.appenderRefs=rootrootLogger.appenderRef.root.ref=${
  sys: hive.root.logger
},
ConfigName: HiveLog4j2,
ConfigClassName: org.apache.logging.log4j.core.config.properties.PropertiesConfiguration,
ConfigFilter: null,
ConfigProperties: {
  hostName=hdpmaster1.myhome.net,
  contextName=AsyncContext@764c12b6
}
],
[
name: metrics: name=memory.non-heap.used,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 108875416
],
[
name: metrics: name=threads.new.count,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 0
],
[
name: metrics: name=memory.heap.used,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 294339144
],
[
name: org.apache.logging.log4j2: type=AsyncContext@764c12b6,
component=ContextSelector,
modelerType: org.apache.logging.log4j.core.jmx.ContextSelectorAdmin,
ImplementationClassName: org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
],
[
name: java.lang: type=OperatingSystem,
modelerType: sun.management.OperatingSystemImpl,
MaxFileDescriptorCount: 32000,
OpenFileDescriptorCount: 653,
CommittedVirtualMemorySize: 28092108800,
FreePhysicalMemorySize: 12110716928,
FreeSwapSpaceSize: 2147479552,
ProcessCpuLoad: 0.0009703324018562333,
ProcessCpuTime: 3358970000000,
SystemCpuLoad: 0.0967370997772646,
TotalPhysicalMemorySize: 29490122752,
TotalSwapSpaceSize: 2147479552,
Arch: amd64,
SystemLoadAverage: 0.45,
Version: 3.10.0-1062.18.1.el7.x86_64,
AvailableProcessors: 8,
Name: Linux,
ObjectName: java.lang: type=OperatingSystem
],
[
name: Hadoop: service=hiveserver2,
name=MetricsSystem,
sub=Control,
modelerType: org.apache.hadoop.metrics2.impl.MetricsSystemImpl
],
[
name: org.apache.logging.log4j2: type=AsyncContext@764c12b6,
component=Loggers,
name=Datastore,
modelerType: org.apache.logging.log4j.core.jmx.LoggerConfigAdmin,
Name: Datastore,
Filter: null,
Level: ERROR,
AppenderRefs: [

],
Additive: true,
IncludeLocation: false
],
[
name: Hadoop: service=hiveserver2,
name=MetricsSystem,
sub=Stats,
modelerType: MetricsSystem,
sub=Stats,
tag.Context: metricssystem,
tag.Hostname: hdpmaster1.myhome.net,
NumActiveSources: 2,
NumAllSources: 2,
NumActiveSinks: 1,
NumAllSinks: 0,
Sink_timelineNumOps: 51775,
Sink_timelineAvgTime: 2.0,
Sink_timelineDropped: 0,
Sink_timelineQsize: 0,
SnapshotNumOps: 155325,
SnapshotAvgTime: 0.0,
PublishNumOps: 51775,
PublishAvgTime: 1.0,
DroppedPubAll: 0
],
[
name: java.lang: type=MemoryPool,
name=G1OldGen,
modelerType: sun.management.MemoryPoolImpl,
CollectionUsage: [
  committed: 0,
  init: 432013312,
  max: 25019023360,
  used: 0
],
CollectionUsageThreshold: 0,
CollectionUsageThresholdCount: 0,
MemoryManagerNames: [
  G1OldGeneration
],
PeakUsage: [
  committed: 432013312,
  init: 432013312,
  max: 25019023360,
  used: 123886176
],
Usage: [
  committed: 167772160,
  init: 432013312,
  max: 25019023360,
  used: 122372680
],
UsageThreshold: 0,
UsageThresholdCount: 0,
CollectionUsageThresholdExceeded: false,
CollectionUsageThresholdSupported: true,
UsageThresholdExceeded: false,
UsageThresholdSupported: true,
Name: G1OldGen,
Type: HEAP,
Valid: true,
ObjectName: java.lang: type=MemoryPool,
name=G1OldGen
],
[
name: java.lang: type=GarbageCollector,
name=G1OldGeneration,
modelerType: sun.management.GarbageCollectorImpl,
LastGcInfo: null,
CollectionCount: 0,
CollectionTime: 0,
MemoryPoolNames: [
  G1EdenSpace,
  G1SurvivorSpace,
  G1OldGen
],
Name: G1OldGeneration,
Valid: true,
ObjectName: java.lang: type=GarbageCollector,
name=G1OldGeneration
],
[
name: java.lang: type=MemoryManager,
name=MetaspaceManager,
modelerType: sun.management.MemoryManagerImpl,
MemoryPoolNames: [
  Metaspace,
  CompressedClassSpace
],
Name: MetaspaceManager,
Valid: true,
ObjectName: java.lang: type=MemoryManager,
name=MetaspaceManager
],
[
name: metrics: name=threads.daemon.count,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 38
],
[
name: metrics: name=memory.total.max,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 25019023359
],
[
name: metrics: name=threads.count,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 50
],
[
name: org.apache.logging.log4j2: type=AsyncContext@764c12b6,
component=Appenders,
name=console,
modelerType: org.apache.logging.log4j.core.jmx.AppenderAdmin,
Name: console,
Filter: null,
IgnoreExceptions: true,
Layout: %d{
  yy/MM/ddHH: mm: ss
}[
  %t
]: %p%c{
  2
}: %m%n,
ErrorHandler: org.apache.logging.log4j.core.appender.DefaultErrorHandler@3d10a435
],
[
name: metrics: name=memory.pools.G1-Eden-Space.usage,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 0.5671641791044776
],
[
name: metrics: name=hs2_open_sessions,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 0
],
[
name: java.lang: type=Memory,
modelerType: sun.management.MemoryImpl,
HeapMemoryUsage: [
  committed: 461373440,
  init: 461373440,
  max: 25019023360,
  used: 294339144
],
NonHeapMemoryUsage: [
  committed: 111140864,
  init: 2555904,
  max: -1,
  used: 108875416
],
ObjectPendingFinalizationCount: 0,
Verbose: true,
ObjectName: java.lang: type=Memory
],
[
name: metrics: name=memory.total.used,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 403214560
],
[
name: metrics: name=memory.non-heap.committed,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 111140864
],
[
name: metrics: name=hs2_avg_active_session_time,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: null
],
[
name: Hadoop: service=hiveserver2,
name=UgiMetrics,
modelerType: UgiMetrics,
tag.Context: ugi,
tag.Hostname: hdpmaster1.myhome.net,
LoginSuccessNumOps: 12,
LoginSuccessAvgTime: 8.0,
LoginFailureNumOps: 0,
LoginFailureAvgTime: 0.0,
GetGroupsNumOps: 0,
GetGroupsAvgTime: 0.0,
RenewalFailuresTotal: 0,
RenewalFailures: 0
],
[
name: metrics: name=threads.waiting.count,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 18
],
[
name: metrics: name=classLoading.unloaded,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 0
],
[
name: metrics: name=memory.heap.committed,
modelerType: com.codahale.metrics.JmxReporter$JmxGauge,
Value: 461373440
]
]
]

in the following code contains a short example of your json.

import groovy.json.*

def json = new JsonSlurper().parseText('''
{
    "beans": [
        {
            "name": "metrics:name=buffers.mapped.count",
            "modelerType": "com.codahale.metrics.JmxReporter$JmxGauge",
            "Value": 0
        },
        {
            "name": "metrics:name=gc.G1-Old-Generation.time",
            "modelerType": "com.codahale.metrics.JmxReporter$JmxGauge",
            "Value": 0
        },
        {
            "name": "Hadoop:service=hiveserver2,name=hiveserver2",
            "modelerType": "hiveserver2",
            "tag.rate_unit": "events/second",
            "tag.duration_unit": "milliseconds",
            "tag.Hostname": "hdpmaster1.myhome.net"
        },
        {
            "name": "metrics:name=threads.waiting.count",
            "modelerType": "com.codahale.metrics.JmxReporter$JmxGauge",
            "Value": 16
        },
        {
            "name": "Hadoop:service=hiveserver2,name=MetricsSystem,sub=Control",
            "modelerType": "org.apache.hadoop.metrics2.impl.MetricsSystemImpl"
        }
    ]
}
''')

def allHive = json.beans.findAll{e-> e.name.startsWith('Hadoop:service=hiveserver2') }

//if tou want to print the result as json
println new JsonBuilder(allHive).toPrettyString()

output:

[
    {
        "name": "Hadoop:service=hiveserver2,name=hiveserver2",
        "modelerType": "hiveserver2",
        "tag.rate_unit": "events/second",
        "tag.duration_unit": "milliseconds",
        "tag.Hostname": "hdpmaster1.myhome.net"
    },
    {
        "name": "Hadoop:service=hiveserver2,name=MetricsSystem,sub=Control",
        "modelerType": "org.apache.hadoop.metrics2.impl.MetricsSystemImpl"
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM