SQL Server / Cognite API v1 / Create relationships
Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
-- Use this online tool to generate code from sample JSON: Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "items": [
-- {
-- "externalId": "dolor incididunt",
-- "sourceExternalId": "veniam",
-- "sourceType": "file",
-- "targetExternalId": "aliquip",
-- "targetType": "file",
-- "startTime": 44830797,
-- "endTime": 1210005,
-- "confidence": 0.5198580769389212,
-- "dataSetId": 1654747236161954,
-- "labels": [
-- {
-- "externalId": "irure commodo proident"
-- },
-- {
-- "externalId": "dolore nulla consequat"
-- }
-- ]
-- },
-- {
-- "externalId": "Lorem enim",
-- "sourceExternalId": "consequat Ut tempor dolore id",
-- "sourceType": "sequence",
-- "targetExternalId": "sed Ut incididunt adipisicing aliquip",
-- "targetType": "event",
-- "startTime": 5339993,
-- "endTime": 40374137,
-- "confidence": 0.1252789445409579,
-- "dataSetId": 4001858727825240,
-- "labels": [
-- {
-- "externalId": "sed officia"
-- },
-- {
-- "externalId": "sint est"
-- }
-- ]
-- }
-- ]
-- }
DECLARE @json int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].externalId', 'dolor incididunt'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].sourceExternalId', 'veniam'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].sourceType', 'file'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].targetExternalId', 'aliquip'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].targetType', 'file'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].startTime', 44830797
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].endTime', 1210005
EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'items[0].confidence', '0.5198580769389212'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].dataSetId', 123
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].labels[0].externalId', 'irure commodo proident'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].labels[1].externalId', 'dolore nulla consequat'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].externalId', 'Lorem enim'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].sourceExternalId', 'consequat Ut tempor dolore id'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].sourceType', 'sequence'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].targetExternalId', 'sed Ut incididunt adipisicing aliquip'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].targetType', 'event'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].startTime', 5339993
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].endTime', 40374137
EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'items[1].confidence', '0.1252789445409579'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].dataSetId', 123
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].labels[0].externalId', 'sed officia'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].labels[1].externalId', 'sint est'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'api-key', '{{api-key}}'
DECLARE @resp int
EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/api/v1/projects/{{project}}/relationships', 'application/json', @json
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
END
GO
Curl Command
curl -X POST
-H "api-key: {{api-key}}"
-H "content-type: application/json"
-d '{
"items": [
{
"externalId": "dolor incididunt",
"sourceExternalId": "veniam",
"sourceType": "file",
"targetExternalId": "aliquip",
"targetType": "file",
"startTime": 44830797,
"endTime": 1210005,
"confidence": 0.5198580769389212,
"dataSetId": 1654747236161954,
"labels": [
{
"externalId": "irure commodo proident"
},
{
"externalId": "dolore nulla consequat"
}
]
},
{
"externalId": "Lorem enim",
"sourceExternalId": "consequat Ut tempor dolore id",
"sourceType": "sequence",
"targetExternalId": "sed Ut incididunt adipisicing aliquip",
"targetType": "event",
"startTime": 5339993,
"endTime": 40374137,
"confidence": 0.1252789445409579,
"dataSetId": 4001858727825240,
"labels": [
{
"externalId": "sed officia"
},
{
"externalId": "sint est"
}
]
}
]
}'
https://domain.com/api/v1/projects/{{project}}/relationships
Postman Collection Item JSON
{
"id": "createRelationships",
"name": "Create relationships",
"request": {
"url": {
"host": "{{baseUrl}}",
"path": [
"api",
"v1",
"projects",
"{{project}}",
"relationships"
],
"query": [
],
"variable": [
]
},
"method": "POST",
"header": [
{
"key": "api-key",
"value": "{{api-key}}",
"description": "An admin can create API keys in the Cognite console."
},
{
"key": "content-type",
"value": "application/json"
}
],
"description": "List of the relationships to create. You can create a maximum of 1000 relationships per request. Relationships should be unique, but CDF does not prevent you from creating duplicates where only the externalId differs.\n\nRelationships are uniquely identified by their externalId. Non-unique relationships will not be created.\n\nThe order of relationships in the response equals the order in the request.",
"body": {
"mode": "raw",
"raw": "{\n \"items\": [\n {\n \"externalId\": \"dolor incididunt\",\n \"sourceExternalId\": \"veniam\",\n \"sourceType\": \"file\",\n \"targetExternalId\": \"aliquip\",\n \"targetType\": \"file\",\n \"startTime\": 44830797,\n \"endTime\": 1210005,\n \"confidence\": 0.5198580769389212,\n \"dataSetId\": 1654747236161954,\n \"labels\": [\n {\n \"externalId\": \"irure commodo proident\"\n },\n {\n \"externalId\": \"dolore nulla consequat\"\n }\n ]\n },\n {\n \"externalId\": \"Lorem enim\",\n \"sourceExternalId\": \"consequat Ut tempor dolore id\",\n \"sourceType\": \"sequence\",\n \"targetExternalId\": \"sed Ut incididunt adipisicing aliquip\",\n \"targetType\": \"event\",\n \"startTime\": 5339993,\n \"endTime\": 40374137,\n \"confidence\": 0.1252789445409579,\n \"dataSetId\": 4001858727825240,\n \"labels\": [\n {\n \"externalId\": \"sed officia\"\n },\n {\n \"externalId\": \"sint est\"\n }\n ]\n }\n ]\n}"
}
}
}