HugeGraph是一个Java语言实现的开源图数据库,内置支持多种后端存储(Cassandra、RocksDB等),此外还支持通过插件的方式扩展存储后端。插件实现者无需更改HugeGraph本身源代码,插件使用者无需关注代码细节,就能轻松增加插件扩展功能。这篇文章主要讲述了HugeGraph扩展自定义插件的流程。
目前插件方式提供如下几个维度的扩展项:
registerOptions()
、registerBackend()
、registerSerializer()
、registerAnalyzer()
register()
方法,在register()
中调用上述第2步中的函数,来注册第3步列出的具体实现类,并打成jar包plugins
目录下,修改相关配置项为插件自定义值,重启即可生效hugegraph-core
Jar包依赖maven pom.xml详细内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-plugin-demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>hugegraph-plugin-demo</name>
<dependencies>
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
扩展功能一般包括:扩展自定义后端存储(下文2.1讲述)和扩展自定义分词器(下文2.2讲述)。插件实现者关注其中之一即可,若有需要当然也可两者同时进行扩展,或者只扩展下文2.1.3所述系列化器等。
com.baidu.hugegraph.backend.store.BackendStoreProvider
com.baidu.hugegraph.backend.store.AbstractBackendStoreProvider
以RocksDB后端RocksDBStoreProvider为例:
public class RocksDBStoreProvider extends AbstractBackendStoreProvider {
protected String database() {
return this.graph().toLowerCase();
}
@Override
protected BackendStore newSchemaStore(String store) {
return new RocksDBSchemaStore(this, this.database(), store);
}
@Override
protected BackendStore newGraphStore(String store) {
return new RocksDBGraphStore(this, this.database(), store);
}
@Override
public String type() {
return "rocksdb";
}
@Override
public String version() {
return "1.0";
}
}
BackendStore接口定义如下:
public interface BackendStore {
// Store name
public String store();
// Database name
public String database();
// Get the parent provider
public BackendStoreProvider provider();
// Open/close database
public void open(HugeConfig config);
public void close();
// Initialize/clear database
public void init();
public void clear();
// Add/delete data
public void mutate(BackendMutation mutation);
// Query data
public Iterator<BackendEntry> query(Query query);
// Transaction
public void beginTx();
public void commitTx();
public void rollbackTx();
// Get metadata by key
public <R> R metadata(HugeType type, String meta, Object[] args);
// Backend features
public BackendFeatures features();
// Generate an id for a specific type
public Id nextId(HugeType type);
}
系列化器必须继承抽象类:com.baidu.hugegraph.backend.serializer.AbstractSerializer
(implements GraphSerializer, SchemaSerializer
)
主要接口的定义如下:
public interface GraphSerializer {
public BackendEntry writeVertex(HugeVertex vertex);
public BackendEntry writeVertexProperty(HugeVertexProperty<?> prop);
public HugeVertex readVertex(HugeGraph graph, BackendEntry entry);
public BackendEntry writeEdge(HugeEdge edge);
public BackendEntry writeEdgeProperty(HugeEdgeProperty<?> prop);
public HugeEdge readEdge(HugeGraph graph, BackendEntry entry);
public BackendEntry writeIndex(HugeIndex index);
public HugeIndex readIndex(HugeGraph graph, ConditionQuery query, BackendEntry entry);
public BackendEntry writeId(HugeType type, Id id);
public Query writeQuery(Query query);
}
public interface SchemaSerializer {
public BackendEntry writeVertexLabel(VertexLabel vertexLabel);
public VertexLabel readVertexLabel(HugeGraph graph, BackendEntry entry);
public BackendEntry writeEdgeLabel(EdgeLabel edgeLabel);
public EdgeLabel readEdgeLabel(HugeGraph graph, BackendEntry entry);
public BackendEntry writePropertyKey(PropertyKey propertyKey);
public PropertyKey readPropertyKey(HugeGraph graph, BackendEntry entry);
public BackendEntry writeIndexLabel(IndexLabel indexLabel);
public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry entry);
}
增加自定义后端时,可能需要增加新的配置项,实现流程主要包括:
com.baidu.hugegraph.config.OptionHolder
public static OptionHolder instance()
,并在对象初始化时调用方法OptionHolder.registerOptions()
ConfigOption
、多值配置项类型为ConfigListOption
以RocksDB配置项定义为例:
public class RocksDBOptions extends OptionHolder {
private RocksDBOptions() {
super();
}
private static volatile RocksDBOptions instance;
public static synchronized RocksDBOptions instance() {
if (instance == null) {
instance = new RocksDBOptions();
instance.registerOptions();
}
return instance;
}
public static final ConfigOption<String> DATA_PATH =
new ConfigOption<>(
"rocksdb.data_path",
"The path for storing data of RocksDB.",
disallowEmpty(),
"rocksdb-data"
);
public static final ConfigOption<String> WAL_PATH =
new ConfigOption<>(
"rocksdb.wal_path",
"The path for storing WAL of RocksDB.",
disallowEmpty(),
"rocksdb-data"
);
public static final ConfigListOption<String> DATA_DISKS =
new ConfigListOption<>(
"rocksdb.data_disks",
false,
"The optimized disks for storing data of RocksDB. " +
"The format of each element: `STORE/TABLE: /path/to/disk`." +
"Allowed keys are [graph/vertex, graph/edge_out, graph/edge_in, " +
"graph/secondary_index, graph/range_index]",
null,
String.class,
ImmutableList.of()
);
}
分词器需要实现接口com.baidu.hugegraph.analyzer.Analyzer
,以实现一个SpaceAnalyzer空格分词器为例。
package com.baidu.hugegraph.plugin;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import com.baidu.hugegraph.analyzer.Analyzer;
public class SpaceAnalyzer implements Analyzer {
@Override
public Set<String> segment(String text) {
return new HashSet<>(Arrays.asList(text.split(" ")));
}
}
插件注册入口为HugeGraphPlugin.register()
,自定义插件必须实现该接口方法,在其内部注册上述定义好的扩展项。
接口com.baidu.hugegraph.plugin.HugeGraphPlugin
定义如下:
public interface HugeGraphPlugin {
public String name();
public void register();
public String supportsMinVersion();
public String supportsMaxVersion();
}
并且HugeGraphPlugin提供了4个静态方法用于注册扩展项:
下面以注册SpaceAnalyzer分词器为例:
package com.baidu.hugegraph.plugin;
public class DemoPlugin implements HugeGraphPlugin {
@Override
public String name() {
return "demo";
}
@Override
public void register() {
HugeGraphPlugin.registerAnalyzer("demo", SpaceAnalyzer.class.getName());
}
}
通过maven打包,在项目目录下执行命令mvn package
,在target目录下会生成Jar包文件。
使用插件时将该Jar包拷到plugins
目录,重启服务即可生效。
HugeGraph扩展Stanford分词器完整示例: https://github.com/hugegraph/hugegraph-plugin-stanford-analyzer
HugeGraph GitHub地址: https://github.com/hugegraph/hugegraph