Titan 1.0 [Berkeley+ Remote Elastic Search] 我们使用的是 Titan 的这种组合。以下是属性文件 -
storage.backend=berkeleyje
storage.directory=D:/other-projects/graph-db/titan/enron-tk3/db/berkeley
index.search.backend=elasticsearch
index.search.index-name=akshayatitan
index.search.hostname=localhost
index.search.elasticsearch.client-only=true
index.search.elasticsearch.local-mode=false
我们只使用混合索引。
现在我们通过 Java 代码添加一个具有少量属性的节点,然后获取它。
我们通过在其上创建混合索引的属性进行查询。
当我们通过键(创建混合索引)查询节点时,我们不会立即获取节点。但是,它会在延迟后可用。
我们做错了什么?还是预期会延迟更新 ES 实例?
这是Java代码。
public static void main(String[] args) throws Exception {
GraphTest test = new GraphTest();
test.init();
Thread.sleep(10000);
String emailId = "emailId" + System.nanoTime();
test.createNode(emailId);
System.out.println("Create " + emailId);
System.out.println("First time " + test.getNode(emailId));
Thread.sleep(2000);
System.out.println("After a delay of 2 sec " + test.getNode(emailId));
}
public void createNode(String emailid) {
Vertex vertex = graph.addVertex("person");
vertex.property("emailId", emailid);
vertex.property("firstName", "First Name");
vertex.property("lastName", "Last Name");
vertex.property("address", "Address");
vertex.property("hometown", "Noida");
vertex.property("city", "Noida");
vertex.property("spousename", "Preeti");
graph.tx().commit();
}
public Object getNode(String emailId) {
Vertex vert = null;
String reString = null;
try {
vert = graph.traversal().V().has("emailId", emailId).next();
reString = vert.value("emailId");
} catch (NoSuchElementException e) {
e.printStackTrace();
} finally {
graph.tx().close();
}
return reString;
}
创建索引的代码是 -
private void createMixedIndexForVertexProperty(String indexName, String propertyKeyName, Class<?> propertyType) {
TitanManagement mgmt = ((TitanGraph) graph).openManagement();
try {
PropertyKey propertyKey = makePropertyKey(propertyKeyName, propertyType, mgmt);
TitanGraphIndex graphIndex = mgmt.getGraphIndex(indexName);
if (graphIndex == null) {
graphIndex = mgmt.buildIndex(indexName, Vertex.class)
.addKey(propertyKey, Parameter.of("mapping", Mapping.STRING)).buildMixedIndex("search");
} else {
mgmt.addIndexKey(graphIndex, propertyKey);
}
mgmt.commit();
} catch (Exception e) {
mgmt.rollback();
} finally {
}
}
public PropertyKey makePropertyKey(String propertyKeyName, Class<?> propertyType, TitanManagement mgmt) {
PropertyKey propertyKey = mgmt.getPropertyKey(propertyKeyName);
if (propertyKey == null) {
propertyKey = mgmt.makePropertyKey(propertyKeyName).dataType(String.class).make();
}
return propertyKey;
}
public void init() throws Exception {
graph = TitanFactory
.open(new PropertiesConfiguration(new File("src/test/resources/titan-berkeleydb-es.properties")));
createMixedIndexForVertexProperty("personnode", "emailId", String.class);
createMixedIndexForVertexProperty("personnode", "firstName", String.class);
createMixedIndexForVertexProperty("personnode", "lastName", String.class);
createMixedIndexForVertexProperty("personnode", "address", String.class);
createMixedIndexForVertexProperty("personnode", "hometown", String.class);
createMixedIndexForVertexProperty("personnode", "city", String.class);
createMixedIndexForVertexProperty("personnode", "spousename", String.class);
}