Skip to main content
1-Visitor
August 16, 2023
Solved

Transferring data from a Stream into another

  • August 16, 2023
  • 1 reply
  • 2122 views

Hi! I'm trying to get all the entries from one Stream Thing and transfer them to another. 

My code looks like this: 

 

new_entries = Things["TemporalStream"].GetStreamEntriesWithData();
var params = {
values:new_entries
};
Things["MainStream"].AddStreamEntries(params);

 

 

I'm getting the error message :: Wrapped java.lang.NullPointerException - See Script Error Log for more details.

Could someone please tell me what I'm doing wrong?

 

 

 

Best answer by Arun_C

Hi @SE_10190539 ,

 

Try to use "Create infotable entry from datashape" snippet by your DataShape Selection it will add code with all field column names are present from your source datashape. 

 

Arun_C_0-1692188751280.png

 

Thanks & Regards,

Arun C

 

 

1 reply

16-Pearl
August 16, 2023

HI @SE_10190539 ,

 

Kindly refer the below articles for how to use 'AddStreamEntries ' service in our custom services in thing.

Thanks & Regards,

Arun C

1-Visitor
August 16, 2023

Hi, I'm reading the articles, but I can't find anything that solves my problem. I just want to take the entries from the stream Temporal and copy them into the Main one. 

16-Pearl
August 16, 2023

Hi @SE_10190539,

 

These below given code updates based on the article will helps you to copy data from 1 stream to another another.

 

 

 

//// *** SET UP META DATA FOR INFO TABLE *** ////
let myInfoTable = { dataShape: { fieldDefinitions : {} }, rows: [] };
myInfoTable.dataShape.fieldDefinitions['timestamp'] = { name: 'timestamp', baseType: 'DATETIME' };
myInfoTable.dataShape.fieldDefinitions['location'] = { name: 'location', baseType: 'LOCATION' };
myInfoTable.dataShape.fieldDefinitions['source'] = { name: 'source', baseType: 'STRING' };
myInfoTable.dataShape.fieldDefinitions['sourceType'] = { name: 'sourceType', baseType: 'STRING' };
myInfoTable.dataShape.fieldDefinitions['tags'] = { name: 'tags', baseType: 'TAGS' };
myInfoTable.dataShape.fieldDefinitions['values'] = { name: 'values', baseType: 'INFOTABLE' };


//// *** GET oldData from STREAM *** ////
let oldData = Things["Demo_Stream_ST"].GetStreamEntriesWithData({
	oldestFirst: undefined /* BOOLEAN */ ,
	maxItems: 500 /* NUMBER {"defaultValue":500} */
});

//// *** LOOP the data and add values to myInfoTable *** ////
for (let i = 0; i < oldData.length; i++) {
	// create new values based on Stream DataShape
	let params = {
		infoTableName: "InfoTable",
		dataShapeName: "Demo_Stream_DS"
	};
	let values = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
 
 // create new values based on Stream DataShape and add values from oldData
 // Demo_Stream_DS entry object
 let newValues = {
 Sno: oldData.rows[i].Sno, // NUMBER
 Name: oldData.rows[i].Name // STRING
 };
	values.AddRow(newValues);
 
 // create new values based on myInfoTable Datashape and add values to their fields
 let newEntry = new Object();
 newEntry.timestamp = oldData.rows[i].timestamp;
 newEntry.location = oldData.rows[i].location;
 newEntry.source = oldData.rows[i].source;
 newEntry.tags = oldData.rows[i].tags;
 newEntry.sourceType = oldData.rows[i].sourceType;
 newEntry.values = values;
 
 // add new Info Table row to Info Table
	myInfoTable.rows[i] = newEntry;
}

 //// *** ADD myInfoTable (HOLDING MULITPLE STREAM ENTRIES) TO STREAM *** ////
Things["Demo_Stream_Duplicate_ST"].AddStreamEntries({
	values: myInfoTable /* INFOTABLE {"dataShape":"StreamEntryWithValues"} */
});
result = "Data Migrated Successfully!";

 

 

 

Kindly take look on it.

 

Note: You need to carefull when the "AddStreamEntires" services execution in your environment.

 

Thanks & Regards,

Arun C