G2Config Reference

Prepare Environment

In [1]:
import com.senzing.g2.engine.G2Config;
import com.senzing.g2.engine.G2ConfigJNI;
import com.senzing.g2.engine.Result;

Helper class for Json Rendering

In [2]:
%%loadFromPOM
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1.4</version>
</dependency>
In [3]:
import javax.json.*;
import static java.util.Collections.*;
import static javax.json.stream.JsonGenerator.PRETTY_PRINTING;
In [4]:
public class JsonUtil {
    private static final JsonWriterFactory PRETTY_FACTORY
        = Json.createWriterFactory(singletonMap(PRETTY_PRINTING, true));
        
    private static final JsonWriterFactory UGLY_FACTORY
        = Json.createWriterFactory(emptyMap());
    
    public static String toJsonText(JsonValue val) {
        return toJsonText(val, true);
    }

    public static String toJsonText(JsonValue val, boolean prettyPrint) {
        JsonWriterFactory factory = (prettyPrint) ? PRETTY_FACTORY : UGLY_FACTORY;
        StringWriter sw = new StringWriter();
        JsonWriter writer = factory.createWriter(sw);
        writer.write(val);
        sw.flush();
        return sw.toString();
    }
    
    public static JsonObject parseJsonObject(String jsonText) {
        if (jsonText == null) return null;
        StringReader sr = new StringReader(jsonText);
        JsonReader jsonReader = Json.createReader(sr);
        return jsonReader.readObject();
  }

    public static JsonArray parseJsonArray(String jsonText) {
        if (jsonText == null) return null;
        StringReader sr = new StringReader(jsonText);
        JsonReader jsonReader = Json.createReader(sr);
        return jsonReader.readArray();
  }
    
    
}
In [5]:
import java.util.UUID;
String str;
public static void RenderJSON(Object obj){
    str = obj.toString();
    JsonObject json = JsonUtil.parseJsonObject(str);
    String Config = JsonUtil.toJsonText(json, false);
    UUID id = UUID.randomUUID();
    String uuid = id.toString();
    String div = "<div id=\""+ uuid +"\" style=\"height:100%; width:100%; background-color: LightCyan\"></div>";
    display(div, "text/html");
    String jav = "require([\"https://rawgit.com/caldwell/renderjson/master/renderjson.js\"], function() {document.getElementById(\'"+ uuid +"\').appendChild(renderjson("+json+"))});";
    display(jav, "application/javascript");
}

Initialize Senzing configuration

Using environment variables and default values, create senzingConfigJson. This value is used when instantiating Senzing objects.

In [6]:
// Get variables used in constructing Senzing Engine configuration.

String configPath = System.getenv("SENZING_ETC_DIR");
if (configPath == null) {
    configPath = "/etc/opt/senzing";
}

String supportPath = System.getenv("SENZING_DATA_VERSION_DIR");
if (supportPath == null) {
    supportPath = "/opt/senzing/data";
}

String g2Path = System.getenv("SENZING_G2_DIR");
if (g2Path == null) {
    g2Path = "/opt/senzing/g2";
}

String resourcePath = g2Path + "/resources";

String sqlConnection = System.getenv("SENZING_SQL_CONNECTION");
if (sqlConnection == null) {
    sqlConnection = "sqlite3://na:na@/var/opt/senzing/sqlite/G2C.db";
}

// Construct the JSON string used for Senzing Engine configuration.

String senzingConfigJson = "{"
   + "\"PIPELINE\": {"
   +     "\"CONFIGPATH\": \"" + configPath + "\","
   +     "\"SUPPORTPATH\": \"" + supportPath + "\","
   +     "\"RESOURCEPATH\": \"" + resourcePath + "\""
   + "},"
   + "\"SQL\": {"
   +     "\"CONNECTION\": \"" + sqlConnection + "\""
   + "}}";
   
RenderJSON(senzingConfigJson);

G2Config

The G2Config API is used to create a new JSON configuration from the default template or manipulate a JSON configuration that it is provided. The API read/writes a JSON string, but that string would typically be read/written from/to a g2config.json file that the G2 engine reads during the initV2(...) call.

Initialize G2Config

Parameters
  • moduleName: (str) A short name given to this instance of the G2Config object.
  • senzingConfigJson: (str) A JSON string containing configuration parameters.
  • verboseLogging: (bool) A boolean which enables diagnostic logging.
In [7]:
String moduleName = "ExampleG2Config";
boolean verboseLogging = true;

G2Config g2config = new G2ConfigJNI();
int return_code = g2config.init(moduleName, senzingConfigJson, verboseLogging);

if(return_code!=0)
    System.out.print(g2config.getLastException());
else
    System.out.print(return_code);
0

Create Configuration handle

In [8]:
Result<Long> config = new Result<Long>();
long configHandle;
int return_code = g2config.create(config);

if(return_code!=0)
    System.out.print(g2config.getLastException());
else{
    configHandle = config.getValue();
    System.out.print(config.getValue());
}
140687494701376

save

Parameters
  • configHandle: (int) variable containg the the number of the used config
  • response: (StringBuffer) Object to store the output of the method. Can be used to print or store for later.
In [9]:
StringBuffer response = new StringBuffer();

int return_code = g2config.save(configHandle, response);

if(return_code!=0)
    System.out.print(g2config.getLastException());
else
    RenderJSON(response);

load

Parameters
  • response: (StringBuffer) Object to store the output of the method. Can be used to print or store for later.
  • config: (long) the handle of the config object to get the config json data from
In [10]:
// example using the saved config from the `save()` method
g2config.load(response.toString(), config);
Out[10]:
0

Datasources

listDataSources

Call G2Config's listDataSources() method and pretty-print results.

Parameters
  • configHandle: (long) the handle of the config object to get the config json data from
  • response: (StringBuffer) Object to store the output of the method. Can be used to print or store for later.
In [11]:
StringBuffer response = new StringBuffer();

int return_code = g2config.listDataSources(configHandle, response);

if(return_code!=0)
    System.out.print(g2config.getLastException());
else
    RenderJSON(response);

addDataSource

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

Parameters
  • configHandle: (long) the handle of the config object to get the config json data from
  • dataSource: (str) json containing the name of the datasource to be added
  • response: (StringBuffer) Object to store the output of the method. Can be used to print or store for later.
In [12]:
String dataSource = "{\"DSRC_CODE\": \"CUSTOMER\"}";

StringBuffer response = new StringBuffer();

int return_code = g2config.addDataSource(configHandle, dataSource, response);

if(return_code!=0)
    System.out.print(g2config.getLastException());
else
    RenderJSON(response);

listDataSources again

Call G2Config's listDataSources() method and pretty-print results. Notice that the list now contains the newly added datasource_code of "CUSTOMER".

Parameters

  • configHandle: (long) the handle of the config object to get the config json data from
  • response: (StringBuffer) Object to store the output of the method. Can be used to print or store for later.
In [13]:
StringBuffer response = new StringBuffer();

int return_code = g2config.listDataSources(configHandle, response);

if(return_code!=0)
    System.out.print(g2config.getLastException());
else
    RenderJSON(response);

deleteDataSource

Call G2Config's deleteDataSource() method and pretty-print results.

Parameters
  • configHandle: (long) the handle of the config object to get the config json data from
  • dataSource: (str) json containing the name of the datasource to be added
In [14]:
String dataSource = "{\"DSRC_CODE\": \"COMPANY\"}";

int return_code = g2config.deleteDataSource(configHandle, dataSource);

if(return_code!=0)
    System.out.print(g2config.getLastException());

Cleanup

getLastException

In [15]:
System.out.print(g2config.getLastException());
null

clearLastException

In [16]:
g2config.clearLastException();

close

  • configHandle: (long) the handle of the config object to get the config json data from
In [17]:
g2config.close(configHandle);
Out[17]:
0

destroy

In [18]:
int return_code = g2config.destroy();

if(return_code!=0)
    System.out.print(g2config.getLastException());