HugeGraph Plugin mechanism and plug-in extension process
For plugins targeting the planned 1.8.0 shared-foundation API, follow the
Java/SPI migration guide and recompile against matching artifacts.
The examples below describe the 1.7.0 API.
Background
HugeGraph is not only open source and open, but also simple and easy to use. General users can easily add plug-in extension functions without changing the source code.
HugeGraph supports a variety of built-in storage backends, and also allows users to extend custom backends without changing the existing source code.
HugeGraph supports full-text search. The full-text search function involves word segmentation in various languages. Currently, there are 7 built-in word
breakers (ansj, hanlp, smartcn, jieba, jcseg, mmseg4j, ikanalyzer), and it also allows users to expand custom word breakers without changing the existing source code.
Scalable dimension
Currently, the plug-in method provides extensions in the following dimensions:
backend storage
serializer
Custom configuration items
tokenizer
Plug-in implementation mechanism
Provide SPI interface: HugeGraph provides the HugeGraphPlugin interface, supporting pluggability via Java SPI.
Implement components: The developer implements the corresponding OptionHolder, BackendStoreProvider, AbstractSerializer, or Analyzer interfaces.
Register in plugin: The developer implements the register() method of HugeGraphPlugin to register implementation classes, declared in META-INF/services/.
Deploy and activate: Place the packaged JAR in the HugeGraph Server plugins/ directory, configure the custom options, and restart the service.
Plug-in implementation process example
1 Create a new maven project
1.1 Name the project name: hugegraph-plugin-demo
1.2 Add hugegraph-core Jar package dependencies
The Maven pom.xml is shown below. Match hugegraph.version to the target Server version; this example uses the current master version 1.7.0:
The main abstract methods of BackendStore are shown below; default helper methods are omitted:
1publicinterfaceBackendStore{ 2// Store name 3Stringstore(); 4 5// Stored version 6StringstoredVersion(); 7 8// Database name 9Stringdatabase();1011// Get the parent provider12BackendStoreProviderprovider();1314// Get the system schema store15SystemSchemaStoresystemSchemaStore();1617// Whether it is the storage of schema18booleanisSchemaStore();1920// Open/close database21voidopen(HugeConfigconfig);22voidclose();23booleanopened();2425// Initialize/clear database26voidinit();27voidclear(booleanclearSpace);28booleaninitialized();2930// Delete all data of database (keep table structure)31voidtruncate();3233// Add/delete data34voidmutate(BackendMutationmutation);3536// Query data37Iterator<BackendEntry>query(Queryquery);38Iterator<Iterator<BackendEntry>>query(Iterator<Query>queries,39Function<Query,Query>queryWriter,40HugeGraphhugeGraph);41NumberqueryNumber(Queryquery);4243// Transaction44voidbeginTx();45voidcommitTx();46voidrollbackTx();4748// Get metadata by key49<R>Rmetadata(HugeTypetype,Stringmeta,Object[]args);5051// Backend features52BackendFeaturesfeatures();5354// Increase next id for specific type55voidincreaseCounter(HugeTypetype,longincrement);5657// Get current counter for a specific type58longgetCounter(HugeTypetype);59}
2.1.3 Extending custom serializers
The serializer must inherit the abstract class: org.apache.hugegraph.backend.serializer.AbstractSerializer
( implements GraphSerializer, SchemaSerializer) The main interface is defined as follows:
When adding a custom backend, it may be necessary to add new configuration items. The implementation process mainly includes:
Add a configuration item container class and implement the interface org.apache.hugegraph.config.OptionHolder
Provide a singleton method public static OptionHolder instance(), and call the method when the object is initialized OptionHolder.registerOptions()
Add configuration item declaration, single-value configuration item type is ConfigOption, multi-value configuration item type is ConfigListOption
Take the RocksDB configuration item definition as an example:
1publicclassRocksDBOptionsextendsOptionHolder{ 2 3privateRocksDBOptions(){ 4super(); 5} 6 7privatestaticvolatileRocksDBOptionsinstance; 8 9publicstaticsynchronizedRocksDBOptionsinstance(){10if(instance==null){11instance=newRocksDBOptions();12instance.registerOptions();13}14returninstance;15}1617publicstaticfinalConfigOption<String>DATA_PATH=18newConfigOption<>(19"rocksdb.data_path",20"The path for storing data of RocksDB.",21disallowEmpty(),22"rocksdb-data/data"23);2425publicstaticfinalConfigOption<String>WAL_PATH=26newConfigOption<>(27"rocksdb.wal_path",28"The path for storing WAL of RocksDB.",29disallowEmpty(),30"rocksdb-data/wal"31);3233publicstaticfinalConfigListOption<String>DATA_DISKS=34newConfigListOption<>(35"rocksdb.data_disks",36false,37"The optimized disks for storing data of RocksDB. "+38"The format of each element: `STORE/TABLE: /path/disk`."+39"Allowed keys are [g/vertex, g/edge_out, g/edge_in, "+40"g/vertex_label_index, g/edge_label_index, "+41"g/range_int_index, g/range_float_index, "+42"g/range_long_index, g/range_double_index, "+43"g/secondary_index, g/search_index, g/shard_index, "+44"g/unique_index, g/olap]",45null,46String.class,47ImmutableList.of()48);49}
2.2 Extend custom tokenizer
The tokenizer needs to implement the interface org.apache.hugegraph.analyzer.Analyzer, take implementing a SpaceAnalyzer space tokenizer as an example.
3. Implement the plug-in interface and register it
The plug-in registration entry is HugeGraphPlugin.register(), the custom plug-in must implement this interface method, and register the extension
items defined above inside it. The interface org.apache.hugegraph.plugin.HugeGraphPlugin is defined as follows:
Make sure the services directory exists: hugegraph-plugin-demo/resources/META-INF/services
Create a text file in the services directory: org.apache.hugegraph.plugin.HugeGraphPlugin
The content of the file is as follows: org.apache.hugegraph.plugin.DemoPlugin
5. Make Jar package
Through maven packaging, execute the command in the project directory mvn package, and a Jar package file will be generated in the
target directory. Copy the Jar package to the plugins directory when using it, and restart the service to take effect.
Search in
Ask AI
Agree to send your questions to the third-party Kapa service for AI answers based on the documentation.