G2Config

Version 2.x Version 3.x

Prepare 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)

Initialize variables

Create variables used for G2Config.

%run senzing-init-config.ipynb
Stored 'senzing_config_json' (str)
Default config already set
Stored 'config_id_bytearray' (bytearray)
%store -r senzing_config_json
RenderJSON(senzing_config_json)
python_path = "{0}/python".format(
    os.environ.get("SENZING_G2_DIR", "/opt/senzing/g2"))
sys.path.append(python_path)

G2Config

The G2Config API…

from senzing import G2Config, G2Exception

Initialize G2Config

Parameters
  • module_name: (str) A short name given to this instance of the G2Config object.
  • senzing_config_json: (str) A JSON string containing configuration parameters.
  • verbose_logging: (bool) A boolean which enables diagnostic logging.
g2_config = G2Config()
try:
    g2_config.init(module_name, senzing_config_json, verbose_logging)

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

Create configuration handle

try:
    config_handle = g2_config.create()

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

save

Parameters
  • config_handle: (int) the handle of the config object to get the config json data from
  • 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
try:
    response_bytearray = bytearray()
    g2_config.save(config_handle, response_bytearray)
    config_bytearray = response_bytearray
except G2Exception as err:
    print(g2_config.getLastException())

RenderJSON(response_bytearray)

load

Parameters
  • config_bytearray: (bytearray) the config data to load into the config handle.
try:
    # example using the saved config from the `save()` method
    g2_config.load(config_bytearray)

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

RenderJSON(config_bytearray)

Datasources

listDataSources

Call G2Config’s listDataSources() method and pretty-printsresults.

Parameters
  • config_handle: (int) the handle of the config object to get the config json data from
  • 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
try:
    response_bytearray = bytearray()

    g2_config.listDataSources(config_handle, response_bytearray)

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

RenderJSON(response_bytearray)

addDataSource

Call G2Config’s addDataSource() method and pretty-prints results.

Parameters
  • config_handle: (int) the handle of the config object to get the config json data from
  • datasource_json: (str) json containing the name of the datasource to be added
  • 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
try:
    datasource = {
        "DSRC_CODE": "CUSTOMER"
    }
    datasource_json = json.dumps(datasource)
    response_bytearray = bytearray()

    g2_config.addDataSource(config_handle, datasource_json, response_bytearray)

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

RenderJSON(response_bytearray)

listDataSources again

Call G2Config’s listDataSources() method and pretty-prints results. Notice that the list now contains the newly added datasource_code of “CUSTOMER”.

Parameters
  • config_handle: (int) the handle of the config object to get the config json data from
  • 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
try:
    response_bytearray = bytearray()

    g2_config.listDataSources(config_handle, response_bytearray)

except G2Exception as err:
    print(g2_config.getLastException())
RenderJSON(response_bytearray)

deleteDataSource

Call G2Config’s deleteDataSource() method and pretty-prints results.

Parameters
  • config_handle: (int) the handle of the config object to get the config json data from
  • datasource_json: (str) json containing the name of the datasource to be added
try:
    datasource = {
        "DSRC_CODE": "CUSTOMER"
    }
    datasource_json = json.dumps(datasource)
    response_bytearray = bytearray()

    g2_config.deleteDataSource(config_handle, datasource_json)

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

Cleanup

getLastException

print(g2_config.getLastException())

clearLastException

try:
    g2_config.clearLastException()

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

close

  • config_handle: (int) the handle of the config object to get the config json data from
try:
    g2_config.close(config_handle)

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

destroy

try:
    g2_config.destroy()

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