org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper. 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: GryoRecordReader.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(final InputSplit genericSplit, final TaskAttemptContext context) throws IOException {
    final FileSplit split = (FileSplit) genericSplit;
    final Configuration configuration = context.getConfiguration();
    if (configuration.get(Constants.GREMLIN_HADOOP_GRAPH_FILTER, null) != null)
        this.graphFilter = VertexProgramHelper.deserialize(ConfUtil.makeApacheConfiguration(configuration), Constants.GREMLIN_HADOOP_GRAPH_FILTER);
    this.gryoReader = GryoReader.build().mapper(
            GryoMapper.build().addRegistries(IoRegistryHelper.createRegistries(ConfUtil.makeApacheConfiguration(configuration))).create()).create();
    long start = split.getStart();
    final Path file = split.getPath();
    if (null != new CompressionCodecFactory(configuration).getCodec(file)) {
        throw new IllegalStateException("Compression is not supported for the (binary) Gryo format");
    }
    // open the file and seek to the start of the split
    this.inputStream = file.getFileSystem(configuration).open(split.getPath());
    this.splitLength = split.getLength();
    if (this.splitLength > 0) this.splitLength -= (seekToHeader(this.inputStream, start) - start);
}
 
Example #2
Source File: AbstractGryoMessageSerializerV1d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void addClassResolverSupplier(final Map<String, Object> config, final GryoMapper.Builder builder) {
    final String className = (String) config.getOrDefault(TOKEN_CLASS_RESOLVER_SUPPLIER, null);
    if (className != null && !className.isEmpty()) {
        try {
            final Class<?> clazz = Class.forName(className);
            try {
                final Method instanceMethod = tryInstanceMethod(clazz);
                builder.classResolver((Supplier<ClassResolver>) instanceMethod.invoke(null));
            } catch (Exception methodex) {
                // tried instance() and that failed so try newInstance() no-arg constructor
                builder.classResolver((Supplier<ClassResolver>) clazz.newInstance());
            }
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    }
}
 
Example #3
Source File: AbstractGryoMessageSerializerV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void addClassResolverSupplier(final Map<String, Object> config, final GryoMapper.Builder builder) {
    final String className = (String) config.getOrDefault(TOKEN_CLASS_RESOLVER_SUPPLIER, null);
    if (className != null && !className.isEmpty()) {
        try {
            final Class<?> clazz = Class.forName(className);
            try {
                final Method instanceMethod = tryInstanceMethod(clazz);
                builder.classResolver((Supplier<ClassResolver>) instanceMethod.invoke(null));
            } catch (Exception methodex) {
                // tried instance() and that failed so try newInstance() no-arg constructor
                builder.classResolver((Supplier<ClassResolver>) clazz.newInstance());
            }
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    }
}
 
Example #4
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteCrewGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-crew-v1d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createTheCrew());
    os.close();
}
 
Example #5
Source File: AbstractGryoMessageSerializerV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public final void configure(final Map<String, Object> config, final Map<String, Graph> graphs) {
    final GryoMapper.Builder builder = GryoMapper.build().version(GryoVersion.V1_0);
    addIoRegistries(config, builder);
    addClassResolverSupplier(config, builder);
    addCustomClasses(config, builder);

    this.serializeToString = Boolean.parseBoolean(config.getOrDefault(TOKEN_SERIALIZE_RESULT_TO_STRING, "false").toString());
    this.bufferSize = Integer.parseInt(config.getOrDefault(TOKEN_BUFFER_SIZE, "4096").toString());

    this.gryoMapper = configureBuilder(builder, config, graphs).create();
}
 
Example #6
Source File: GryoLiteMessageSerializerV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static GryoMapper.Builder overrideWithLite(final GryoMapper.Builder builder) {
    // override the core graph Elements so as to serialize with "reference" as opposed to "detached"
    builder.addCustom(Edge.class, new EdgeLiteSerializer());
    builder.addCustom(Vertex.class, new VertexLiteSerializer());
    builder.addCustom(VertexProperty.class, new VertexPropertyLiteSerializer());
    builder.addCustom(Property.class, new PropertyLiteSerializer());
    builder.addCustom(Path.class, new PathLiteSerializer());
    return builder;
}
 
Example #7
Source File: AbstractGryoMessageSerializerV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public final void configure(final Map<String, Object> config, final Map<String, Graph> graphs) {
    final GryoMapper.Builder builder = GryoMapper.build().version(GryoVersion.V3_0);
    addIoRegistries(config, builder);
    addClassResolverSupplier(config, builder);
    addCustomClasses(config, builder);

    this.serializeToString = Boolean.parseBoolean(config.getOrDefault(TOKEN_SERIALIZE_RESULT_TO_STRING, "false").toString());
    this.bufferSize = Integer.parseInt(config.getOrDefault(TOKEN_BUFFER_SIZE, "4096").toString());

    this.gryoMapper = configureBuilder(builder, config, graphs).create();
}
 
Example #8
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraph() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example #9
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraphUsingDeprecatedTinkerIoRegistry() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example #10
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteClassicGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-classic-v1d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createClassic());
    os.close();
}
 
Example #11
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteModernGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-modern-v1d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createModern());
    os.close();
}
 
Example #12
Source File: QueryTest.java    From janusgraph-visualization with Apache License 2.0 5 votes vote down vote up
public void before() {
    String host = "fenglex.com";
    int port = 8182;
    String remoteTraversalSourceName = "g";
    GryoMapper.Builder builder = GryoMapper.build().addRegistry(JanusGraphIoRegistry.getInstance());
    MessageSerializer serializer = new GryoMessageSerializerV3d0(builder);
    Cluster cluster = Cluster.build().
            addContactPoint(host).
            port(port).
            serializer(serializer).
            create();
    traversal().withRemote(DriverRemoteConnection.using(cluster, remoteTraversalSourceName));
}
 
Example #13
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteSinkGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-sink-v1d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createKitchenSink());
    os.close();
}
 
Example #14
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteClassicGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-classic-v3d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createClassic());
    os.close();
}
 
Example #15
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteModernGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-modern-v3d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createModern());
    os.close();
}
 
Example #16
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteCrewGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-crew-v3d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createTheCrew());
    os.close();
}
 
Example #17
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteSinkGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-sink-v3d0.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createKitchenSink());
    os.close();
}
 
Example #18
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteDEFAULTClassicGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-classic.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createClassic());
    os.close();
}
 
Example #19
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteDEFAULTModernGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-modern.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createModern());
    os.close();
}
 
Example #20
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteDEFAULTCrewGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-crew.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createTheCrew());
    os.close();
}
 
Example #21
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteDEFAULTSinkGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(new File(tempPath, "tinkerpop-sink.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createKitchenSink());
    os.close();
}
 
Example #22
Source File: IoDataGenerationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWriteSampleForGremlinServer() throws IOException {
    final Graph g = TinkerGraph.open();
    IntStream.range(0, 10000).forEach(i -> g.addVertex("oid", i));
    DistributionGenerator.build(g)
            .label("knows")
            .seedGenerator(() -> 987654321L)
            .outDistribution(new PowerLawDistribution(2.1))
            .inDistribution(new PowerLawDistribution(2.1))
            .expectedNumEdges(100000).create().generate();

    final OutputStream os = new FileOutputStream(new File(tempPath, "sample.kryo"));
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, g);
    os.close();
}
 
Example #23
Source File: IoDataGenerationTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteDEFAULTClassicGraphAsGryoV3d0() throws IOException {
    final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-classic.kryo");
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V3_0).create()).create().writeGraph(os, TinkerFactory.createClassic());
    os.close();
}
 
Example #24
Source File: GremlinUtil.java    From janusgraph-visualization with Apache License 2.0 5 votes vote down vote up
public static Cluster cluster(String host, int port, IoRegistry registry) {
    // GryoMapper.Builder builder = GryoMapper.build().addRegistry(JanusGraphIoRegistry.getInstance());
    GryoMapper.Builder builder = GryoMapper.build().addRegistry(registry);
    MessageSerializer serializer = new GryoMessageSerializerV3d0(builder);
    return Cluster.build().
            addContactPoint(host).
            port(port).
            serializer(serializer).
            create();
}
 
Example #25
Source File: TinkerGraphTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraph() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example #26
Source File: TinkerGraphTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeWithColorClassResolverToTinkerGraphUsingDeprecatedTinkerIoRegistry() throws Exception {
    final Map<String,Color> colors = new HashMap<>();
    colors.put("red", Color.RED);
    colors.put("green", Color.GREEN);

    final ArrayList<Color> colorList = new ArrayList<>(Arrays.asList(Color.RED, Color.GREEN));

    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = GryoMapper.build().version(GryoVersion.V3_0).addRegistry(TinkerIoRegistryV3d0.instance()).classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        kryo.writeObject(out, colorList);
        out.flush();
        final byte[] b = stream.toByteArray();

        try (final InputStream inputStream = new ByteArrayInputStream(b)) {
            final Input input = new Input(inputStream);
            final List m = kryo.readObject(input, ArrayList.class);
            final TinkerGraph readX = (TinkerGraph) m.get(0);
            assertEquals(104, IteratorUtils.count(readX.vertices()));
            assertEquals(102, IteratorUtils.count(readX.edges()));
        }
    }
}
 
Example #27
Source File: IoDataGenerationTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteClassicGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-classic-v1d0.kryo");
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createClassic());
    os.close();
}
 
Example #28
Source File: IoDataGenerationTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteModernGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-modern-v1d0.kryo");
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createModern());
    os.close();
}
 
Example #29
Source File: IoDataGenerationTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteCrewGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-crew-v1d0.kryo");
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createTheCrew());
    os.close();
}
 
Example #30
Source File: IoDataGenerationTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
/**
 * No assertions.  Just write out the graph for convenience.
 */
@Test
public void shouldWriteSinkGraphAsGryoV1d0() throws IOException {
    final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-sink-v1d0.kryo");
    GryoWriter.build().mapper(GryoMapper.build().version(GryoVersion.V1_0).create()).create().writeGraph(os, TinkerFactory.createKitchenSink());
    os.close();
}