在进行hugegrpah的使用的时候需要进行预先的初始化:
执行 com.baidu.hugegraph.cmd 中的main 进行图的初始化
private static void initGraph(String configPath) throws Exception {
LOG.info("Init graph with config file: {}", configPath);
HugeConfig config = new HugeConfig(configPath);
// Forced set to false when initializing backend
config.setProperty(CoreOptions.RAFT_MODE.name(), "false");
HugeGraph graph = (HugeGraph) GraphFactory.open(config);
BackendStoreSystemInfo sysInfo = graph.backendStoreSystemInfo();
try {
if (sysInfo.exists()) {
LOG.info("Skip init-store due to the backend store of '{}' " +
"had been initialized", graph.name());
sysInfo.checkVersion();
} else {
initBackend(graph); //主要是初始化后端
}
} finally {
graph.close();
}
}
初始化后端存储:
int retries = RETRIES;
retry: do {
try {
graph.initBackend();
} catch (Exception e) {
String clz = e.getClass().getSimpleName();
String message = e.getMessage();
if (exceptions.containsKey(clz) && retries > 0) {
@SuppressWarnings("unchecked")
Collection<String> keywords = exceptions.getCollection(clz);
for (String keyword : keywords) {
if (message.contains(keyword)) {
LOG.info("Init failed with exception '{} : {}', " +
"retry {}...",
clz, message, RETRIES - retries + 1);
Thread.sleep(RETRY_INTERVAL);
continue retry;
}
}
}
throw e;
}
break;
} while (retries-- > 0);
}
}
这里主要是graph.initBackend();
跟踪接口的实现我们走到StandardHugegraph
@Override
public void initBackend() {
this.loadSchemaStore().open(this.configuration); //schema 初始化
this.loadSystemStore().open(this.configuration); //system 系统信息初始化
this.loadGraphStore().open(this.configuration); //graph//图存储初始化
LockUtil.lock(this.name, LockUtil.GRAPH_LOCK);
try {
this.storeProvider.init(); //存储提供者初始化
this.storeProvider.initSystemInfo(this);//初始化系统信息
} finally {
LockUtil.unlock(this.name, LockUtil.GRAPH_LOCK);
this.loadGraphStore().close();
this.loadSystemStore().close();
this.loadSchemaStore().close();
}
LOG.info("Graph '{}' has been initialized", this.name);
}
loadSchemaStore 可以看到是通过父类调用子类的实现,这里我们看看抽象类中的实现
private BackendStore loadSchemaStore() {
String name = this.configuration.get(CoreOptions.STORE_SCHEMA);
return this.storeProvider.loadSchemaStore(name);
}
//抽象类的实现
public BackendStore loadSchemaStore(final String name) {
LOG.debug("The '{}' StoreProvider load SchemaStore '{}'",
this.type(), name);
this.checkOpened();
if (!this.stores.containsKey(name)) {
BackendStore s = this.newSchemaStore(name);
this.stores.putIfAbsent(name, s);
}
BackendStore store = this.stores.get(name);
E.checkNotNull(store, "store");
return store;
}
可以看到抽象类是通过名字来创建相应的schema hbase的具体实现:
@Override
protected BackendStore newSchemaStore(String store) {
return new HbaseSchemaStore(this, this.namespace(), store);
}
public HbaseSchemaStore(BackendStoreProvider provider,
String namespace, String store) {
super(provider, namespace, store);
this.counters = new HbaseTables.Counters();
registerTableManager(HugeType.VERTEX_LABEL,
new HbaseTables.VertexLabel());
registerTableManager(HugeType.EDGE_LABEL,
new HbaseTables.EdgeLabel());
registerTableManager(HugeType.PROPERTY_KEY,
new HbaseTables.PropertyKey());
registerTableManager(HugeType.INDEX_LABEL,
new HbaseTables.IndexLabel());
registerTableManager(HugeType.SECONDARY_INDEX,
new HbaseTables.SecondaryIndex(store));
}
可以看到HbaseSchemaStore中创建了不同的表 包括VERTEX_LABEL,EDGE_LABEL,PROPERTY_KEY,INDEX_LABEL,SECONDARY_INDEX
schemastore 加载完成之后接着加载systemStore
this.loadSystemStore().open(this.configuration);
loadSystemStore走到最后hbase的具体实现是:
public HbaseGraphStore(BackendStoreProvider provider,
String namespace, String store) {
super(provider, namespace, store);
registerTableManager(HugeType.VERTEX,
new HbaseTables.Vertex(store));
registerTableManager(HugeType.EDGE_OUT,
HbaseTables.Edge.out(store));
registerTableManager(HugeType.EDGE_IN,
HbaseTables.Edge.in(store));
registerTableManager(HugeType.SECONDARY_INDEX,
new HbaseTables.SecondaryIndex(store));
registerTableManager(HugeType.VERTEX_LABEL_INDEX,
new HbaseTables.VertexLabelIndex(store));
registerTableManager(HugeType.EDGE_LABEL_INDEX,
new HbaseTables.EdgeLabelIndex(store));
registerTableManager(HugeType.RANGE_INT_INDEX,
HbaseTables.RangeIndex.rangeInt(store));
registerTableManager(HugeType.RANGE_FLOAT_INDEX,
HbaseTables.RangeIndex.rangeFloat(store));
registerTableManager(HugeType.RANGE_LONG_INDEX,
HbaseTables.RangeIndex.rangeLong(store));
registerTableManager(HugeType.RANGE_DOUBLE_INDEX,
HbaseTables.RangeIndex.rangeDouble(store));
registerTableManager(HugeType.SEARCH_INDEX,
new HbaseTables.SearchIndex(store));
registerTableManager(HugeType.SHARD_INDEX,
new HbaseTables.ShardIndex(store));
registerTableManager(HugeType.UNIQUE_INDEX,
new HbaseTables.UniqueIndex(store));
}
关联的表如下图:
本文由 妖言君 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Apr 3, 2022 at 11:55 am