Sample Javascript Service
The following sample represents a javascript service that creates an AnalyticsDatasetRef infotable and uses it to create a dataset with data added in the body of the request. The service includes four sections:
1. The first section creates the data infotable. It references a DataShape, which must already exist as an entity in ThingWorx, and lists a rows of data – one for each record.
2. This section creates the metadata infotable that defines the columns in the dataset.
3. This section creates the AnalyticsDatasetRef table itself. It references both the data and metadata infotables created in sections 1 and 2. In this case, it specifies csv for the format and body:/ for datasetURI.
4. The final section shows how to use the AnalyticsDatasetRef in a request.
//----------------------------------------------------------
// 1. Create the "data" field InfoTable containing the dataset
//----------------------------------------------------------
var dataInfoTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName : "InfoTable",
dataShapeName : "MyDataShape"
});
dataInfoTable.AddRow({ "col1": 11, "col2": 12, "col3": 13 });
dataInfoTable.AddRow({ "col1": 21, "col2": 22, "col3": 23 });
dataInfoTable.AddRow({ "col1": 31, "col2": 32, "col3": 33 });
//-------------------------------------------------------------------------
// 2. Create the "metadata" field InfoTable containing the metadata for the dataset
//-------------------------------------------------------------------------
var metadataInfoTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName : "InfoTable",
dataShapeName : "AnalyticsDatasetMetadata"
});
metadataInfoTable.AddRow({ "fieldName": "col1", "dataType": "INTEGER", "opType": "CONTINUOUS" });
metadataInfoTable.AddRow({ "fieldName": "col2", "dataType": "INTEGER", "opType": "CONTINUOUS" });
metadataInfoTable.AddRow({ "fieldName": "col3", "dataType": "INTEGER", "opType": "CONTINUOUS" });
//---------------------------------------------------------------------------
// 3. Create the AnalyticsDatasetRef that will be used for various services
//---------------------------------------------------------------------------
var analyticsDatasetRef = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName : "InfoTable",
dataShapeName : "AnalyticsDatasetRef"
});
analyticsDatasetRef.AddRow({
data: dataInfoTable,
datasetUri: "body:/",
format: "csv",
metadata: metadataInfoTable
});
//-------------------------------------------
// 4. Do something with the AnalyticsDatasetRef
//-------------------------------------------
var jobId = Things["AnalyticsServer_DataThing"].CreateDatasetWithDatasetRef({
datasetRef: analyticsDatasetRef,
tags: undefined
});