2015-07-14 11 views
7

Ho un messaggio JSON con un array in un array. Voglio dividere che in più eventi:Come dividere un array JSON all'interno di un oggetto

{ 
"type": "monitor", 
"server": "10.111.222.333", 
"host": "abc.de", 
"bean": [{ 
    "name": "beanName1", 
    "reseted": "2015-06-05T15:10:00.192Z", 
    "method": [{ 
     "name": "getAllXY", 
     "count": 5, 
     "min": 3, 
     "max": 5 
    }, 
    { 
     "name": "getName", 
     "count": 4, 
     "min": 2, 
     "max": 4 
    }] 
    }, 
    { 
    "name": "beanName2", 
    "reseted": "2015-06-05T15:10:00.231Z", 
    "method": [{ 
     "name": "getProperty", 
     "count": 4, 
     "min": 3, 
     "max": 3 
    }] 
    }, 
    { 
    "name": "beanName3", 
    "reseted": "2015-06-05T15:10:00.231Z" 
    }] 
} 

Usando un filtro per dividere "fagiolo":

input { 
    stdin { 
    codec => "json" 
    } 
} 

filter { 
    split { 
    field => "bean" 
    } 
} 

output { 
    stdout{codec => "json"} 
} 

funziona bene:

{"type":"monitor", 
"server":"10.111.222.333", 
"host":"abc.de", 
"bean":{ 
    "name":"beanName1", 
    "reseted":"2015-06-05T15:10:00.192Z", 
    "method":[{ 
    "name":"getAllXY", 
    "count":5, 
    "min":3, 
    "max":5 
    },{ 
    "name":"getName", 
    "count":4, 
    "min":2, 
    "max":4 
    }]}, 
"@version":"1", 
"@timestamp":"2015-07-14T09:21:18.326Z" 
} 

{"type":"monitor", 
"server":"10.111.222.333", 
"host":"abc.de", 
"bean":{ 
    "name":"beanName2", 
    "reseted":"2015-06-05T15:10:00.231Z", 
    "method":[{ 
    "name":"getProperty", 
    "count":4, 
    "min":3, 
    "max":3 
    }]}, 
"@version":"1", 
"@timestamp":"2015-07-14T09:21:18.326Z" 
} 

    ... 

per separare anche i "metodi" , Ho aggiunto un altro filtro split:

split { 
    field => "bean" 
    } 
    split { 
    field => "bean.method" 
    } 

Ma in questo modo ottengo solo un messaggio di errore:

Exception in filterworker {"exception"=>#LogStash::ConfigurationError: Only String and Array types are splittable. field:bean.method is of type = NilClass

Non riesco ad accedere al "metodo" di matrice all'interno del "fagiolo" oggetto. Ho provato diverse notazioni senza fortuna. È possibile accedere alla matrice, forse non è ancora supportato?

risposta

4

Il seguente codice dovrebbe fare quello che vuoi e tornare un evento per ogni metodo:

filter { 
    if !("splitted_beans" in [tags]) { 
     json { 
      source => "message" 
     } 
     split { 
      field => "bean" 
      add_tag => ["splitted_beans"] 
     } 
    } 

    if ("splitted_beans" in [tags] and [bean][method]) { 
     split { 
      field => "bean[method]" 
     } 
    } 
} 

il secondo controlla condizione, se il primo metodo ha avuto successo e se un metodo esiste dentro il bean. Quindi funziona anche per i bean senza metodi.

+1

Funziona, ma ho fagioli senza metodi. Così ho aggiunto _if [bean] [metodo] {..._ e ora va bene, grazie mille. – joerno

+0

Perfetto, ho aggiunto la condizione '[bean] [metodo]' alla mia risposta. – hurb

+0

@hurb Cosa succede se il "bean" ha un solo elemento? In questo caso, questo codice non funziona –

Problemi correlati