Professional Documents
Culture Documents
The Context SQL Sync REST service allows importing (either one-time or by scheduling a
continuous job) of context data from an SQL database (PostgreSQL, MySQL, or MS SQL) to
TrendMiner.
Authentication
Currently, only OAuth2 authentication is supported.
Method: POST
Path: /security/oauth/token
Authorization header: Basic
dHJlbmRtaW5lckFwaTpjdmJwa3R6SmVGUENLVk10dFN0ZVQ1ejNlYXlkeUZaYQ==
Body
grant_type: password
username: [TrendMiner username]
password: [TrendMiner password]
Define a datasource
New data sources can be defined by the API:
Method: POST
Path: /context-sql-sync/datasource
Authorization header: Bearer [access_token]
Body
{
"name": "localdb",
TrendMiner N.V. | VAT BE 0894.414.630 | RPR Hasselt, Belgium | IBAN: BE82 737022894568 | BIC: KREDBEBB | T +3211263830
info@trendminer.com | www.trendminer.com
"jdbcUrl":
"jdbc:postgresql://localhost:5432/tm_context_sql_sync",
"dbType": "POSTGRES",
"username": "postgres",
"password": "postgres"
}
The result will contain the id of the newly created datasource. This id will be used later when
executing a historical sync or when defining sync jobs.
Define a query
Method: POST
Path: /context-sql-sync/query
Authorization header: Bearer [access_token]
Body
{
"name": "queryAlias",
"supportsParameters": true,
"supportsNamedParameters": true,
"componentLookupStrategy": "ASSETS|TAGS|ASSETS_THEN_TAGS",
"fieldNames": "customField1, customField2",
"sqlString": "Select id, componentId, type, description,
start_event, start_time, stop_event, stop_time, customField1,
customField2 from LOGBOOK where :startDate <= start_time and
start_time < :endDate;"
"typeMapping": [
{ "PRODUCTION": "MAPPED_TYPE" },
{ "SOURCE_TYPE": "TM_TYPE" }
]
}
If the datasource contains a lastModifiedDate column, the query can be modified to select
records based on their lastModifiedDate. For example:
{
...
"sqlString": "Select id, componentId, type, description,
TrendMiner N.V. | VAT BE 0894.414.630 | RPR Hasselt, Belgium | IBAN: BE82 737022894568 | BIC: KREDBEBB | T +3211263830
info@trendminer.com | www.trendminer.com
start_event, start_time, stop_event, stop_time, last_modified_date
from LOGBOOK where :startDate <= last_modified_date and
last_modified_date < :endDate;"
...
}
This way, if an item is modified, it will be selected for sync, even if its start time is
untouched.
To get an overview of your synced asset structure and the external IDs of the different
assets you can use the "/assets/browse" endpoint of the TrendMiner Assets API. The
external ID of the assets is identified by the “externalId” field.
● The type will be looked up in typeMapping (see below) and the associated value will
be used as the type of the context item
● The description will represent the description of the context item
TrendMiner N.V. | VAT BE 0894.414.630 | RPR Hasselt, Belgium | IBAN: BE82 737022894568 | BIC: KREDBEBB | T +3211263830
info@trendminer.com | www.trendminer.com
● The start_event and start_time will be used for the start event of the context item
● The end_event and end_time will be used for the end event of the context item
● If the query supports positional parameters and is run for a specific time interval, the
start and end of the interval will be passed to the query to be used in the selection
criteria
● If the query supports named parameters and is run for a specific time interval, the
start and end of the interval will be passed to the query through the :startDate and
:endDate parameters respectively, so they need to be utilized in the selection criteria
typeMapping - a table used to map the values in the type column to actual context types in
TrendMiner
The result will contain the id of the newly created query. This id will be used later.
Historical Sync
Method: POST
Path: /context-sql-sync/sync
Authorization header: Bearer [access_token]
Body
{
"datasourceId": "1",
"queryId": "2",
"externalIdPrefix": "prefix",
"startDate": "2018-10-17 12:29:01",
"endDate": "2018-10-18 20:29:01",
"chunkSize": "2"
}
The startDate and endDate can be omitted. In this case the associated query will be run only
once without parameters.
If startDate and endDate are specified, the resulting interval is split to small subintervals
based on chunkSize. Then the SQL query is executed continuously for each of the
subintervals and the start and end of the subintervals are passed to the query as
parameters.
TrendMiner N.V. | VAT BE 0894.414.630 | RPR Hasselt, Belgium | IBAN: BE82 737022894568 | BIC: KREDBEBB | T +3211263830
info@trendminer.com | www.trendminer.com
The historical sync provides means to perform a “dry” run, where the data is fetched from the
database and returned to the customer, but no actual context items are created. To use the
dry run:
Path: /context-sql-sync/sync/test
Live Sync
To use live sync, a sync job must be defined first:
Method: POST
Path: /context-sql-sync/syncjob
Authorization header: Bearer [access_token]
Body
{
"datasourceId": "1",
"queryId": "2",
"externalIdPrefix": "prefix",
"syncJobInterval": "10",
"chunkSize": "2"
}
The result will contain the id of the newly created sync job. This id can be used to start and
stop the job.
Method: POST
Path: /context-sql-sync/syncjob/{id}/start
Authorization header: Bearer [access_token]
To stop a job:
Method: POST
Path: /context-sql-sync/syncjob/{id}/stop
Authorization header: Bearer [access_token]
TrendMiner N.V. | VAT BE 0894.414.630 | RPR Hasselt, Belgium | IBAN: BE82 737022894568 | BIC: KREDBEBB | T +3211263830
info@trendminer.com | www.trendminer.com
To get a list of all currently defined sync jobs:
Method: GET
Path: /context-sql-sync/syncjob
Authorization header: Bearer [access_token]
Important notes
● The access token will expire after 12 hours. Best practice is to request an access
token before every API call.
TrendMiner N.V. | VAT BE 0894.414.630 | RPR Hasselt, Belgium | IBAN: BE82 737022894568 | BIC: KREDBEBB | T +3211263830
info@trendminer.com | www.trendminer.com