Java Code Examples for org.apache.tinkerpop.gremlin.structure.io.GraphReader#readGraph()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.io.GraphReader#readGraph() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    sampleGraph1.addVertex(T.id, 100L, "name", "kevin");
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example 2
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);

    sampleGraph1.addVertex(T.id, 100, "name", "kevin", "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example 3
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    final  Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101L);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example 4
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final Graph sampleGraph1 = TinkerFactory.createModern();

    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101,
            "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example 5
Source File: TinkerpopTest.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testTail() throws IOException {
    Graph graph = this.sqlgGraph;
    final GraphReader reader = GryoReader.build()
            .mapper(graph.io(GryoIo.build()).mapper().create())
            .create();
    try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern.kryo")) {
        reader.readGraph(stream, graph);
    }
    assertModernGraph(graph, true, false);
    GraphTraversalSource g = graph.traversal();
    int tail = 0;
    for (int i = 1; i < 72; i++) {
        tail = i;
        List<Vertex> vertices = g.V().repeat(both()).times(3).tail(tail).toList();
        if (tail != vertices.size()) {
            System.out.println("expected " + tail + " found " + vertices.size());
        }
    }
}
 
Example 6
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    sampleGraph1.addVertex(T.id, 100L, "name", "kevin");
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example 7
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);

    sampleGraph1.addVertex(T.id, 100, "name", "kevin", "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example 8
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final Graph sampleGraph1 = TinkerFactory.createModern();

    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101,
            "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example 9
Source File: BaseTest.java    From sqlg with MIT License 5 votes vote down vote up
protected void loadModern(SqlgGraph sqlgGraph) {
    Io.Builder<GraphSONIo> builder = GraphSONIo.build(GraphSONVersion.V3_0);
    final GraphReader reader = sqlgGraph.io(builder).reader().create();
    try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern-v3d0.json")) {
        reader.readGraph(stream, sqlgGraph);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example 10
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the graph has been fully ser/deser with types.
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphWithPartialTypes() throws IOException {
    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);
    final  TinkerGraph baseModern = TinkerFactory.createModern();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, baseModern);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        IoTest.assertModernGraph(read, true, false);
    }
}
 
Example 11
Source File: TinkerpopTest.java    From sqlg with MIT License 5 votes vote down vote up
public void g_V_chooseXlabel_eq_person__unionX__out_lang__out_nameX__in_labelX() throws IOException {
    Graph graph = this.sqlgGraph;
    final GraphReader reader = GryoReader.build()
            .mapper(graph.io(GryoIo.build()).mapper().create())
            .create();
    try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/tinkerpop-modern.kryo")) {
        reader.readGraph(stream, graph);
    }
    assertModernGraph(graph, true, false);
    GraphTraversalSource g = graph.traversal();
    List<Vertex> traversala2 =  g.V().hasId(convertToVertexId("marko")).toList();
    Assert.assertEquals(1, traversala2.size());
    Assert.assertEquals(convertToVertex(graph, "marko"), traversala2.get(0));

}
 
Example 12
Source File: AbstractGraphBenchmark.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a new {@link TinkerGraph} instance and optionally preloads it with one of the test data sets enumerated
 * in {@link LoadGraphWith}.
 *
 * @throws IOException on failure to load graph
 */
@Setup
public void prepare() throws IOException {
    graph = TinkerGraph.open();
    g = graph.traversal();

    final LoadGraphWith[] loadGraphWiths = this.getClass().getAnnotationsByType(LoadGraphWith.class);
    final LoadGraphWith loadGraphWith = loadGraphWiths.length == 0 ? null : loadGraphWiths[0];
    final LoadGraphWith.GraphData loadGraphWithData = null == loadGraphWith ? null : loadGraphWith.value();

    String graphFile;
    if(loadGraphWithData != null) {
        if (loadGraphWithData.equals(LoadGraphWith.GraphData.GRATEFUL)) {
            graphFile = "grateful-dead-v3d0.kryo";
        } else if (loadGraphWithData.equals(LoadGraphWith.GraphData.MODERN)) {
            graphFile = "tinkerpop-modern-v3d0.kryo";
        } else if (loadGraphWithData.equals(LoadGraphWith.GraphData.CLASSIC)) {
            graphFile = "tinkerpop-classic-v3d0.kryo";
        } else if (loadGraphWithData.equals(LoadGraphWith.GraphData.CREW)) {
            graphFile = "tinkerpop-crew-v3d0.kryo";
        } else {
            throw new RuntimeException("Could not load graph with " + loadGraphWithData);
        }

        final GraphReader reader = GryoReader.build().create();
        try (final InputStream stream = AbstractGraphBenchmark.class.
                getResourceAsStream(PATH + graphFile)) {
            reader.readGraph(stream, graph);
        }
    }
}
 
Example 13
Source File: ToyGraphInputRDD.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public JavaPairRDD<Object, VertexWritable> readGraphRDD(final Configuration configuration, final JavaSparkContext sparkContext) {
    KryoShimServiceLoader.applyConfiguration(TinkerGraph.open().configuration());
    final List<VertexWritable> vertices;
    if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("modern"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createModern().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("classic"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createClassic().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("crew"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createTheCrew().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("sink"))
        vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createKitchenSink().vertices(), VertexWritable::new));
    else if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("grateful")) {
        try {
            final Graph graph = TinkerGraph.open();
            final GraphReader reader = GryoReader.build().mapper(graph.io(GryoIo.build()).mapper().create()).create();
            try (final InputStream stream = GryoResourceAccess.class.getResourceAsStream("grateful-dead-v3d0.kryo")) {
                reader.readGraph(stream, graph);
            }
            vertices = IteratorUtils.list(IteratorUtils.map(graph.vertices(), VertexWritable::new));
        } catch (final IOException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    } else
        throw new IllegalArgumentException("No legal toy graph was provided to load: " + configuration.getProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION));

    return sparkContext.parallelize(vertices).mapToPair(vertex -> new Tuple2<>(vertex.get().id(), vertex));
}
 
Example 14
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Those kinds of types are declared differently in the GraphSON type deserializer, check that all are handled
 * properly.
 */
@Test
public void shouldKeepTypesWhenDeserializingSerializedTinkerGraph() throws IOException {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final UUID uuidProp = UUID.randomUUID();
    final Duration durationProp = Duration.ofHours(3);
    final Long longProp = 2L;
    final ByteBuffer byteBufferProp = ByteBuffer.wrap("testbb".getBytes());
    final InetAddress inetAddressProp = InetAddress.getByName("10.10.10.10");

    // One Java util type natively supported by Jackson
    v.property("uuid", uuidProp);
    // One custom time type added by the GraphSON module
    v.property("duration", durationProp);
    // One Java native type not handled by JSON natively
    v.property("long", longProp);
    // One Java util type added by GraphSON
    v.property("bytebuffer", byteBufferProp);
    v.property("inetaddress", inetAddressProp);


    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, tg);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        final Vertex vRead = read.traversal().V().hasLabel("vertexTest").next();
        assertEquals(vRead.property("uuid").value(), uuidProp);
        assertEquals(vRead.property("duration").value(), durationProp);
        assertEquals(vRead.property("long").value(), longProp);
        assertEquals(vRead.property("bytebuffer").value(), byteBufferProp);
        assertEquals(vRead.property("inetaddress").value(), inetAddressProp);
    }
}
 
Example 15
Source File: BaseTest.java    From sqlg with MIT License 5 votes vote down vote up
protected void loadGratefulDead(SqlgGraph sqlgGraph) {
    Io.Builder<GraphSONIo> builder = GraphSONIo.build(GraphSONVersion.V3_0);
    final GraphReader reader = sqlgGraph.io(builder).reader().create();
    try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/grateful-dead-v3d0.json")) {
        reader.readGraph(stream, sqlgGraph);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example 16
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the graph has been fully ser/deser without types.
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphWithoutTypes() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph baseModern = TinkerFactory.createModern();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, baseModern);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        IoTest.assertModernGraph(read, true, false);
    }
}
 
Example 17
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the graph has been fully ser/deser with types.
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphWithPartialTypes() throws IOException {
    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);
    final  TinkerGraph baseModern = TinkerFactory.createModern();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, baseModern);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        IoTest.assertModernGraph(read, true, false);
    }
}
 
Example 18
Source File: CustomTest.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Ignore
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldReadWriteModern() throws Exception {
    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        ((HBaseGraph) graph).createIndex(ElementType.EDGE, "knows", "weight");
        final GraphWriter writer = graph.io(ioBuilderToTest).writer().create();
        writer.writeGraph(os, graph);

        final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.MODERN);
        graphProvider.clear(configuration);
        final Graph g1 = graphProvider.openTestGraph(configuration);
        ((HBaseGraph) g1).createIndex(ElementType.EDGE, "knows", "weight");
        final GraphReader reader = graph.io(ioBuilderToTest).reader().create();
        //((HBaseGraph) graph).dump();
        //((HBaseGraph) g1).dump();
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            reader.readGraph(bais, g1);
        }

        // modern uses double natively so always assert as such
        IoTest.assertModernGraph(g1, true, lossyForId);

        graphProvider.clear(g1, configuration);
    }
}
 
Example 19
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * Those kinds of types are declared differently in the GraphSON type deserializer, check that all are handled
 * properly.
 */
@Test
public void shouldKeepTypesWhenDeserializingSerializedTinkerGraph() throws IOException {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final UUID uuidProp = UUID.randomUUID();
    final Duration durationProp = Duration.ofHours(3);
    final Long longProp = 2L;
    final ByteBuffer byteBufferProp = ByteBuffer.wrap("testbb".getBytes());
    final InetAddress inetAddressProp = InetAddress.getByName("10.10.10.10");

    // One Java util type natively supported by Jackson
    v.property("uuid", uuidProp);
    // One custom time type added by the GraphSON module
    v.property("duration", durationProp);
    // One Java native type not handled by JSON natively
    v.property("long", longProp);
    // One Java util type added by GraphSON
    v.property("bytebuffer", byteBufferProp);
    v.property("inetaddress", inetAddressProp);


    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, tg);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        final Vertex vRead = read.traversal().V().hasLabel("vertexTest").next();
        assertEquals(vRead.property("uuid").value(), uuidProp);
        assertEquals(vRead.property("duration").value(), durationProp);
        assertEquals(vRead.property("long").value(), longProp);
        assertEquals(vRead.property("bytebuffer").value(), byteBufferProp);
        assertEquals(vRead.property("inetaddress").value(), inetAddressProp);
    }
}
 
Example 20
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the graph has been fully ser/deser without types.
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphWithoutTypes() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph baseModern = TinkerFactory.createModern();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, baseModern);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        IoTest.assertModernGraph(read, true, false);
    }
}