DataHub
Transforming Values (ID Formatting Example)
Transforming Values in a DataHub Connector (Example: Formatting Asset ID)
Overview
sometime, source data must be transformed to meet system requirements. This example shows how to convert numeric IDs into a formatted Asset ID. It shows three types of transformations. Adding text to beginning, putting in a thousand's comma, left filling with zeros
Scenario
Input:
1
200
22456Required Output:
Client-000,001
Client-000,200
Client-022,456Implementation
Use a mutator to:
- Parse the number
- Pad with zeros
- Add prefix
- Insert thousands separator
Steps to write the mutator:
Example Mutator
return function mutate(sourceData, columnNames, rowObject, connectionData, createError) {
const [raw] = sourceData;
if (raw == null || raw === "") {
createError("AssetID is missing");
return null;
}
const id = parseInt(raw);
if (isNaN(id)) {
createError("Invalid AssetID: " + raw);
return null;
}
let str = id + "";
while (str.length < 6) {
str = "0" + str;
}
// Add comma separator
var formatted = str.slice(0, 3) + "," + str.slice(3);
return ["Client-" + formatted];
}Output Examples
Input
1 200 22456
Output
Client-000,001
Client-000,200
Client-022,456
Notes
- Always validate input before transformation
- Avoid modifying source files
- This example perform all formatting within DataHub, but you might want to make the changes before it gets to DataHub in other cases.