Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X
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?
Solved! Go to Solution.
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.
Thanks & Regards,
Arun C
HI @SE_10190539 ,
Kindly refer the below articles for how to use 'AddStreamEntries ' service in our custom services in thing.
Thanks & Regards,
Arun C
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.
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
Thank you for your reply! I tried this and it is copying the entries into the Main stream. However, I am not getting the data that the entries contain. I guess I could do this by setting the fieldDefinitions with the additional data. Is there a way to copy the column names without having to hard code them?
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.
Thanks & Regards,
Arun C
Thank you!