Java Code Examples for org.apache.tinkerpop.gremlin.structure.util.GraphFactory#open()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.GraphFactory#open() . 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: InputOutputRDDTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadFromWriteToArbitraryRDD() throws Exception {
    final Configuration configuration = new BaseConfiguration();
    configuration.setProperty("spark.master", "local[4]");
    configuration.setProperty("spark.serializer", GryoSerializer.class.getCanonicalName());
    configuration.setProperty(Graph.GRAPH, HadoopGraph.class.getName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, ExampleInputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, ExampleOutputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, TestHelper.makeTestDataDirectory(this.getClass(), "shouldReadFromWriteToArbitraryRDD"));
    configuration.setProperty(Constants.GREMLIN_HADOOP_JARS_IN_DISTRIBUTED_CACHE, false);
    ////////
    Graph graph = GraphFactory.open(configuration);
    graph.compute(SparkGraphComputer.class)
            .result(GraphComputer.ResultGraph.NEW)
            .persist(GraphComputer.Persist.EDGES)
            .program(TraversalVertexProgram.build()
                    .traversal(graph.traversal().withComputer(SparkGraphComputer.class),
                            "gremlin-groovy",
                            "g.V()").create(graph)).submit().get();
}
 
Example 2
Source File: PersistedInputOutputRDDIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotPersistRDDAcrossJobs() throws Exception {
    Spark.create("local[4]");
    final String rddName = TestHelper.makeTestDataDirectory(PersistedInputOutputRDDIntegrateTest.class, UUID.randomUUID().toString());
    final Configuration configuration = super.getBaseConfiguration();
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, SparkHadoopGraphProvider.PATHS.get("tinkerpop-modern-v3d0.kryo"));
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, PersistedOutputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, rddName);
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, false);  // because the spark context is NOT persisted, neither is the RDD
    Graph graph = GraphFactory.open(configuration);
    graph.compute(SparkGraphComputer.class)
            .result(GraphComputer.ResultGraph.NEW)
            .persist(GraphComputer.Persist.EDGES)
            .program(TraversalVertexProgram.build()
                    .traversal(graph.traversal().withComputer(SparkGraphComputer.class),
                            "gremlin-groovy",
                            "g.V()").create(graph)).submit().get();
    ////////
    Spark.create("local[4]");
    assertFalse(Spark.hasRDD(Constants.getGraphLocation(rddName)));
    assertEquals(0, Spark.getContext().getPersistentRDDs().size());
    Spark.close();
}
 
Example 3
Source File: PersistedInputOutputRDDIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotHaveDanglingPersistedComputeRDDs() throws Exception {
    Spark.create("local[4]");
    final String rddName = TestHelper.makeTestDataDirectory(PersistedInputOutputRDDIntegrateTest.class, UUID.randomUUID().toString());
    final Configuration configuration = super.getBaseConfiguration();
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, SparkHadoopGraphProvider.PATHS.get("tinkerpop-modern-v3d0.kryo"));
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, GryoOutputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, rddName);
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    Graph graph = GraphFactory.open(configuration);
    ///
    assertEquals(6, graph.traversal().withComputer(Computer.compute(SparkGraphComputer.class)).V().out().count().next().longValue());
    assertFalse(Spark.hasRDD(Constants.getGraphLocation(rddName)));
    assertEquals(0, Spark.getContext().getPersistentRDDs().size());
    //
    assertEquals(2, graph.traversal().withComputer(Computer.compute(SparkGraphComputer.class)).V().out().out().count().next().longValue());
    assertFalse(Spark.hasRDD(Constants.getGraphLocation(rddName)));
    assertEquals(0, Spark.getContext().getPersistentRDDs().size());
    ///////
    Spark.close();
}
 
Example 4
Source File: InputRDDTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadFromArbitraryRDD() {
    final Configuration configuration = new BaseConfiguration();
    configuration.setProperty("spark.master", "local[4]");
    configuration.setProperty("spark.serializer", GryoSerializer.class.getCanonicalName());
    configuration.setProperty(Graph.GRAPH, HadoopGraph.class.getName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, ExampleInputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, GryoOutputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, TestHelper.makeTestDataDirectory(this.getClass(), "shouldReadFromArbitraryRDD"));
    configuration.setProperty(Constants.GREMLIN_HADOOP_JARS_IN_DISTRIBUTED_CACHE, false);
    ////////
    Graph graph = GraphFactory.open(configuration);
    assertEquals(123l, graph.traversal().withComputer(SparkGraphComputer.class).V().values("age").sum().next());
    assertEquals(Long.valueOf(4l), graph.traversal().withComputer(SparkGraphComputer.class).V().count().next());
}
 
Example 5
Source File: GraphConstructionTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * All Gremlin Structure implementations should be constructable through {@link GraphFactory}.
 */
@Test
public void shouldOpenGraphThroughGraphFactoryViaApacheConfig() throws Exception {
    final Graph expectedGraph = graph;
    final Configuration c = graphProvider.newGraphConfiguration("temp1", this.getClass(), name.getMethodName(), null);
    final Graph createdGraph = GraphFactory.open(c);

    assertNotNull(createdGraph);
    assertEquals(expectedGraph.getClass(), createdGraph.getClass());

    graphProvider.clear(createdGraph, c);
}
 
Example 6
Source File: DocumentGraphConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentGraphConsumerRelationAsLinkCanCopeWithSameRelationExternalId()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  properties.setProperty(
      TinkerGraph.GREMLIN_TINKERGRAPH_DEFAULT_VERTEX_PROPERTY_CARDINALITY,
      VertexProperty.Cardinality.list.name());
  writeProperties();

  Person p1 = Annotations.createPerson(jCas, 0, 10, "source");
  Person p2 = Annotations.createPerson(jCas, 10, 20, "target");

  Relation r1 = new Relation(jCas);
  r1.setBegin(0);
  r1.setEnd(4);
  r1.setValue("test");
  r1.setSource(p1);
  r1.setTarget(p2);
  r1.addToIndexes(jCas);

  Relation r2 = new Relation(jCas);
  r2.setBegin(0);
  r2.setEnd(4);
  r2.setValue("test");
  r2.setSource(p1);
  r2.setTarget(p2);
  r2.addToIndexes(jCas);

  assertEquals(r1.getExternalId(), r2.getExternalId());

  processJCas(
      DocumentGraphConsumer.PARAM_GRAPH_CONFIG,
      propertiesFile.getAbsolutePath(),
      DocumentGraphConsumer.PARAM_OUTPUT_RELATIONS_AS_LINKS,
      false);
  Graph graph = GraphFactory.open(propertiesFile.getAbsolutePath());

  assertTrue(graph.traversal().V(r1.getExternalId()).hasNext());
}
 
Example 7
Source File: DocumentGraphConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentGraphConsumerCanCopeWithSameRelationExternalId()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  properties.setProperty(
      TinkerGraph.GREMLIN_TINKERGRAPH_DEFAULT_VERTEX_PROPERTY_CARDINALITY,
      VertexProperty.Cardinality.list.name());
  writeProperties();

  Person p1 = Annotations.createPerson(jCas, 0, 10, "source");
  Person p2 = Annotations.createPerson(jCas, 10, 20, "target");

  Relation r1 = new Relation(jCas);
  r1.setBegin(0);
  r1.setEnd(4);
  r1.setValue("test");
  r1.setSource(p1);
  r1.setTarget(p2);
  r1.addToIndexes(jCas);

  Relation r2 = new Relation(jCas);
  r2.setBegin(0);
  r2.setEnd(4);
  r2.setValue("test");
  r2.setSource(p1);
  r2.setTarget(p2);
  r2.addToIndexes(jCas);

  assertEquals(r1.getExternalId(), r2.getExternalId());

  processJCas(
      DocumentGraphConsumer.PARAM_GRAPH_CONFIG,
      propertiesFile.getAbsolutePath(),
      DocumentGraphConsumer.PARAM_OUTPUT_RELATIONS_AS_LINKS,
      true);
  Graph graph = GraphFactory.open(propertiesFile.getAbsolutePath());

  assertTrue(graph.traversal().E(r1.getExternalId()).hasNext());
}
 
Example 8
Source File: DocumentGraphConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentGraphConsumerCanCopeWithSameReferenceTargetExternalId()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  properties.setProperty(
      TinkerGraph.GREMLIN_TINKERGRAPH_DEFAULT_VERTEX_PROPERTY_CARDINALITY,
      VertexProperty.Cardinality.list.name());
  writeProperties();

  Person p1 = Annotations.createPerson(jCas, 0, 4, "test");
  Person p2 = Annotations.createPerson(jCas, 0, 4, "test");
  Annotations.createReferenceTarget(jCas, p1, p2);
  Annotations.createReferenceTarget(jCas, p1, p2);

  String externalId = ConsumerUtils.getExternalId(ImmutableList.of(p1, p2));

  processJCas(
      DocumentGraphConsumer.PARAM_GRAPH_CONFIG,
      propertiesFile.getAbsolutePath(),
      DocumentGraphConsumer.PARAM_OUTPUT_RELATIONS_AS_LINKS,
      true,
      DocumentGraphConsumer.PARAM_OUTPUT_REFERENTS,
      true);
  Graph graph = GraphFactory.open(propertiesFile.getAbsolutePath());

  assertTrue(graph.traversal().V(externalId).hasNext());
}
 
Example 9
Source File: PersistedInputOutputRDDIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPersistRDDBasedOnStorageLevel() throws Exception {
    Spark.create("local[4]");
    int counter = 0;
    for (final String storageLevel : Arrays.asList("MEMORY_ONLY", "DISK_ONLY", "MEMORY_ONLY_SER", "MEMORY_AND_DISK_SER")) {
        assertEquals(counter, Spark.getRDDs().size());
        assertEquals(counter, Spark.getContext().getPersistentRDDs().size());
        counter++;
        final String rddName = TestHelper.makeTestDataDirectory(PersistedInputOutputRDDIntegrateTest.class, UUID.randomUUID().toString());
        final Configuration configuration = super.getBaseConfiguration();
        configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, SparkHadoopGraphProvider.PATHS.get("tinkerpop-modern-v3d0.kryo"));
        configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
        configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, PersistedOutputRDD.class.getCanonicalName());
        configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_STORAGE_LEVEL, storageLevel);
        configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, rddName);
        configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
        Graph graph = GraphFactory.open(configuration);
        graph.compute(SparkGraphComputer.class)
                .result(GraphComputer.ResultGraph.NEW)
                .persist(GraphComputer.Persist.EDGES)
                .program(TraversalVertexProgram.build()
                        .traversal(graph.traversal().withComputer(SparkGraphComputer.class),
                                "gremlin-groovy",
                                "g.V().groupCount('m').by('name').out()").create(graph)).submit().get();
        ////////
        assertTrue(Spark.hasRDD(Constants.getGraphLocation(rddName)));
        assertEquals(StorageLevel.fromString(storageLevel), Spark.getRDD(Constants.getGraphLocation(rddName)).getStorageLevel());
        assertEquals(counter, Spark.getRDDs().size());
        assertEquals(counter, Spark.getContext().getPersistentRDDs().size());
    }
    Spark.close();
}
 
Example 10
Source File: CassandraInputFormatIT.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadGraphOfTheGods() {
    GraphOfTheGodsFactory.load(graph, null, true);
    assertEquals(12L, (long) graph.traversal().V().count().next());
    Graph g = GraphFactory.open("target/test-classes/cassandra-read.properties");
    GraphTraversalSource t = g.traversal(GraphTraversalSource.computer(SparkGraphComputer.class));
    assertEquals(12L, (long) t.V().count().next());
}
 
Example 11
Source File: GraphManager.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private void loadGraph(String name, String path) {
    final Graph graph = GraphFactory.open(path);
    this.graphs.put(name, graph);
    LOG.info("Graph '{}' was successfully configured via '{}'", name, path);

    if (this.requireAuthentication() &&
        !(graph instanceof HugeGraphAuthProxy)) {
        LOG.warn("You may need to support access control for '{}' with {}",
                 path, HugeFactoryAuthProxy.GRAPH_FACTORY);
    }
}
 
Example 12
Source File: StandardAuthenticator.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public void setup(HugeConfig config) {
    String graphName = config.get(ServerOptions.AUTH_GRAPH_STORE);
    String graphPath = config.getMap(ServerOptions.GRAPHS).get(graphName);
    E.checkArgument(graphPath != null,
                    "Invalid graph name '%s'", graphName);
    this.graph = (HugeGraph) GraphFactory.open(graphPath);
}
 
Example 13
Source File: DocumentGraphConsumer.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Override
protected Graph createGraph() {
  return GraphFactory.open(graphConfig);
}
 
Example 14
Source File: LocalPropertyTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSetThreadLocalProperties() throws Exception {
    final String testName = "ThreadLocalProperties";
    final String rddName = TestHelper.makeTestDataDirectory(LocalPropertyTest.class, UUID.randomUUID().toString());
    final Configuration configuration = new BaseConfiguration();
    configuration.setProperty("spark.master", "local[4]");
    configuration.setProperty("spark.serializer", GryoSerializer.class.getCanonicalName());
    configuration.setProperty(Graph.GRAPH, HadoopGraph.class.getName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, SparkHadoopGraphProvider.PATHS.get("tinkerpop-modern-v3d0.kryo"));
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, PersistedOutputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, rddName);
    configuration.setProperty(Constants.GREMLIN_HADOOP_JARS_IN_DISTRIBUTED_CACHE, false);
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    configuration.setProperty("spark.jobGroup.id", "22");
    Graph graph = GraphFactory.open(configuration);
    graph.compute(SparkGraphComputer.class)
            .result(GraphComputer.ResultGraph.NEW)
            .persist(GraphComputer.Persist.EDGES)
            .program(TraversalVertexProgram.build()
                    .traversal(graph.traversal().withComputer(Computer.compute(SparkGraphComputer.class)),
                            "gremlin-groovy",
                            "g.V()").create(graph)).submit().get();
    ////////
    SparkConf sparkConfiguration = new SparkConf();
    sparkConfiguration.setAppName(testName);
    ConfUtil.makeHadoopConfiguration(configuration).forEach(entry -> sparkConfiguration.set(entry.getKey(), entry.getValue()));
    JavaSparkContext sparkContext = new JavaSparkContext(SparkContext.getOrCreate(sparkConfiguration));
    JavaSparkStatusTracker statusTracker = sparkContext.statusTracker();
    assertTrue(statusTracker.getJobIdsForGroup("22").length >= 1);
    assertTrue(Spark.hasRDD(Constants.getGraphLocation(rddName)));
    ///////
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, PersistedInputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, rddName);
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, null);
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, null);

    // just a note that this value should have always been set to true, but from the initial commit was false.
    // interestingly the last assertion had always passed up to spark 2.3.x when it started to fail. apparently
    // that assertion should likely have never passed, so it stands to reason that there was a bug in spark in
    // 2.2.x that was resolved for 2.3.x....that's my story and i'm sticking to it.
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    configuration.setProperty("spark.jobGroup.id", "44");
    graph = GraphFactory.open(configuration);
    graph.compute(SparkGraphComputer.class)
            .result(GraphComputer.ResultGraph.NEW)
            .persist(GraphComputer.Persist.NOTHING)
            .program(TraversalVertexProgram.build()
                    .traversal(graph.traversal().withComputer(SparkGraphComputer.class),
                            "gremlin-groovy",
                            "g.V()").create(graph)).submit().get();
    ///////
    assertTrue(statusTracker.getJobIdsForGroup("44").length >= 1);
}
 
Example 15
Source File: HBaseGraphTest.java    From hgraphdb with Apache License 2.0 4 votes vote down vote up
@Before
public void makeGraph() {
    graph = (HBaseGraph) GraphFactory.open(generateGraphConfig("testgraph"));
}
 
Example 16
Source File: DocumentGraphConsumerTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testDocumentGraphConsumerCanCopeWithSameEventExternalId()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  properties.setProperty(
      TinkerGraph.GREMLIN_TINKERGRAPH_DEFAULT_VERTEX_PROPERTY_CARDINALITY,
      VertexProperty.Cardinality.list.name());
  writeProperties();

  Person p1 = Annotations.createPerson(jCas, 0, 4, "test");
  Person p2 = Annotations.createPerson(jCas, 0, 4, "test");

  Event e1 = new Event(jCas);
  e1.setBegin(0);
  e1.setEnd(4);
  e1.setValue("test");
  e1.setEntities(new FSArray(jCas, 2));
  e1.setEntities(0, p1);
  e1.setEntities(1, p2);
  e1.addToIndexes(jCas);

  Event e2 = new Event(jCas);
  e2.setBegin(0);
  e2.setEnd(4);
  e2.setValue("test");
  e2.setEntities(new FSArray(jCas, 2));
  e2.setEntities(0, p1);
  e2.setEntities(1, p2);
  e2.addToIndexes(jCas);

  assertEquals(e1.getExternalId(), e2.getExternalId());

  processJCas(
      DocumentGraphConsumer.PARAM_GRAPH_CONFIG,
      propertiesFile.getAbsolutePath(),
      DocumentGraphConsumer.PARAM_OUTPUT_RELATIONS_AS_LINKS,
      true,
      DocumentGraphConsumer.PARAM_OUTPUT_EVENTS,
      true);
  Graph graph = GraphFactory.open(propertiesFile.getAbsolutePath());

  assertTrue(graph.traversal().V(e1.getExternalId()).hasNext());
}
 
Example 17
Source File: HadoopGraphFactory.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@VisibleForTesting
public synchronized HadoopGraph getGraph(Keyspace keyspace) {
    return (HadoopGraph) GraphFactory.open(addHadoopProperties(keyspace.name()).properties());
}
 
Example 18
Source File: GryoSerializerIntegrateTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldHaveAllRegisteredGryoSerializerClasses() throws Exception {
    // this is a stress test that ensures that when data is spilling to disk, persisted to an RDD, etc. the correct classes are registered with GryoSerializer.
    final TinkerGraph randomGraph = TinkerGraph.open();
    int totalVertices = 200000;
    TestHelper.createRandomGraph(randomGraph, totalVertices, 100);
    final String inputLocation = TestHelper.makeTestDataFile(GryoSerializerIntegrateTest.class,
                                                             UUID.randomUUID().toString(),
                                                             "random-graph.kryo");
    randomGraph.io(IoCore.gryo()).writeGraph(inputLocation);
    randomGraph.clear();
    randomGraph.close();

    final String outputLocation = TestHelper.makeTestDataDirectory(GryoSerializerIntegrateTest.class, UUID.randomUUID().toString());
    Configuration configuration = getBaseConfiguration();
    configuration.clearProperty(Constants.SPARK_SERIALIZER); // ensure proper default to GryoSerializer
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, inputLocation);
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, GryoOutputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, outputLocation);
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, false);
    Graph graph = GraphFactory.open(configuration);
    final GraphTraversal.Admin<Vertex, Map<Vertex, Collection<Vertex>>> traversal = graph.traversal().withComputer(SparkGraphComputer.class).V().group("m").<Map<Vertex, Collection<Vertex>>>cap("m").asAdmin();
    assertTrue(traversal.hasNext());
    assertEquals(traversal.next(), traversal.getSideEffects().get("m"));
    assertFalse(traversal.hasNext());
    assertTrue(traversal.getSideEffects().exists("m"));
    assertTrue(traversal.getSideEffects().get("m") instanceof Map);
    assertEquals(totalVertices, traversal.getSideEffects().<Map>get("m").size());

    configuration = getBaseConfiguration();
    configuration.clearProperty(Constants.SPARK_SERIALIZER); // ensure proper default to GryoSerializer
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, inputLocation);
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, GryoInputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, PersistedOutputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_STORAGE_LEVEL, "DISK_ONLY");
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, "persisted-rdd");
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    graph = GraphFactory.open(configuration);
    assertEquals(totalVertices, graph.compute(SparkGraphComputer.class).program(PageRankVertexProgram.build().iterations(2).create(graph)).submit().get().graph().traversal().V().count().next().longValue());

    configuration = getBaseConfiguration();
    configuration.clearProperty(Constants.SPARK_SERIALIZER); // ensure proper default to GryoSerializer
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, "persisted-rdd");
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, PersistedInputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, GryoOutputFormat.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, outputLocation);
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    graph = GraphFactory.open(configuration);
    assertEquals(totalVertices, graph.traversal().withComputer(SparkGraphComputer.class).V().count().next().longValue());

    configuration = getBaseConfiguration();
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, "persisted-rdd");
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, PersistedInputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_WRITER, PersistedOutputRDD.class.getCanonicalName());
    configuration.setProperty(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION, outputLocation);
    configuration.setProperty(Constants.GREMLIN_SPARK_GRAPH_STORAGE_LEVEL, "MEMORY_ONLY"); // this should be ignored as you can't change the persistence level once created
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_STORAGE_LEVEL, "MEMORY_AND_DISK");
    configuration.setProperty(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, true);
    graph = GraphFactory.open(configuration);
    assertEquals(totalVertices, graph.traversal().withComputer(SparkGraphComputer.class).V().count().next().longValue());
}
 
Example 19
Source File: EntityGraphConsumerTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiGraphTransform()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  properties.setProperty(
      TinkerGraph.GREMLIN_TINKERGRAPH_DEFAULT_VERTEX_PROPERTY_CARDINALITY,
      VertexProperty.Cardinality.list.name());
  writeProperties();

  processJCas(
      EntityGraphConsumer.PARAM_GRAPH_CONFIG,
      propertiesFile.getAbsolutePath(),
      EntityGraphConsumer.PARAM_MULTI_VALUE_PROPERTIES,
      true);
  Graph graph = GraphFactory.open(propertiesFile.getAbsolutePath());

  String documentId = ConsumerUtils.getExternalId(getDocumentAnnotation(jCas), false);

  final GraphTraversalSource traversal = graph.traversal();

  assertEquals(3, traversal.V().hasLabel(ENTITY).count().next().intValue());
  assertEquals(1, traversal.V().hasLabel(EVENT).count().next().intValue());
  assertEquals(2, traversal.E().hasLabel(PARTICIPANT_IN).count().next().intValue());
  assertEquals(2, traversal.E().hasLabel(RELATION).count().next().intValue());

  assertEquals(4, IteratorUtils.count(graph.vertices()));
  assertEquals(4, IteratorUtils.count(graph.edges()));

  Multimap<ReferenceTarget, Entity> targets =
      ReferentUtils.createReferentMap(jCas, Entity.class, false);
  targets
      .entries()
      .forEach(
          e ->
              assertTrue(
                  traversal
                      .V()
                      .hasLabel(ENTITY)
                      .has("type")
                      .has(MENTIONS_PROPERTY)
                      .has("value")
                      .not(has("begin"))
                      .not(has("end"))
                      .has(DocumentGraphFactory.FIELD_DOCUMENT_ID, documentId)
                      .hasNext()));

  Event event = JCasUtil.selectSingle(jCas, Event.class);
  assertTrue(
      traversal
          .V(event.getExternalId())
          .hasLabel(EVENT)
          .has("value", event.getValue())
          .not(has("begin"))
          .not(has("end"))
          .has(MENTIONS_PROPERTY)
          .has(DocumentGraphFactory.FIELD_DOCUMENT_ID, documentId)
          .hasNext());

  JCasUtil.select(jCas, Relation.class)
      .forEach(
          r ->
              assertTrue(
                  traversal
                      .E(r.getExternalId())
                      .hasLabel(RELATION)
                      .has("relationshipType", r.getRelationshipType())
                      .has("value", r.getValue())
                      .not(has("begin"))
                      .not(has("end"))
                      .has(MENTIONS_PROPERTY)
                      .has(DocumentGraphFactory.FIELD_DOCUMENT_ID, documentId)
                      .hasNext()));

  Map<String, Object> mention = new HashMap<>();
  mention.put("id", "cb7ba8e02c88dcdc832f181c1336ce54334f9bb125bd90371a6d59d098844f23");
  mention.put("begin", 25);
  mention.put("end", 35);
  mention.put("confidence", 0.9d);
  assertTrue(
      traversal
          .V()
          .has("value", "He")
          .has("value", "John Smith")
          .has(MENTIONS_PROPERTY, mention)
          .hasNext());
}
 
Example 20
Source File: EntityGraphConsumerTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testSingleGraphTransform()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  properties.setProperty(
      TinkerGraph.GREMLIN_TINKERGRAPH_DEFAULT_VERTEX_PROPERTY_CARDINALITY,
      VertexProperty.Cardinality.single.name());
  writeProperties();

  processJCas(
      EntityGraphConsumer.PARAM_GRAPH_CONFIG,
      propertiesFile.getAbsolutePath(),
      EntityGraphConsumer.PARAM_VALUE_STRATEGY,
      new String[] {"value", "Longest", "gender", "Mode"});
  Graph graph = GraphFactory.open(propertiesFile.getAbsolutePath());

  String documentId = ConsumerUtils.getExternalId(getDocumentAnnotation(jCas), false);

  final GraphTraversalSource traversal = graph.traversal();

  assertEquals(3, traversal.V().hasLabel(ENTITY).count().next().intValue());
  assertEquals(1, traversal.V().hasLabel(EVENT).count().next().intValue());
  assertEquals(2, traversal.E().hasLabel(PARTICIPANT_IN).count().next().intValue());
  assertEquals(2, traversal.E().hasLabel(RELATION).count().next().intValue());

  assertEquals(4, IteratorUtils.count(graph.vertices()));
  assertEquals(4, IteratorUtils.count(graph.edges()));

  JCasUtil.select(jCas, Entity.class)
      .forEach(
          e ->
              assertTrue(
                  traversal
                      .V()
                      .hasLabel(ENTITY)
                      .has("type")
                      .has(MENTIONS_PROPERTY)
                      .has("value")
                      .not(has("begin"))
                      .not(has("end"))
                      .hasNext()));

  Event event = JCasUtil.selectSingle(jCas, Event.class);
  assertTrue(
      traversal
          .V(event.getExternalId())
          .hasLabel(EVENT)
          .has("value", event.getValue())
          .not(has("begin"))
          .not(has("end"))
          .has(MENTIONS_PROPERTY)
          .hasNext());

  JCasUtil.select(jCas, Relation.class)
      .forEach(
          r ->
              assertTrue(
                  traversal
                      .E(r.getExternalId())
                      .hasLabel(RELATION)
                      .has("relationshipType", r.getRelationshipType())
                      .has("value", r.getValue())
                      .not(has("begin"))
                      .not(has("end"))
                      .has(MENTIONS_PROPERTY)
                      .hasNext()));

  assertTrue(
      traversal
          .V()
          .has("gender", "Male")
          .has("value", "John Smith")
          .not(has("value", "He"))
          .has(DocumentGraphFactory.FIELD_DOCUMENT_ID, ImmutableList.of(documentId, documentId))
          .hasNext());
}