Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question
0

How can I convert json to SpaceDocument

Presently, I am storing data in NOSQL DB in json format. Now, I want to store same json in GS. How can I convert same json to SpaceDocument. If I am correct, SpaceDocument is Key-value data structure. I would not like refactor my POJO-JSON code base.

asked 2017-02-04 06:56:28 -0600

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

Hi,

Please see the code snippet below on how to declare and transform a JSON document to a space document. The strategy in this example is simply to convert JSON to MAP and pass it to the Space Document Constructor.

https://docs.gigaspaces.com/xap121/do...

Although you define an Id, it is not a key-value store because you can access data without knowing the object's key, but by using rich SQL semantics with field level access. (location='NY' AND stockQuantity>50).

There is also nested fields as well so that if you object map/json model has multiple levels of depth. you can query on those properties directly without iterating the tree. (address.city='NYC' AND stockQuantity>50)

https://docs.gigaspaces.com/xap121/qu...

We also provide Semi Structured Data modeling as well as Structured and unstructured data types. You could essentially define some static properties but have dynamic portion for data variability. With this model you could parse is using Pojo/JSon mappers.

https://docs.gigaspaces.com/xap121/dy...

/****
     * We Need to Register a type by specifying a name, id, and routing field.
     * Routing is used to partition data across the grid, similar to a shard key 
     * This only needs to be done once 
     */
    final String PRODUCT_TYPE_NAME = "com.mycompany.organization.application.Product";

    SpaceTypeDescriptor typeDescriptor = new SpaceTypeDescriptorBuilder(PRODUCT_TYPE_NAME)
                .idProperty("id")
                .routingProperty("location")
                .addPropertyIndex("processed", SpaceIndexType.BASIC)
                .create();

    gigaSpace.getTypeManager().registerTypeDescriptor(typeDescriptor);

    /****
     * Example JSON payload containing the properties 
     * of the new Type. In this example we use jaxson object mapper.
     * You can use any parser you would like
     */
    String jsonPayload = "{\"id\":1, \"location\":\"usa\", \"processed\":false}";

    HashMap<String,Object> jsonProperties =
            new ObjectMapper().readValue(jsonPayload, HashMap.class);

    /****
     * Convert to a space document simply pass the hashmap to the 
     * in to the SpaceDocument Constructor along with the document type name
     * from above
     */
    SpaceDocument dataAsDocument = new SpaceDocument(PRODUCT_TYPE_NAME, jsonProperties);

    /***
     * Insert to the grid
     */
    gigaSpace.write(dataAsDocument);

    /***
     * To confirm the result. Read the document from the grid
     */
    SpaceDocument dataAsDocumentFromGrid = gigaSpace.read(new SQLQuery<SpaceDocument>(PRODUCT_TYPE_NAME, "id = ?", 1));

    /***
     * Map the Object back to JSON
     */
    String jsonFromGrid = new ObjectMapper().writeValueAsString(dataAsDocumentFromGrid);

    System.out.println(jsonFromGrid);

answered 2017-05-05 11:32:17 -0600

Michael Raney gravatar image

updated 2017-05-05 11:33:20 -0600

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-02-04 06:56:28 -0600

Seen: 65 times

Last updated: May 05