我有一个简单的 Azure 函数,它从表存储中获取以前的数据,使用它来生成新行,然后将其记录回同一个表中。 这些行有许多不同的字段,其中一些是整数,一些是浮点。但正如我所看到的,它们都作为 int64 数字存储到表中。当然所有的分数都会丢失。其次,当我将此数据输入到我的 Web 应用程序中时,它会产生问题,因为它不适用于 64 位整数。 简单来说一下 Azure 函数内部发生了什么
export async function eventHubTrigger1(message: Message, context: InvocationContext): Promise<void> {
context.log('Event hub function processed message:', message);
const prevData: ProcessedRecord[] = context.extraInputs.get(tableInput) as ProcessedRecord[];
const currData: DeviceData[] = context.extraInputs.get(blobInput) as DeviceData[];
const row: ProcessedRecord = StatusEvaluatingFunction(prevData, message, trapData, context);
context.extraOutputs.set(tableOutput, [row]);
}
这就是这个“行”的样子
type ProcessedRecord = {
PartitionKey: string;
RowKey: string;
Leak: number; // integer
CycleCounts: number; // integer
Status: number; // integer
TotLossesKg: number; // double
...
};
有没有办法明确说明通过 Azure 函数(特别是用 JS/TS 编写)存储到表中的记录中的值的类型(如here)?
谷歌搜索没有帮助。也许,我找到的最有用的建议就是将所有数据作为字符串保留在表中,并在必要时将它们转换为数字。但我希望有更好的方法来解决这个问题。
使用下面的代码,我能够使用事件中心trigger更新Azure存储表中的数据。
const { app } = require('@azure/functions');
const { TableClient, AzureNamedKeyCredential } = require("@azure/data-tables");
// Define the ProcessedRecord type
const ProcessedRecord = {
PartitionKey: 'string',
RowKey: 'string',
Leak: 'number',
CycleCounts: 'number',
Status: 'number',
TotLossesKg: 'number',
// Add other properties as needed
};
app.eventHub('eventHubTrigger1', {
connection: 'sampathRootManageSharedAccessKey_EVENTHUB',
eventHubName: 'sampath',
cardinality: 'many',
handler: async (messages, context) => {
const account = Azure Storage Account Name";
const accountKey = "Azure Storage Account AccountKey ";
const tableName = "Azure Storage Account TableName ";
const credential = new AzureNamedKeyCredential(account, accountKey);
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);
if (Array.isArray(messages)) {
context.log(`Event hub function processed ${messages.length} messages`);
for (const message of messages) {
context.log('Event hub message:', message);
// Assuming your message contains data to be inserted into the table
const entity = {
PartitionKey: 'p2',
RowKey: 'r2',
Leak: 1,
CycleCounts: 2,
Status: 4,
TotLossesKg: 2.5,
// Add other properties as needed
};
try {
// Insert the entity into the table
await client.createEntity(entity);
context.log('Entity inserted into the table:', entity);
} catch (error) {
context.log('Error inserting entity into the table:', error);
}
}
} else {
context.log('Event hub function processed message:', messages);
}
}
});
输出:
Azure 存储表: