import os
import sys
import json
# For RenderJSON
import uuid
from IPython.display import display_javascript, display_html, display
A class for pretty-printing JSON. Not required by Senzing, but helps visualize JSON.
class RenderJSON(object):
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)
Update system path.
python_path = "{0}/python".format(
os.environ.get("SENZING_G2_DIR", "/opt/senzing/g2"))
sys.path.append(python_path)
Create variables used for G2Config and G2ConfigMgr.
%run senzing-init-config.ipynb
%store -r senzing_config_json
RenderJSON(senzing_config_json)
The G2Config API is used to create a new JSON configuration from the default template. For more information, view the senzing-G2Config-reference.ipynb notebook.
import G2Exception
from G2Config import G2Config
g2_config = G2Config()
try:
g2_config.initV2(module_name, senzing_config_json, verbose_logging)
# Create configuration from template file.
config_handle = g2_config.create()
# Save JSON to string variable.
response_bytearray = bytearray()
g2_config.save(config_handle, response_bytearray)
senzing_model_config_json = response_bytearray.decode()
except G2Exception.G2ModuleGenericException as err:
print(g2_config.getLastException())
RenderJSON(response_bytearray)
The G2ConfigMgr API is used to add specific JSON configurations to the database, so that they may be shared across remote systems. Such configurations are added to the database, and a configuration ID is created for each of them. The system may then be configured with a specific configuratin ID that points to one of those configurations. That configuration ID will then be the shared global config.
from G2ConfigMgr import G2ConfigMgr
To start using G2ConfigMgr, you must first create and initialize an instance of the config manager. This should be done once per process.
Create a new instance of the config manager and assign it to a variable. Then, call the appropriate initialization method (such as initV2) to initialize the config manager.
During the call, the initialization methods accept the following parameters:
Calling these functions will return "0" upon success - useful for error handling.
g2_configuration_manager = G2ConfigMgr()
try:
g2_configuration_manager.initV2(
module_name,
senzing_config_json,
verbose_logging)
except G2Exception.G2ModuleGenericException as err:
print(g2_configuration_manager.getLastException())
Use addConfig()
to add a configuration JSON document to the data repository.
The addConfig()
function accepts the following parameters as input:
The function returns "0" upon success.
config_comment = "Configuration added from G2SetupConfig."
config_id_bytearray = bytearray()
try:
g2_configuration_manager.addConfig(
senzing_model_config_json,
config_comment,
config_id_bytearray)
except G2Exception.G2ModuleGenericException as err:
print(g2_configuration_manager.getLastException())
print("Config ID: {0}".format(config_id_bytearray.decode()))
Use getConfigList()
to retrieve a list of the configuration JSON documents contained in the data repository.
The function returns "0" upon success.
response_bytearray = bytearray()
try:
g2_configuration_manager.getConfigList(response_bytearray)
except G2Exception.G2ModuleGenericException as err:
print(g2_configuration_manager.getLastException())
RenderJSON(response_bytearray)
Use getConfig()
to retrieve a specific configuration JSON document from the data repository.
The getConfig()
function accepts the following parameters as input:
They also have various arguments used to return response documents.
The function returns "0" upon success.
response_bytearray = bytearray()
try:
g2_configuration_manager.getConfig(
config_id_bytearray,
response_bytearray)
except G2Exception.G2ModuleGenericException as err:
print(g2_configuration_manager.getLastException())
RenderJSON(response_bytearray)
Use setDefaultConfigID()
to set the default configuration JSON document in the data repository.
The setDefaultConfigID()
function accepts the following parameters as input:
The function returns "0" upon success.
try:
g2_configuration_manager.setDefaultConfigID(config_id_bytearray)
except G2Exception.G2ModuleGenericException as err:
print(g2_configuration_manager.getLastException())
Use getDefaultConfigID()
to retrieve a specific configuration JSON document from the data repository.
The getDefaultConfigID()
function accepts the following parameters as input:
The function returns "0" upon success.
try:
g2_configuration_manager.getDefaultConfigID(config_id_bytearray)
except G2Exception.G2ModuleGenericException as err:
print(g2_configuration_manager.getLastException())
print("Configuration ID: {0}".format(config_id_bytearray.decode()))