Reputation: 1572
I am using Jackon Json library to parse JSON response returned by EclasticSearch. Here is the JSON structure. You can see that "nodes" is a class and "mRyA6hF0QAqIvggAbFWBsw" is the NodeID which is dynamic and allocated by ES so I cannot make concrete nested class. I am using Map<String, Nodes>
for it.
{
"ok" : true,
"cluster_name" : "elasticsearch",
"nodes" : {
"mRyA6hF0QAqIvggAbFWBsw" : {
"name" : "Fagin",
"transport_address" : "inet[/192.16.12.18:9300]",
"hostname" : "anand-K53SD",
"version" : "0.90.9",
"http_address" : "inet[/192.16.12.18:9200]",
"settings" : {
"path.home" : "/home/anand/logmanagementtools/elasticsearch/elasticsearch-0.90.9",
"name" : "Fagin",
"cluster.name" : "elasticsearch",
"path.logs" : "/home/anand/logmanagementtools/elasticsearch/elasticsearch-0.90.9/logs"
},
"os" : {
"refresh_interval" : 1000,
"available_processors" : 4,
"cpu" : {
"vendor" : "Intel",
"model" : "Core(TM) i3-2350M CPU @ 2.30GHz",
"mhz" : 2300,
"total_cores" : 4,
"total_sockets" : 4,
"cores_per_socket" : 16,
"cache_size" : "3kb",
"cache_size_in_bytes" : 3072
},
"mem" : {
"total" : "3.6gb",
"total_in_bytes" : 3912163328
},
"swap" : {
"total" : "5gb",
"total_in_bytes" : 5369749504
}
},
"process" : {
"refresh_interval" : 1000,
"id" : 5291,
"max_file_descriptors" : 4096,
"mlockall" : false
},
"jvm" : {
"pid" : 5291,
"version" : "1.7.0_45",
"vm_name" : "Java HotSpot(TM) 64-Bit Server VM",
"vm_version" : "24.45-b08",
"vm_vendor" : "Oracle Corporation",
"start_time" : 1390103541852,
"mem" : {
"heap_init" : "256mb",
"heap_init_in_bytes" : 268435456,
"heap_max" : "990.7mb",
"heap_max_in_bytes" : 1038876672,
"non_heap_init" : "23.1mb",
"non_heap_init_in_bytes" : 24313856,
"non_heap_max" : "130mb",
"non_heap_max_in_bytes" : 136314880,
"direct_max" : "990.7mb",
"direct_max_in_bytes" : 1038876672
}
},
"thread_pool" : {
"generic" : {
"type" : "cached",
"keep_alive" : "30s"
},
"index" : {
"type" : "fixed",
"min" : 4,
"max" : 4,
"queue_size" : "200"
},
"get" : {
"type" : "fixed",
"min" : 4,
"max" : 4,
"queue_size" : "1k"
},
"snapshot" : {
"type" : "scaling",
"min" : 1,
"max" : 2,
"keep_alive" : "5m"
},
"merge" : {
"type" : "scaling",
"min" : 1,
"max" : 2,
"keep_alive" : "5m"
},
"suggest" : {
"type" : "fixed",
"min" : 4,
"max" : 4,
"queue_size" : "1k"
},
"bulk" : {
"type" : "fixed",
"min" : 4,
"max" : 4,
"queue_size" : "50"
},
"optimize" : {
"type" : "fixed",
"min" : 1,
"max" : 1
},
"warmer" : {
"type" : "scaling",
"min" : 1,
"max" : 2,
"keep_alive" : "5m"
},
"flush" : {
"type" : "scaling",
"min" : 1,
"max" : 2,
"keep_alive" : "5m"
},
"search" : {
"type" : "fixed",
"min" : 12,
"max" : 12,
"queue_size" : "1k"
},
"percolate" : {
"type" : "fixed",
"min" : 4,
"max" : 4,
"queue_size" : "1k"
},
"management" : {
"type" : "scaling",
"min" : 1,
"max" : 5,
"keep_alive" : "5m"
},
"refresh" : {
"type" : "scaling",
"min" : 1,
"max" : 2,
"keep_alive" : "5m"
}
},
"network" : {
"refresh_interval" : 5000,
"primary_interface" : {
"address" : "192.16.12.18",
"name" : "wlan0",
"mac_address" : "00:08:CA:F3:F6:AB"
}
},
"transport" : {
"bound_address" : "inet[/0:0:0:0:0:0:0:0:9300]",
"publish_address" : "inet[/192.16.12.18:9300]"
},
"http" : {
"bound_address" : "inet[/0:0:0:0:0:0:0:0:9200]",
"publish_address" : "inet[/192.16.12.18:9200]",
"max_content_length" : "100mb",
"max_content_length_in_bytes" : 104857600
},
"plugins" : [ {
"name" : "paramedic",
"description" : "No description found for paramedic.",
"url" : "/_plugin/paramedic/",
"jvm" : false,
"site" : true
}, {
"name" : "bigdesk",
"description" : "No description found for bigdesk.",
"url" : "/_plugin/bigdesk/",
"jvm" : false,
"site" : true
}, {
"name" : "head",
"description" : "No description found for head.",
"url" : "/_plugin/head/",
"jvm" : false,
"site" : true
}, {
"name" : "HQ",
"description" : "No description found for HQ.",
"url" : "/_plugin/HQ/",
"jvm" : false,
"site" : true
} ]
}
}
}
Here is my code to parse this JSON
package com.esmon.pojo;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
public class NodeInfo {
private String ok;
private String cluster_name;
private Nodes nodes;
public Nodes getNodes() {
return nodes;
}
public void setNodes(Nodes nodes) {
this.nodes = nodes;
}
public String getOk() {
return ok;
}
public void setOk(String ok) {
this.ok = ok;
}
public String getCluster_name() {
return cluster_name;
}
public void setCluster_name(String cluster_name) {
this.cluster_name = cluster_name;
}
@SuppressWarnings("rawtypes")
public void printInfo() {
System.out.println("------Nodes Information------");
System.out.println("Ok: " + ok);
System.out.println("Cluster Name: " + cluster_name);
nodes.printnfo();
}
}
class Nodes {
private Map<String , NodeObject> allNodes = new HashMap<String , NodeObject>();
@JsonAnyGetter
public Map<String , NodeObject> any() {
return allNodes;
}
@JsonAnySetter
public void set(String name, NodeObject value) {
allNodes.put(name, value);
}
@SuppressWarnings("rawtypes")
public void printnfo() {
System.out.println(allNodes.size());
for (Map.Entry entry: allNodes.entrySet()) {
System.out.println("Node ID: " + entry.getKey());
}
}
}
class NodeObject {
private String name;
private String transport_address;
private String hostname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTransport_address() {
return transport_address;
}
public void setTransport_address(String transport_address) {
this.transport_address = transport_address;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
}
When i check the Map size its returning 0. Please point where I am doing wrong.
Upvotes: 0
Views: 514
Reputation: 76898
The problem is that your Java Nodes
class contains a field that is a Map<String, NodeObject>
. Your JSON defines "nodes" as an object (Map<String, NodeObject>
).
Your NodeInfo
class should define:
private Map<String, NodeObject> nodes;
And you can scrap the Nodes
class. Once you've done that, the following will work
ObjectMapper mapper = new ObjectMapper();
NodeInfo info = mapper.readValue(jsonString, NodeInfo.class);
Upvotes: 2