我有以下使用Titan创建图形数据库的代码:
public class TitanMassiveInsertion {
private TitanGraph titanGraph = null;
private BatchGraph<TitanGraph> batchGraph = null;
public static void main(String args[]) {
TitanMassiveInsertion test = new TitanMassiveInsertion();
test.startup("data/titanDB");
test.createGraph("data/flickrEdges.txt");
test.shutdown();
}
public void startup(String titanDBDir) {
System.out.println("The Titan database is now starting . . . .");
BaseConfiguration config = new BaseConfiguration();
Configuration storage = config.subset(GraphDatabaseConfiguration.STORAGE_NAMESPACE);
storage.setProperty(GraphDatabaseConfiguration.STORAGE_BACKEND_KEY, "local");
storage.setProperty(GraphDatabaseConfiguration.STORAGE_DIRECTORY_KEY, titanDBDir);
storage.setProperty(GraphDatabaseConfiguration.STORAGE_BATCH_KEY, true);
Configuration index = storage.subset(GraphDatabaseConfiguration.INDEX_NAMESPACE).subset("nodes");
index.setProperty(INDEX_BACKEND_KEY, "elasticsearch");
index.setProperty("local-mode", true);
index.setProperty("client-only", false);
index.setProperty(STORAGE_DIRECTORY_KEY, titanDBDir + File.separator + "es");
titanGraph = TitanFactory.open(config);
titanGraph.makeKey("nodeId").dataType(String.class).indexed(Vertex.class).make();
titanGraph.makeLabel("similar").oneToOne().make();
titanGraph.commit();
batchGraph = new BatchGraph<TitanGraph>(titanGraph, VertexIDType.STRING, 1000);
batchGraph.setVertexIdKey("nodeId");
batchGraph.setLoadingFromScratch(true);
}
public void shutdown() {
System.out.println("The Titan database is now shuting down . . . .");
if(titanGraph != null) {
batchGraph.shutdown();
titanGraph.shutdown();
batchGraph = null;
titanGraph = null;
}
}
public void createGraph(String datasetDir) {
System.out.println("Creating the Titan database . . . .");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(datasetDir)));
String line;
int lineCounter = 1;
int nodeCounter = 0;
Vertex srcVertex, dstVertex;
while((line = reader.readLine()) != null) {
if(lineCounter > 4) {
String[] parts = line.split("\t");
srcVertex = batchGraph.getVertex(parts[0]);
if(srcVertex == null) {
srcVertex = batchGraph.addVertex(parts[0]);
srcVertex.setProperty("nodeId", parts[0]);
nodeCounter++;
}
dstVertex = batchGraph.getVertex(parts[1]);
if(dstVertex == null) {
dstVertex = batchGraph.addVertex(parts[1]);
dstVertex.setProperty("nodeId", parts[1]);
nodeCounter++;
}
Edge edge = batchGraph.addEdge(null, srcVertex, dstVertex, "similar");
//System.out.println(edge);
System.out.println(nodeCounter);
}
lineCounter++;
}
reader.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
创建图形数据库之后,我想测试我的代码,看看一切正常,所以我试图用这个测试代码打印顶点:
TitanGraph graph = TitanFactory.open("data/titanDB");
for(Vertex v : graph.getVertices()) {
System.out.println(v.getProperty("nodeId"));
}
graph.shutdown();
但它没有打印任何内容,它似乎堆叠在一个无限循环中。我也试过了
Vertex v : graph.query().vertices()
,正如教程所示,但问题依然存在。另一方面,当我System.out.println(graph.getVertices("nodeId", "2604051056"));
时,我得到了正确的输出。 getVertices()函数有问题吗?
答案 0 :(得分:1)
您正在使用“本地”存储后端,这意味着默认情况下为BerkeleyJE。 BerkeleyJE默认启用事务,这意味着它必须跟踪它接触的每个数据元素以提供正确的隔离。但是,当您基本上使用顶点扫描触摸所有数据时,这很昂贵。
因此,请尝试禁用交易
storage.transactions = false