Help Center/ Graph Engine Service/ API Reference/ Service Plane APIs/ Database Edition/ HyG Algorithm APIs/ DSL Syntax/ API for Running Custom Algorithms (Currently, the Pregel Programming Model Is Supported)
Updated on 2024-12-03 GMT+08:00

API for Running Custom Algorithms (Currently, the Pregel Programming Model Is Supported)

When the built-in graph analysis algorithms cannot meet the user's needs, HyG allows users to implement custom algorithms using the Python language in the Pregel programming model. The API for running custom Pregel algorithms is:

BaseGraph.run_pregel(model:class, result_filter=None, debug_mode=False)

The model is a class type and a subclass of hyg.analytics.model.PregelModel. Before calling the run_pregel interface, users need to first implement the algorithm calculation logic in their own PregelModel subclass, as shown in Figure 1. First, users need to specify the vertex value type (ntype) and message type (mtype) in the @pregel_type decorator. The mtype can be left unset, and it will default to the same as ntype. Next, users need to implement the vertex-centered init and compute methods in UserPregelAlgorithm. The init method is executed only once at the beginning of the algorithm, while the compute method is iterated multiple times. If these methods are not implemented, they default to empty. Additionally, the combiner parameter combines messages sent to the same target vertex to reduce communication overhead. By default, it is set to None, which means messages are not combined.

Pregel programming model:

from hyg.analytics.model import pregel_types, PregelModel
@pregel_types(ntype=None, mtype=None, combiner=None)
class PregelModel:
    @staticmethod
    def init(ctx, nid):
        pass
 
    @staticmethod
    def compute(ctx, nid, msgs):
        pass

The result_filter parameter is of type function and supports lambda functions. It takes (ctx, nid) as input and returns a boolean value, which is used to filter the Pregel calculation results. The debug_mode is of type bool. When set to True, the UDF is not compiled into native code but is interpreted by the Python interpreter. In this case, the number of concurrent threads in the HyG framework process is forced to 1, and users can use print statements to print debugging information in the UDF. When debug_mode is set to False, using print statements in the UDF will result in an error.

In addition, the BaseGraph also has an interface BaseGraph.nid(ext_id:str) -> int, which is used to obtain the internal ID of a vertex.