com.thinkaurelius.titan.core.TitanGraph Java Examples

The following examples show how to use com.thinkaurelius.titan.core.TitanGraph. 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: AbstractTitanGraphProvider.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
@Override
public void clear(Graph g, final Configuration configuration) throws Exception {
    if (null != g) {
        while (g instanceof WrappedGraph) g = ((WrappedGraph<? extends Graph>) g).getBaseGraph();
        TitanGraph graph = (TitanGraph) g;
        if (graph.isOpen()) {
            if (g.tx().isOpen()) g.tx().rollback();
            g.close();
        }
    }

    WriteConfiguration config = new CommonsConfiguration(configuration);
    BasicConfiguration readConfig = new BasicConfiguration(GraphDatabaseConfiguration.ROOT_NS, config, BasicConfiguration.Restriction.NONE);
    if (readConfig.has(GraphDatabaseConfiguration.STORAGE_BACKEND)) {
        TitanGraphBaseTest.clearGraph(config);
    }
}
 
Example #2
Source File: ManagementSystem.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
public Consumer<ScanMetrics> getIndexJobFinisher(final TitanGraph graph, final SchemaAction action) {
    Preconditions.checkArgument((graph != null && action != null) || (graph == null && action == null));
    return metrics -> {
        try {
            if (metrics.get(ScanMetrics.Metric.FAILURE) == 0) {
                if (action != null) {
                    ManagementSystem mgmt = (ManagementSystem) graph.openManagement();
                    try {
                        TitanIndex index = retrieve(mgmt);
                        mgmt.updateIndex(index, action);
                    } finally {
                        mgmt.commit();
                    }
                }
                LOGGER.info("Index update job successful for [{}]", IndexIdentifier.this.toString());
            } else {
                LOGGER.error("Index update job unsuccessful for [{}]. Check logs", IndexIdentifier.this.toString());
            }
        } catch (Throwable e) {
            LOGGER.error("Error encountered when updating index after job finished [" + IndexIdentifier.this.toString() + "]: ", e);
        }
    };
}
 
Example #3
Source File: MizoRDD.java    From mizo with Apache License 2.0 6 votes vote down vote up
/**
 * Given a path for Titan config file, connects and gets the internal Titan types,
 * converting them to MizoTitanRelationTypes mapped by type-ids
 * @param titanConfigPath Path to Titan's config path
 * @return Mapping between relation type-ids to InternalRelationType instances
 */
protected static HashMap<Long, MizoTitanRelationType> loadRelationTypes(String titanConfigPath) {
    TitanGraph g = TitanFactory.open(titanConfigPath);
    StandardTitanTx tx = (StandardTitanTx)g.buildTransaction().readOnly().start();

    HashMap<Long, MizoTitanRelationType> relations = Maps.newHashMap();

    tx.query()
            .has(BaseKey.SchemaCategory, Contain.IN, Lists.newArrayList(TitanSchemaCategory.values()))
            .vertices()
            .forEach(v -> {
                if (v instanceof InternalRelationType)
                    relations.put(v.longId(), new MizoTitanRelationType((InternalRelationType)v));
            });

    tx.close();

    try {
        ((StandardTitanGraph)g).getBackend().close();
    } catch (BackendException e) {
        e.printStackTrace();
    }

    return relations;
}
 
Example #4
Source File: SchemaContainer.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
public SchemaContainer(TitanGraph graph) {
    vertexLabels = Maps.newHashMap();
    relationTypes = Maps.newHashMap();
    TitanManagement mgmt = graph.openManagement();

    try {
        for (VertexLabel vl : mgmt.getVertexLabels()) {
            VertexLabelDefinition vld = new VertexLabelDefinition(vl);
            vertexLabels.put(vld.getName(),vld);
        }

        for (EdgeLabel el : mgmt.getRelationTypes(EdgeLabel.class)) {
            EdgeLabelDefinition eld = new EdgeLabelDefinition(el);
            relationTypes.put(eld.getName(),eld);
        }
        for (PropertyKey pk : mgmt.getRelationTypes(PropertyKey.class)) {
            PropertyKeyDefinition pkd = new PropertyKeyDefinition(pk);
            relationTypes.put(pkd.getName(), pkd);
        }
    } finally {
        mgmt.rollback();
    }

}
 
Example #5
Source File: TitanCleanup.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
/**
 * Clears out the entire graph. This will delete ALL of the data stored in this graph and the data will NOT be
 * recoverable. This method is intended only for development and testing use.
 *
 * @param graph
 * @throws IllegalArgumentException if the graph has not been shut down
 * @throws com.thinkaurelius.titan.core.TitanException if clearing the storage is unsuccessful
 */
public static final void clear(TitanGraph graph) {
    Preconditions.checkNotNull(graph);
    Preconditions.checkArgument(graph instanceof StandardTitanGraph,"Invalid graph instance detected: %s",graph.getClass());
    StandardTitanGraph g = (StandardTitanGraph)graph;
    Preconditions.checkArgument(!g.isOpen(),"Graph needs to be shut down before it can be cleared.");
    final GraphDatabaseConfiguration config = g.getConfiguration();
    BackendOperation.execute(new Callable<Boolean>(){
        @Override
        public Boolean call() throws Exception {
            config.getBackend().clearStorage();
            return true;
        }
        @Override
        public String toString() { return "ClearBackend"; }
    }, Duration.ofSeconds(20));
}
 
Example #6
Source File: Driver.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
private void doWork()
    {
        TitanGraph graph = TitanFactory.build().set("storage.backend", "inmemory").open();

        // do "auto" transaction work
        graph.addVertex("type", "human");
        graph.tx().commit();

        // do "named" transaction work
//        TitanTransaction tx = graph.buildTransaction().logIdentifier("david1").start();
//        Vertex v = tx.addVertex("type", "dog");
//        Long id = (Long)v.id();
//        tx.commit();

//        GraphTraversalSource g = graph.traversal();
//        Vertex v1 = g.V(id.longValue()).next();
//        v1.remove();

        graph.close();
    }
 
Example #7
Source File: Titan1GraphDatabase.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
public static TitanGraph getGraphInstance() {
    if (graphInstance == null) {
        synchronized (Titan1GraphDatabase.class) {
            if (graphInstance == null) {
                Configuration config;
                try {
                    config = getConfiguration();
                } catch (AtlasException e) {
                    throw new RuntimeException(e);
                }

                graphInstance = TitanFactory.open(config);
                atlasGraphInstance = new Titan1Graph();
                validateIndexBackend(config);
            }
        }
    }
    return graphInstance;
}
 
Example #8
Source File: ElasticSearchConfigTest.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTitanFactoryBuilder()
{
    String baseDir = Joiner.on(File.separator).join("target", "es", "titanfactory_jvmlocal_ext");
    TitanFactory.Builder builder = TitanFactory.build();
    builder.set("storage.backend", "inmemory");
    builder.set("index." + INDEX_NAME + ".elasticsearch.interface", "NODE");
    builder.set("index." + INDEX_NAME + ".elasticsearch.ext.node.data", "true");
    builder.set("index." + INDEX_NAME + ".elasticsearch.ext.node.client", "false");
    builder.set("index." + INDEX_NAME + ".elasticsearch.ext.node.local", "true");
    builder.set("index." + INDEX_NAME + ".elasticsearch.ext.path.data", baseDir + File.separator + "data");
    builder.set("index." + INDEX_NAME + ".elasticsearch.ext.path.work", baseDir + File.separator + "work");
    builder.set("index." + INDEX_NAME + ".elasticsearch.ext.path.logs", baseDir + File.separator + "logs");
    TitanGraph graph = builder.open(); // Must not throw an exception
    assertTrue(graph.isOpen());
    graph.close();
}
 
Example #9
Source File: Titan0GraphDatabase.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
public static TitanGraph getGraphInstance() {
    if (graphInstance == null) {
        synchronized (Titan0GraphDatabase.class) {
            if (graphInstance == null) {
                Configuration config;
                try {
                    config = getConfiguration();
                } catch (AtlasException e) {
                    throw new RuntimeException(e);
                }

                graphInstance = TitanFactory.open(config);
                atlasGraphInstance = new Titan0Graph();
                validateIndexBackend(config);
            }
        }
    }
    return graphInstance;
}
 
Example #10
Source File: PopulateDB.java    From titan-web-example with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Configuration conf = new PropertiesConfiguration(TitanGraphFactory.PROPS_PATH);

    TitanGraph g = TitanFactory.open(conf);

    // Uncomment the following if your graph is already populated and you want to clear it out first.
    // g.close();
    // TitanCleanup.clear(g);
    // g = TitanFactory.open(conf);

    // Interested in the source?
    // https://github.com/thinkaurelius/titan/blob/titan05/titan-core/src/main/java/com/thinkaurelius/titan/example/GraphOfTheGodsFactory.java
    GraphOfTheGodsFactory.load(g);
    g.close();
    System.out.println("Success.");
}
 
Example #11
Source File: TitanTxLongVersionedGraphTest.java    From antiquity with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected ActiveVersionedGraph<?, Long> generateGraph() {
    File f = new File("/tmp/testgraph");
    if (f.exists()) {
        if (f.isDirectory()) {
            try {
                FileUtils.deleteDirectory(f);
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        } else {
            f.delete();
        }

    }

    Configuration c = new BaseConfiguration();
    c.addProperty("storage.directory","/tmp/testgraph");
    TitanGraph g = TitanFactory.open(c);

    return new ActiveVersionedGraph.ActiveVersionedTransactionalGraphBuilder<TitanGraph, Long>(g, new LongGraphIdentifierBehavior())
            .init(true).conf(null).build();
}
 
Example #12
Source File: AbstractTitanGraphProvider.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
@Override
public void loadGraphData(final Graph g, final LoadGraphWith loadGraphWith, final Class testClass, final String testName) {
    if (loadGraphWith != null) {
        this.createIndices((TitanGraph) g, loadGraphWith.value());
    } else {
        if (TransactionTest.class.equals(testClass) && testName.equalsIgnoreCase("shouldExecuteWithCompetingThreads")) {
            TitanManagement mgmt = ((TitanGraph) g).openManagement();
            mgmt.makePropertyKey("blah").dataType(Double.class).make();
            mgmt.makePropertyKey("bloop").dataType(Integer.class).make();
            mgmt.makePropertyKey("test").dataType(Object.class).make();
            mgmt.makeEdgeLabel("friend").make();
            mgmt.commit();
        }
    }
    super.loadGraphData(g, loadGraphWith, testClass, testName);
}
 
Example #13
Source File: Titan0Graph.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() {
    TitanGraph graph = getGraph();
    if (graph.isOpen()) {
        // only a shut down graph can be cleared
        graph.shutdown();
    }
    TitanCleanup.clear(graph);
}
 
Example #14
Source File: TitanSuite.java    From peapod with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setGraphProvider() throws IOException {
    GraphTest.graphProvider = new GraphProvider() {
        public Graph getGraph() throws IOException {
            TitanGraph graph = TitanFactory.build().set("storage.backend", "inmemory").open();
            TitanManagement management = graph.openManagement();
            management.makePropertyKey("location").dataType(String.class).cardinality(Cardinality.LIST).make();
            management.makePropertyKey("firstName").dataType(String.class).cardinality(Cardinality.LIST).make();
            management.commit();
            return graph;
        }
    };

}
 
Example #15
Source File: RestaurantFactory.java    From aws-big-data-blog with Apache License 2.0 5 votes vote down vote up
private static Vertex get(TitanGraph graph, String key, String value) {
	Iterator<Vertex> it = graph.getVertices(key, value).iterator();
	Vertex vertex = null;
	if (it.hasNext()) {
		vertex = it.next();
	}
	return vertex;

}
 
Example #16
Source File: Titan1Graph.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() {
    TitanGraph graph = getGraph();
    if (graph.isOpen()) {
        // only a shut down graph can be cleared
        graph.close();
    }
    TitanCleanup.clear(graph);
}
 
Example #17
Source File: TitanGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public void createGraphForMassiveLoad()
{
    open(true /* batchLoading */);
    createSchema();

    batchGraph = new BatchGraph<TitanGraph>(titanGraph, VertexIDType.NUMBER, 100000 /* bufferSize */);
    batchGraph.setVertexIdKey(NODE_ID);
    batchGraph.setLoadingFromScratch(true /* fromScratch */);
}
 
Example #18
Source File: IndexUpdateJob.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public void workerIterationStart(TitanGraph graph, Configuration config, ScanMetrics metrics) {
    this.graph = (StandardTitanGraph)graph;
    Preconditions.checkArgument(config.has(GraphDatabaseConfiguration.JOB_START_TIME),"Invalid configuration for this job. Start time is required.");
    this.jobStartTime = Instant.ofEpochMilli(config.get(GraphDatabaseConfiguration.JOB_START_TIME));
    if (indexName == null) {
        Preconditions.checkArgument(config.has(INDEX_NAME), "Need to configure the name of the index to be repaired");
        indexName = config.get(INDEX_NAME);
        indexRelationTypeName = config.get(INDEX_RELATION_TYPE);
        log.info("Read index information: name={} type={}", indexName, indexRelationTypeName);
    }

    try {
        this.mgmt = (ManagementSystem)graph.openManagement();

        if (isGlobalGraphIndex()) {
            index = mgmt.getGraphIndex(indexName);
        } else {
            indexRelationType = mgmt.getRelationType(indexRelationTypeName);
            Preconditions.checkArgument(indexRelationType!=null,"Could not find relation type: %s", indexRelationTypeName);
            index = mgmt.getRelationIndex(indexRelationType,indexName);
        }
        Preconditions.checkArgument(index!=null,"Could not find index: %s [%s]",indexName,indexRelationTypeName);
        log.info("Found index {}", indexName);
        validateIndexStatus();

        StandardTransactionBuilder txb = this.graph.buildTransaction();
        txb.commitTime(jobStartTime);
        writeTx = (StandardTitanTx)txb.start();
    } catch (final Exception e) {
        if (null != mgmt && mgmt.isOpen())
            mgmt.rollback();
        if (writeTx!=null && writeTx.isOpen())
            writeTx.rollback();
        metrics.incrementCustom(FAILED_TX);
        throw new TitanException(e.getMessage(), e);
    }
}
 
Example #19
Source File: GraphOfTheGodsFactory.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static TitanGraph create(final String directory) {
    TitanFactory.Builder config = TitanFactory.build();
    config.set("storage.backend", "berkeleyje");
    config.set("storage.directory", directory);
    config.set("index." + INDEX_NAME + ".backend", "elasticsearch");
    config.set("index." + INDEX_NAME + ".directory", directory + File.separator + "es");
    config.set("index." + INDEX_NAME + ".elasticsearch.local-mode", true);
    config.set("index." + INDEX_NAME + ".elasticsearch.client-only", false);

    TitanGraph graph = config.open();
    GraphOfTheGodsFactory.load(graph);
    return graph;
}
 
Example #20
Source File: HadoopVertexScanMapper.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
protected void setup(Context context) throws IOException, InterruptedException {
    /* Don't call super implementation super.setup(context); */
    org.apache.hadoop.conf.Configuration hadoopConf = DEFAULT_COMPAT.getContextConfiguration(context);
    ModifiableHadoopConfiguration scanConf = ModifiableHadoopConfiguration.of(TitanHadoopConfiguration.MAPRED_NS, hadoopConf);
    VertexScanJob vertexScan = getVertexScanJob(scanConf);
    ModifiableConfiguration graphConf = getTitanConfiguration(context);
    TitanGraph graph = TitanFactory.open(graphConf);
    job = VertexJobConverter.convert(graph, vertexScan);
    metrics = new HadoopContextScanMetrics(context);
    finishSetup(scanConf, graphConf);
}
 
Example #21
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static void assertGraphOfTheGods(TitanGraph gotg) {
    assertCount(12, gotg.query().vertices());
    assertCount(3, gotg.query().has(LABEL_NAME, "god").vertices());
    TitanVertex h = getOnlyVertex(gotg.query().has("name", "hercules"));
    assertEquals(30, h.<Integer>value("age").intValue());
    assertEquals("demigod", h.label());
    assertCount(5, h.query().direction(Direction.BOTH).edges());
    gotg.tx().commit();
}
 
Example #22
Source File: BerkeleyElasticsearchTest.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
/**
 * Test {@link com.thinkaurelius.titan.example.GraphOfTheGodsFactory#create(String)}.
 */
@Test
public void testGraphOfTheGodsFactoryCreate() {
    File bdbtmp = new File("target/gotgfactory");
    IOUtils.deleteDirectory(bdbtmp, true);

    TitanGraph gotg = GraphOfTheGodsFactory.create(bdbtmp.getPath());
    TitanIndexTest.assertGraphOfTheGods(gotg);
    gotg.close();
}
 
Example #23
Source File: VertexIDAssignerTest.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static TitanGraph getInMemoryGraph() {
    ModifiableConfiguration config = GraphDatabaseConfiguration.buildGraphConfiguration();
    config.set(GraphDatabaseConfiguration.STORAGE_BACKEND, InMemoryStoreManager.class.getCanonicalName());
    config.set(GraphDatabaseConfiguration.IDS_FLUSH, false);
    config.set(GraphDatabaseConfiguration.IDAUTHORITY_WAIT, Duration.ofMillis(1L));
    return TitanFactory.open(config);
}
 
Example #24
Source File: TinkerpopFeaturesTest.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public TitanGraph open(boolean useVertexIdSetting)
{
    TitanFactory.Builder builder = TitanFactory.build();
    builder.set("storage.backend", "inmemory");
    builder.set("graph.set-vertex-id", useVertexIdSetting);
    return builder.open();
}
 
Example #25
Source File: ManagementUtil.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public static void awaitVertexIndexUpdate(TitanGraph g, String indexName, String relationTypeName, long time, TemporalUnit unit) {
    awaitIndexUpdate(g,indexName,relationTypeName,time,unit);
}
 
Example #26
Source File: GraphOfTheGodsFactory.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public static void loadWithoutMixedIndex(final TitanGraph graph, boolean uniqueNameCompositeIndex) {
    load(graph, null, uniqueNameCompositeIndex);
}
 
Example #27
Source File: Titan1Graph.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
private TitanGraph getGraph() {
    return Titan1GraphDatabase.getGraphInstance();
}
 
Example #28
Source File: MapReduceIndexManagement.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
public MapReduceIndexManagement(TitanGraph g) {
    this.graph = (StandardTitanGraph)g;
}
 
Example #29
Source File: Titan0Graph.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
private TitanGraph getGraph() {
    // return the singleton instance of the graph in the plugin
    return Titan0GraphDatabase.getGraphInstance();
}
 
Example #30
Source File: TitanFactoryShorthandTest.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Test
public void testTitanFactoryShorthand() {
    TitanGraph g = TitanFactory.open("inmemory");
    g.close();
}