G2Engine

Version 2.x Version 3.x

Prepare environment

Initialize Senzing configuration

Use G2ConfigMgr to install a Senzing engine configuration in the database.

Initialize python environment

import os
import sys
import json

# For RenderJSON

import uuid
from IPython.display import display_javascript, display_html, display

Helper class for JSON rendering

A class for pretty-printing JSON. Not required by Senzing, but helps visualize JSON.

class RenderJSON():
    def __init__(self, json_data):
        if isinstance(json_data, dict):
            self.json_str = json.dumps(json_data)
        elif isinstance(json_data, bytearray):
            self.json_str = json_data.decode()
        else:
            self.json_str = json_data
        self.uuid = str(uuid.uuid4())

    def _ipython_display_(self):
        display_html('<div id="{}" style="height:100%; width:100%; background-color: LightCyan"></div>'.format(self.uuid), raw=True)
        display_javascript("""
        require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() {
        document.getElementById('%s').appendChild(renderjson(%s))
        });
        """ % (self.uuid, self.json_str), raw=True)

System path

Update system path.

python_path = "{0}/python".format(
    os.environ.get("SENZING_G2_DIR", "/opt/senzing/g2"))
sys.path.append(python_path)

Initialize variables

Create variables used for G2Engine.

%run senzing-init-config.ipynb
Stored 'senzing_config_json' (str)
Default config already set
Stored 'config_id_bytearray' (bytearray)
%store -r senzing_config_json
%store -r config_id_bytearray
RenderJSON(senzing_config_json)

Response message:

{
    "PIPELINE": {
        "CONFIGPATH": "/etc/opt/senzing",
        "SUPPORTPATH": "/opt/senzing/data",
        "RESOURCEPATH": "/opt/senzing/g2/resources"
    },
    "SQL": {
        "CONNECTION": "postgresql://postgres:postgres@senzing-postgres:5432:G2/"
    }
}

G2Engine

from senzing import G2Engine, G2Exception

G2Engine initialization

To start using Senzing G2Engine, create and initialize an instance. This should be done once per process.

Parameters
  • module_name: (str) A short name given to this instance of the G2Engine object.
  • senzing_config_json: A JSON string containing configuration parameters.
  • verbose_logging: A boolean which enables diagnostic logging.

Calling this function will return “0” upon success.

g2_engine = G2Engine()
try:
    return_code = g2_engine.init(
        module_name,
        senzing_config_json,
        verbose_logging)

except G2Exception as err:
    print(g2_engine.getLastException())

destroy()

destroy the engine so that we can initalize differently

g2_engine.destroy()

initWithConfigID

Alternatively initWithConfigID() can be used to specify a configuration.

Parameters
  • module_name: (str) A short name given to this instance of the G2Engine object.
  • senzing_config_json: (str) A JSON string containing configuration parameters.
  • verbose_logging: (bool) A boolean which enables diagnostic logging.
  • config_id_bytearray: (bytearray) The configID that you want to initialize the engine with.
try:
    return_code = g2_engine.initWithConfigID(
        module_name,
        senzing_config_json,
        config_id_bytearray,
        verbose_logging)

except G2Exception as err:
    print(g2_engine.getLastException())

reinit

The reinit() function may be used to reinitialize the engine using a specified initConfigID.

Parameters
  • config_id_bytearray: (bytearray) The configID that you want to initialize the engine with.
try:
    return_code = g2_engine.reinit(
        config_id_bytearray)

except G2Exception as err:
    print(g2_engine.getLastException())

primeEngine

The primeEngine() method may optionally be called to pre-initialize some of the heavier weight internal resources of the G2 engine.

try:
    g2_engine.primeEngine()

except G2Exception as err:
    print(g2_engine.getLastException())

getActiveConfigID

Call getActiveConfigID() to return an identifier for the loaded Senzing engine configuration. The call will assign a long integer to a user-designated variable

Parameters
  • configuration_id_bytearray: (bytearray) The identifier value for the engine configuration. The result of function call is returned here
configuration_id_bytearray = bytearray()
try:
    g2_engine.getActiveConfigID(configuration_id_bytearray)
    print("Configuration id: {0}".format(configuration_id_bytearray.decode()))

except G2Exception as err:
    print(g2_engine.getlastException())

Response message:

    Configuration id: 1890181000

exportConfig

Call exportConfig() to retrieve your Senzing engine’s configuration. The call will assign a JSON document to a user-designated buffer, containing all relevant configuration information – the function itself will return “0” upon success. The exportConfig function accepts the following parameters as input:

Parameters
  • response_bytearray: (bytearray) Object to store the output of the method. Can be used to print or store for later. Warning: This is not a string
  • config_id_bytearray: (bytearray) The identifier value for the engine configuration can be returned here.
response_bytearray = bytearray()
config_id_bytearray = bytearray()

try:
    g2_engine.exportConfig(response_bytearray, config_id_bytearray)
    print("Configuration ID: {0}".format(config_id_bytearray.decode()))

except G2Exception as err:
    print(g2_engine.getLastException())
RenderJSON(response_bytearray)
    Configuration ID: 1890181000
Click to expand exportConfig response message