@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final GraphSONMapper v1mapper = GraphSONMapper.build().version(GraphSONVersion.V1_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistryV1d0.getInstance()).create(); final GraphSONMapper v2mapper = GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistry.getInstance()).create(); final GraphSONMapper v3mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistry.getInstance()).create(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v1mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v1mapper).create()}, {"graphson-v2-embedded", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v2mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v2mapper).create()}, {"graphson-v3", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v3mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v3mapper).create()}, {"gryo", (Function<Graph, GraphReader>) g -> g.io(IoCore.gryo()).reader().mapper(g.io(IoCore.gryo()).mapper().create()).create(), (Function<Graph, GraphWriter>) g -> g.io(IoCore.gryo()).writer().mapper(g.io(IoCore.gryo()).mapper().create()).create()} }); }
/** * {@inheritDoc} */ @Override public void readGraph(final String file) throws IOException { try (final InputStream in = new FileInputStream(file)) { reader().create().readGraph(in, graph); } }
/** * Read {@link Vertex} objects from output generated by any of the {@link GraphSONWriter} {@code writeVertex} or * {@code writeVertices} methods or by {@link GraphSONWriter#writeGraph(OutputStream, Graph)}. * * @param inputStream a stream containing at least one {@link Vertex} as defined by the accompanying * {@link GraphWriter#writeVertices(OutputStream, Iterator, Direction)} or * {@link GraphWriter#writeVertices(OutputStream, Iterator)} methods. * @param vertexAttachMethod a function that creates re-attaches a {@link Vertex} to a {@link Host} object. * @param edgeAttachMethod a function that creates re-attaches a {@link Edge} to a {@link Host} object. * @param attachEdgesOfThisDirection only edges of this direction are passed to the {@code edgeMaker}. */ @Override public Iterator<Vertex> readVertices(final InputStream inputStream, final Function<Attachable<Vertex>, Vertex> vertexAttachMethod, final Function<Attachable<Edge>, Edge> edgeAttachMethod, final Direction attachEdgesOfThisDirection) throws IOException { return readVertexStrings(inputStream).<Vertex>map(FunctionUtils.wrapFunction(line -> readVertex(new ByteArrayInputStream(line.getBytes()), vertexAttachMethod, edgeAttachMethod, attachEdgesOfThisDirection))).iterator(); }
/** * Read a {@link Vertex} from output generated by any of the {@link GraphSONWriter} {@code writeVertex} or * {@code writeVertices} methods or by {@link GraphSONWriter#writeGraph(OutputStream, Graph)}. * * @param inputStream a stream containing at least a single vertex as defined by the accompanying * {@link GraphWriter#writeVertex(OutputStream, Vertex)}. * @param vertexAttachMethod a function that creates re-attaches a {@link Vertex} to a {@link Host} object. */ @Override public Vertex readVertex(final InputStream inputStream, final Function<Attachable<Vertex>, Vertex> vertexAttachMethod) throws IOException { return readVertex(inputStream, vertexAttachMethod, null, null); }
public GraphSONReader create() { return new GraphSONReader(this); } }
@Override public boolean nextKeyValue() throws IOException { if (!this.lineRecordReader.nextKeyValue()) return false; try (InputStream in = new ByteArrayInputStream(this.lineRecordReader.getCurrentValue().getBytes())) { this.vertexWritable.set(this.hasEdges ? this.graphsonReader.readVertex(in, Attachable::get, Attachable::get, Direction.BOTH) : this.graphsonReader.readVertex(in, Attachable::get)); return true; } }
public GraphSONReader create() { return new GraphSONReader(this); } }
/** * {@inheritDoc} */ @Override public GraphSONReader.Builder reader() { return GraphSONReader.build().mapper(mapper().create()); }
final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge(); readVertexStrings(inputStream).<Vertex>map(FunctionUtils.wrapFunction(line -> readVertex(new ByteArrayInputStream(line.getBytes()), null, null, Direction.IN))).forEach(vertex -> { final Attachable<Vertex> attachable = (Attachable<Vertex>) vertex; cache.put((StarGraph.StarVertex) attachable.get(), attachable.attach(Attachable.Method.create(graphToWriteTo)));
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteSelfLoopingEdges() throws Exception { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V3_0).create(); final Graph source = graph; final Vertex v1 = source.addVertex(); final Vertex v2 = source.addVertex(); v1.addEdge("CONTROL", v2); v1.addEdge("SELFLOOP", v1); final Configuration targetConf = graphProvider.newGraphConfiguration("target", this.getClass(), name.getMethodName(), null); final Graph target = graphProvider.openTestGraph(targetConf); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { source.io(IoCore.graphson()).writer().mapper(mapper).create().writeGraph(os, source); try (ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray())) { target.io(IoCore.graphson()).reader().mapper(mapper).create().readGraph(is, target); } } catch (IOException ioe) { throw new RuntimeException(ioe); } assertEquals(IteratorUtils.count(source.vertices()), IteratorUtils.count(target.vertices())); assertEquals(IteratorUtils.count(source.edges()), IteratorUtils.count(target.edges())); } }
/** * Read a {@link Vertex} from output generated by any of the {@link GraphSONWriter} {@code writeVertex} or * {@code writeVertices} methods or by {@link GraphSONWriter#writeGraph(OutputStream, Graph)}. * * @param inputStream a stream containing at least a single vertex as defined by the accompanying * {@link GraphWriter#writeVertex(OutputStream, Vertex)}. * @param vertexAttachMethod a function that creates re-attaches a {@link Vertex} to a {@link Host} object. */ @Override public Vertex readVertex(final InputStream inputStream, final Function<Attachable<Vertex>, Vertex> vertexAttachMethod) throws IOException { return readVertex(inputStream, vertexAttachMethod, null, null); }
@Override public void initialize(final InputSplit genericSplit, final TaskAttemptContext context) throws IOException { this.lineRecordReader.initialize(genericSplit, context); this.hasEdges = context.getConfiguration().getBoolean(Constants.GREMLIN_HADOOP_GRAPH_READER_HAS_EDGES, true); this.graphsonReader = GraphSONReader.build().mapper( GraphSONMapper.build(). version(GraphSONVersion.valueOf(context.getConfiguration().get(Constants.GREMLIN_HADOOP_GRAPHSON_VERSION, "V3_0"))). typeInfo(TypeInfo.PARTIAL_TYPES). addRegistries(IoRegistryHelper.createRegistries(ConfUtil.makeApacheConfiguration(context.getConfiguration()))).create()).create(); }
/** * Read {@link Vertex} objects from output generated by any of the {@link GraphSONWriter} {@code writeVertex} or * {@code writeVertices} methods or by {@link GraphSONWriter#writeGraph(OutputStream, Graph)}. * * @param inputStream a stream containing at least one {@link Vertex} as defined by the accompanying * {@link GraphWriter#writeVertices(OutputStream, Iterator, Direction)} or * {@link GraphWriter#writeVertices(OutputStream, Iterator)} methods. * @param vertexAttachMethod a function that creates re-attaches a {@link Vertex} to a {@link Host} object. * @param edgeAttachMethod a function that creates re-attaches a {@link Edge} to a {@link Host} object. * @param attachEdgesOfThisDirection only edges of this direction are passed to the {@code edgeMaker}. */ @Override public Iterator<Vertex> readVertices(final InputStream inputStream, final Function<Attachable<Vertex>, Vertex> vertexAttachMethod, final Function<Attachable<Edge>, Edge> edgeAttachMethod, final Direction attachEdgesOfThisDirection) throws IOException { return readVertexStrings(inputStream).<Vertex>map(FunctionUtils.wrapFunction(line -> readVertex(new ByteArrayInputStream(line.getBytes()), vertexAttachMethod, edgeAttachMethod, attachEdgesOfThisDirection))).iterator(); }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteSelfLoopingEdges() throws Exception { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V1_0).create(); final Graph source = graph; final Vertex v1 = source.addVertex(); final Vertex v2 = source.addVertex(); v1.addEdge("CONTROL", v2); v1.addEdge("SELFLOOP", v1); final Configuration targetConf = graphProvider.newGraphConfiguration("target", this.getClass(), name.getMethodName(), null); final Graph target = graphProvider.openTestGraph(targetConf); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { source.io(IoCore.graphson()).writer().mapper(mapper).create().writeGraph(os, source); try (ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray())) { target.io(IoCore.graphson()).reader().mapper(mapper).create().readGraph(is, target); } } catch (IOException ioe) { throw new RuntimeException(ioe); } assertEquals(IteratorUtils.count(source.vertices()), IteratorUtils.count(target.vertices())); assertEquals(IteratorUtils.count(source.edges()), IteratorUtils.count(target.edges())); } }
@Override public boolean nextKeyValue() throws IOException { if (!this.lineRecordReader.nextKeyValue()) return false; try (InputStream in = new ByteArrayInputStream(this.lineRecordReader.getCurrentValue().getBytes())) { this.vertexWritable.set(this.hasEdges ? this.graphsonReader.readVertex(in, Attachable::get, Attachable::get, Direction.BOTH) : this.graphsonReader.readVertex(in, Attachable::get)); return true; } }
final GraphSONMapper.Builder builder = GraphSONMapper.build(); detectRegistries().forEach(builder::addRegistry); return GraphSONReader.build().mapper(builder.create()).create(); } else if (objectOrClass.equals(IO.gryo)){ final GryoMapper.Builder builder = GryoMapper.build();
final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge(); readVertexStrings(inputStream).<Vertex>map(FunctionUtils.wrapFunction(line -> readVertex(new ByteArrayInputStream(line.getBytes()), null, null, Direction.IN))).forEach(vertex -> { final Attachable<Vertex> attachable = (Attachable<Vertex>) vertex; cache.put((StarGraph.StarVertex) attachable.get(), attachable.attach(Attachable.Method.create(graphToWriteTo)));
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteSelfLoopingEdges() throws Exception { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V2_0).create(); final Graph source = graph; final Vertex v1 = source.addVertex(); final Vertex v2 = source.addVertex(); v1.addEdge("CONTROL", v2); v1.addEdge("SELFLOOP", v1); final Configuration targetConf = graphProvider.newGraphConfiguration("target", this.getClass(), name.getMethodName(), null); final Graph target = graphProvider.openTestGraph(targetConf); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { source.io(IoCore.graphson()).writer().mapper(mapper).create().writeGraph(os, source); try (ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray())) { target.io(IoCore.graphson()).reader().mapper(mapper).create().readGraph(is, target); } } catch (IOException ioe) { throw new RuntimeException(ioe); } assertEquals(IteratorUtils.count(source.vertices()), IteratorUtils.count(target.vertices())); assertEquals(IteratorUtils.count(source.edges()), IteratorUtils.count(target.edges())); } }
/** * {@inheritDoc} */ @Override public GraphSONReader.Builder reader() { return GraphSONReader.build().mapper(mapper().create()); }
/** * {@inheritDoc} */ @Override public void readGraph(final String file) throws IOException { try (final InputStream in = new FileInputStream(file)) { reader().create().readGraph(in, graph); } }