繁体   English   中英

无法检索Neo4j中的索引节点

[英]Cannot retrieve indexed nodes in Neo4j

我使用以下代码加载数据:

public void createGraph() {
 Map<String, String> config = new HashMap<String, String>();
 config.put("cache_type", "none");
 config.put("use_memory_mapped_buffers", "true");
 config.put("neostore.nodestore.db.mapped_memory", "200M");
 config.put("neostore.relationshipstore.db.mapped_memory", "1000M");
 config.put("neostore.propertystore.db.mapped_memory", "250M");
 config.put("neostore.propertystore.db.strings.mapped_memory", "250M");
 inserter = BatchInserters.inserter("./data/neo4j", config);

 long start = System.currentTimeMillis();

 try {
 BufferedReader reader = new BufferedReader(new InputStreamReader(new 
 FileInputStream("./data/enronEdges.txt")));
 String line;
 int lineCounter = 1;
 long srcNode, dstNode;
 while((line = reader.readLine()) != null) {
 if(lineCounter > 4) {
 String[] parts = line.split("\t");

 srcNode = getOrCreate(parts[0]);
 dstNode = getOrCreate(parts[1]);

 inserter.createRelationship(srcNode, dstNode, RelTypes.SIMILAR, null);
 }
 lineCounter++;
 }
 reader.close();
 } 
 catch (IOException e) {
 e.printStackTrace();
 }

 long time = System.currentTimeMillis() - start;
 inserter.createDeferredSchemaIndex(NODE_LABEL).on("nodeId").create();
 System.out.println("Loading time: " + time / 1000.0);

 inserter.shutdown();
 }

 private long getOrCreate(String value) {

 Long id = cache.get(Long.valueOf(value));
 if(id == null) {
 Map<String, Object> properties = MapUtil.map("nodeId", value);
 id = inserter.createNode(properties, NODE_LABEL);
 cache.put(Long.valueOf(value), id);
 }
 return id;
 }

然后,我尝试使用以下代码检索节点:

GraphDatabaseService gdb = new GraphDatabaseFactory().newEmbeddedDatabase(dbPath);
try(Transaction tx = gdb.beginTx()) {
 Node n  = gdb.findNodesByLabelAndProperty(DynamicLabel.label("Node"), "nodeId", 1).iterator().next();
 System.out.println(n);

}

但是我收到以下错误:

线程“主”中的异常java.util.NoSuchElementException:org.neo4j.collection.primitive.PrimitiveLongCollections $ PrimitiveLongBaseIterator.next(PrimitiveLongCollections.java:60)上的org.neo4j.collection.primitive.PrimitiveLongCollections$5@6b3ab760中没有更多元素。 org.neo4j.collection.primitive.PrimitiveLongCollections $ 13.next(PrimitiveLongCollections.java:712)在org.neo4j.helpers.collection.ResourceClosingIterator.next(ResourceClosingIterator.java:76)在test.Neo4jBatchInsert.visitJons(56)。 )在test.Neo4jBatchInsert.main(Neo4jBatchInsert.java:49)

这不是获取索引节点的正确方法吗?
仅供参考,我使用neo4j 2.1.3嵌入式。

初始化gdb时会填充索引,因此可能需要一些时间才能使索引联机。

您可以在findNodesByLabelAndProperty之前调用gdb.schema().awaitIndexesOnline(10l, TimeUnits.MINUTES)以确保已建立索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM