Working With JSON Document in Gemfire Cache
A quick tutorial, with code, describing how to store a JSON document in a Gemfire cache, from the very first step of setting up the environment.
Join the DZone community and get the full member experience.
Join For FreeJSON Document Store in Gemfire Cache.
Let us consider below JSON needs to stored in Gemfire Cache.
x
{
"firstName" : "Smith",
"lastName" : "Dagg",
"age" : 31,
"address" : {
"streetAddress" : "1930 Apple Dr",
"city" : "Wilmington",
"state" : "DE",
"postalCode" : "12345"
},
"phoneNumber" : [ {
"type" : "mobile",
"number" : "123 456 1234"
}
]
}
Let us set up the environment; it involves Server and Client setup.
The server configuration looks like below.
cache_server.xml
xxxxxxxxxx
<?xml version="1.0" encoding="UTF-8"?>
<cache
xmlns="http://geode.apache.org/schema/cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd"
version="1.0">
<cache-server port="40404"></cache>
<region name="jsonPersonRegion">
<region-attributes refid="REPLICATE" ></region>
</region>
</cache>
The cache server configures a cache to serve caching clients. The Region 'jsonPersonRegion' also is configured with a loader.
Above Cache has been configured with port 40404, region name is 'jsonPersonRegion' and region is REPLICATED type. If data is in sync with all the nodes then it is REPLICATED.
Below is how the client is setup:
client.xml
xxxxxxxxxx
<?xml version="1.0" encoding="UTF-8"?>
<client-cache
xmlns="http://geode.apache.org/schema/cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://geode.apache.org/schema/cache
http://geode.apache.org/schema/cache/cache-1.0.xsd"
version="1.0">
<pool name="serverPool">
<locator host="hostA" port="40404"/>
</pool>
<region name=" jsonPersonRegion" refid="REPLICATE"/>
</client-cache>
Client cache configures a region as a client region in a client/server cache. The region's pool connects to the cache server.
Below is the cache client code which stores the above json document and does query on the region.
JSONGemFireClient.java
xxxxxxxxxx
import java.util.List;
import org.json.simple.JSONObject;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.pdx.JSONFormatter;
import com.gemstone.gemfire.pdx.PdxInstance;
public class JSONGemFireClient
{
public static final String REGION_PERSON = " jsonPersonRegion";
public ClientCache cache = null;
personJson ="{\"firstName\":\"Smith\",\"lastName\":\"Dagg\",\"age\":31,\"address\":{\"streetAddress\":\"1930 Apple Dr\",\"city\":\"Wilmington\",\"state\":\"DE\",\"postalCode\":\"12345\"},\"phoneNumber\":[{\"type\":\"mobile\",\"number\":\"123 456 1234\"}]}"
public JSONGemFireClient()
{
cache = new ClientCacheFactory()
.set("name", "JSONClient")
.set("cache-xml-file", "xml/client.xml")
.create();
}
"unchecked") (
public void run() throws Exception
{
JSONObject obj = null;
Log.info("Connecting to the distributed system and creating the cache.");
// Get the region
Region<String, PdxInstance> personregion =cache.getRegion(REGION_PERSON);
Log.info("Example region \"" + personregion.getFullPath() + "\" created is cache.");
//Store string key, value pair in region
personregion.put(“person1”,JSONFormatter.fromJSON(personString.toJSONString();
// Query person region
PdxInstance personinfo = personregion.get(“person1”);
log.info(JSONFormatter.toJSON(personinfo));
personFirstName = personinfo.getValue(“firstName”);
log.info(“First Name is “, personFirstName);
cache.close();
}
}
The JSONFormatter API allows you to put JSON formatted documents into regions and retrieve them later by storing the documents internally as PdxInstances.
You can then use the JSONFormatter
to convert the PdxInstance results back into the JSON document.
fromJSON:
Creates a PdxInstance from a JSON byte array. Returns the PdxInstance
Creates a PdxInstance from a JSON string. Returns the PdxInstance.
toJSON
Reads a PdxInstance and returns a JSON string.
toJSONByteArray
Reads a PdxInstance and returns a JSON byte array.
Opinions expressed by DZone contributors are their own.
Comments