Skip to main content
10-Marble
November 14, 2017
Solved

Thingworx Analytics 8.1 API Example

  • November 14, 2017
  • 7 replies
  • 4430 views

Hi,

I am trying to explore the Thingworx Analytics API - 8.1. I was trying to use the GetDatasetSchema service on the localhost-AnalyticsServer_DataThing as a REST API. Although there is API guide at ThingWorx Analytics API , an example will help me.

Any documents giving example usages for the TWX Analytics API 8.1?

Thanks and Regards,

Jyothi

Best answer by cmorfin

Hi Jyothi

You may want to have a look at this article: https://www.ptc.com/en/support/article?n=CS271485

There are some explanation and most importantly, as far as example as concerned, you have a json collection file (at the very bottom) that you can download and import into, let's say Postman, to check some examples.

Hope this helps.

Kind regards

Christophe

7 replies

cmorfin19-TanzaniteAnswer
19-Tanzanite
November 14, 2017

Hi Jyothi

You may want to have a look at this article: https://www.ptc.com/en/support/article?n=CS271485

There are some explanation and most importantly, as far as example as concerned, you have a json collection file (at the very bottom) that you can download and import into, let's say Postman, to check some examples.

Hope this helps.

Kind regards

Christophe

jrajesh10-MarbleAuthor
10-Marble
November 15, 2017

Hi Christophe,

Thanks you very much for getting back... that really helped!

I am particularly trying to use the API

http://127.0.0.1:8888/ThingWorx/Things/localhost-AnalyticsServer_DataThing/Services/GetDatasetSchema


The documentation at ThingWorx Analytics API mentions the details. I am giving the body as follows, But I am still getting a 404. Any ideas what might be going wrong?

{"dataSource": {

                "dataShape": {

                    "fieldDefinitions": {

                        "datasetUri": {

                            "name": "datasetUri",

                            "description": "Location of the data",

                            "baseType": "STRING",

                            "ordinal": 0,

                            "aspects": {}

                        },

                        "format": {

                            "name": "format",

                            "description": "Structure of the data (CSV,PARQUET,CLQUET)",

                            "baseType": "STRING",

                            "ordinal": 0,

                            "aspects": {}

                        },

                        "filter": {

                            "name": "filter",

                            "description": "SQL WHERE clause to refine the dataset",

                            "baseType": "STRING",

                            "ordinal": 0,

                            "aspects": {}

                        },

                        "exclusions": {

                            "name": "exclusions",

                            "description": "Fields to remove from the dataset",

                            "baseType": "INFOTABLE",

                            "ordinal": 0,

                            "aspects": {

                                "dataShape": "GenericStringList"

                            }

                        },

                        "data": {

                            "name": "data",

                            "description": "An infotable of data (must be flat. for providing data as part of request)",

                            "baseType": "INFOTABLE",

                            "ordinal": 0,

                            "aspects": {}

                        },

                        "metadata": {

                            "name": "metadata",

                            "description": "Metadata of the dataset (for providing data as part of request)",

                            "baseType": "INFOTABLE",

                            "ordinal": 0,

                            "aspects": {

                                "dataShape": "AnalyticsDatasetMetadata"

                            }

                        }

                    }

                },

                "rows": [

                    {

                        "datasetUri": "dataset:/1715c6d4-a513-44fa-be49-68fcd1075642",

                         "format": "parquet"

                    }

                ]

            }

}

Thanks and Regards,

Jyothi

19-Tanzanite
November 15, 2017

Hi Jyothi

I have used your code and it works for me.

Since you are gettign a 404 error this woudl indicate that the URI has some error.

Are you sure of the Thing name in the URI ?

Thanks

Christophe

jrajesh10-MarbleAuthor
10-Marble
November 15, 2017

Hi Christophe,

The Thing name is correct. I could execute the ListCreatedDatasets API with this thing. Is there a way to access the http access logs to understand what is the REST URI that reached the server? I can compare mine with the one executed via Thingworx Test Services. The same service executed via ThingWorx works fine.

Thanks and Regards,

Jyothi

jrajesh10-MarbleAuthor
10-Marble
November 23, 2017

Hi,

I am now trying to create a Javascript service in my own ThingTemplate which internally calls the GetDataSchema. This is how I am doing it.

var dataSet = Things['localhost-AnalyticsServer_DataThing'].ListCreatedDatasets();

var params = {

infoTableName : "InfoTable",

dataShapeName : "AnalyticsDatasetRef"

};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(AnalyticsDatasetRef)

var infoTable1 = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

// AnalyticsDatasetRef entry object

var newEntry = new Object();

newEntry.filter = dataSet.filter; // STRING

newEntry.datasetUri = dataSet.datasetUri; // STRING

newEntry.metadata = dataSet.metadata; // INFOTABLE

newEntry.data = dataSet.data; // INFOTABLE

newEntry.format = dataSet.format; // STRING

newEntry.exclusions = dataSet.exclusions; // INFOTABLE

infoTable1.AddRow(newEntry);

logger.debug(infoTable1);

result = Things['localhost-AnalyticsServer_DataThing'].GetDatasetSchema(infoTable1);

But I am getting the error:

Wrapped java.lang.Exception: Invalid parameter type for service : [GetDatasetSchema] on localhost-AnalyticsServer_DataThing Cause: Invalid parameter type for service : [GetDatasetSchema] on localhost-AnalyticsServer_DataThing

Any ideas?

Thanks and Regards,

Jyothi

jrajesh10-MarbleAuthor
10-Marble
November 23, 2017

Got it!

var params = {

dataSource: infoTable1 /* INFOTABLE */

};

// result: INFOTABLE dataShape: AnalyticsDatasetMetadata

result = Things["localhost-AnalyticsServer_DataThing"].GetDatasetSchema(params);