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():
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)
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)
The G2Config API…
from senzing import G2Config, G2Exception
g2_config = G2Config()
try:
g2_config.init(module_name, senzing_config_json, verbose_logging)
except G2Exception as err:
print(g2_config.getLastException())
try:
config_handle = g2_config.create()
except G2Exception as err:
print(g2_config.getLastException())
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)
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)
Call G2Config’s listDataSources()
method and pretty-printsresults.
try:
response_bytearray = bytearray()
g2_config.listDataSources(config_handle, response_bytearray)
except G2Exception as err:
print(g2_config.getLastException())
RenderJSON(response_bytearray)
Call G2Config’s addDataSource()
method and pretty-prints results.
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)
Call G2Config’s listDataSources()
method and pretty-prints results.
Notice that the list now contains the newly added datasource_code of “CUSTOMER”.
try:
response_bytearray = bytearray()
g2_config.listDataSources(config_handle, response_bytearray)
except G2Exception as err:
print(g2_config.getLastException())
RenderJSON(response_bytearray)
Call G2Config’s deleteDataSource()
method and pretty-prints results.
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())
print(g2_config.getLastException())
try:
g2_config.clearLastException()
except G2Exception as err:
print(g2_config.getLastException())
try:
g2_config.close(config_handle)
except G2Exception as err:
print(g2_config.getLastException())
try:
g2_config.destroy()
except G2Exception as err:
print(g2_config.getLastException())