Skip to content

Instantly share code, notes, and snippets.

@dkuppitz
Created March 13, 2017 19:38
Show Gist options
  • Save dkuppitz/67f1d446a72366cca234b062810cadfb to your computer and use it in GitHub Desktop.
Save dkuppitz/67f1d446a72366cca234b062810cadfb to your computer and use it in GitHub Desktop.
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
import org.apache.tinkerpop.gremlin.structure.io.GraphWriter;
import org.apache.tinkerpop.gremlin.structure.io.IoCore;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.apache.tinkerpop.gremlin.util.TimeUtil;
import org.javatuples.Pair;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* @author Daniel Kuppitz (http://gremlin.guru)
*/
public class App {
public static void main(String[] args) throws Exception {
final Random random = new Random();
final Graph graph = TinkerGraph.open();
final GraphWriter writer = IoCore.gryo().graph(graph).create().writer().create();
final List<Vertex> vertices = new ArrayList<>();
for (int i = 0; i < 100; i++) {
final Vertex v;
vertices.add(v = graph.addVertex(T.id, i, "name", "#" + Integer.toString(i)));
if (i > 0 && random.nextDouble() >= 0.5) {
v.addEdge("link", vertices.get(random.nextInt(vertices.size())));
}
}
System.out.println(graph);
// serialize the graph and send it to a remote script
final Pair<Double, byte[]> serializationResult = TimeUtil.clockWithResult(100, () -> {
final byte[] bytes;
try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024)) {
writer.writeGraph(byteArrayOutputStream, graph);
byteArrayOutputStream.flush();
bytes = byteArrayOutputStream.toByteArray();
return bytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
System.out.println(String.format("Time to serialize the graph: %.2f ms, size of serialized graph: %d bytes",
serializationResult.getValue0(), serializationResult.getValue1().length));
// receive the serialized graph, deserialize it and add all vertices and edges to an existing graph
final Graph dseGraph = TinkerGraph.open(); // this should be a DseGraph using custom ids to allow upserts
final byte[] bytes = serializationResult.getValue1();
final Double deserializationResult = TimeUtil.clock(100, () -> {
try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
final Map<Vertex, Vertex> vMap = new HashMap<>();
final Graph tmpGraph = TinkerGraph.open();
final GraphReader reader = IoCore.gryo().graph(tmpGraph).create().reader().create();
reader.readGraph(byteArrayInputStream, tmpGraph);
final Iterator<Vertex> vertexIterator = tmpGraph.vertices();
while (vertexIterator.hasNext()) {
final Vertex v = vertexIterator.next();
final List<Object> properties = new ArrayList<>();
properties.add(T.label);
properties.add(v.label());
final Iterator<VertexProperty<Object>> propertyIterator = v.properties();
while (propertyIterator.hasNext()) {
final Property p = propertyIterator.next();
properties.add(p.key());
properties.add(p.value());
}
vMap.put(v, dseGraph.addVertex(properties.toArray()));
}
final Iterator<Edge> edgeIterator = tmpGraph.edges();
while (edgeIterator.hasNext()) {
final Edge e = edgeIterator.next();
final Vertex outV = vMap.get(e.outVertex());
final Vertex inV = vMap.get(e.inVertex());
final List<Object> properties = new ArrayList<>();
final Iterator<Property<Object>> propertyIterator = e.properties();
while (propertyIterator.hasNext()) {
final Property p = propertyIterator.next();
properties.add(p.key());
properties.add(p.value());
}
outV.addEdge(e.label(), inV, properties.toArray());
}
} catch (IOException e) {
e.printStackTrace();
}
});
System.out.println(String.format("Time to deserialize and merge the graph: %.2f ms.", deserializationResult));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment