Tech and Media Labs
This site uses cookies to improve the user experience.




Grid Ops Java - Getting Started

Jakob Jenkov
Last update: 2017-03-18

This text shows how to get started using Grid Ops for Java. It is quite simple, actually. Getting started requires the following steps:

  • Include Grid Ops for Java in your Java project.
  • Create the Grid Ops components your application need.

That is all there is to it. Both steps will be described in the following sections.

Include Grid Ops in Your Project

Including the Grid Ops JAR file in your Java project can be done in several ways:

Maven Dependency

You can include Grid Ops in your Java project via your project's Maven POM file by adding the Grid Ops Maven dependency to your project's POM file. Here is the Grid Ops Maven dependency:

<dependency>
  <groupId>com.nanosai</groupId>
  <artifactId>grid-ops</artifactId>
  <version>0.4.0</version>
</dependency>

Remember to replace the version number with the version of Grid Ops you intend to use.

Adding Grid Ops JAR File to Classpath

If your project is not using Maven, you can download the JAR file for the release of Grid Ops from the Grid Ops GitHub repository under releases.

Create Grid Ops Components

Once you have included the Grid Ops JAR file in your Maven project, you can start creating Grid Ops components. Most important Grid Ops components can be created via the class com.nanosai.gridops.GridOps. The following sections show a few examples of how to create Grid Ops components via the GridOps class.

Creating an IonWriter

Here is an example creating an IonWriter and write some ION data to a byte array:

IonWriter writer = GridOps.ionWriter();

byte[] destination = new byte[1024];
writer.setDestination(destination, 0, destination.length);

ionWriter.writeUtf8("A UTF-8 field");
ionWriter.writeInt64(123456);

You can read more about the IonWriter here:
IonWriter .

Creating an IonReader

Once you have some binary ION data in a byte array, you can read it with an IonReader. Here is how you create an IonReader that can read the ION data written to the above byte array:

IonReader ionReader = GridOps.ionReader();
ionReader.setSource(destination, 0, ionWriter.index);

while(ionReader.hasNext()){
    ionReader.next();
    ionReader.parse();

    int fieldType   = ionReader.fieldType;

    if(fieldType == IonFieldTypes.UTF_8){
        String value = ionReader.readUtf8String();
        System.out.println("Read UTF-8 string: " + value);
    } else if(fieldType == IonFieldTypes.INT_POS){
        long value = ionReader.readInt64();
        System.out.println("Read Int64: " + value);
    }
}

You can read more about the IonReader here:
IonReader .

Creating an IonObjectWriter

Grid Ops contains an IonObjectWriter which can serialize Java objects to ION. Here is how you create and use an IonObjectWriter via the GridOps class:

IonObjectWriter ionObjectWriter = GridOps.ionObjectWriter(Pojo.class);

Pojo pojo = new Pojo();
pojo.setFirstName("John");
pojo.setLastName("Doe");

byte[] destination = new byte[1024];
int length = ionObjectWriter.writeObject(pojo, 1, destination, 0);

You can read more about the IonObjectWriter here:
IonObjectWriter .

Creating an IonObjectReader

Grid Ops also contains an IonObjectReader which can deserialize ION data into Java objects. Here is how you create and use an IonObjectReader via the GridOps class:

IonObjectReader ionObjectReader = GridOps.ionObjectReader(Pojo.class);

byte[] source = new byte[1024];

Pojo pojo = (Pojo) ionObjectReader.read(source, 0);

You can read more about the IonObjectReader here:
IonObjectReader .

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC