Azure 表存储中的数据转换

问题描述 投票:0回答:1

我有一个简单的 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
  ...
};

进入桌子后会变成什么enter image description here

有没有办法明确说明通过 Azure 函数(特别是用 JS/TS 编写)存储到表中的记录中的值的类型(如here)?

谷歌搜索没有帮助。也许,我找到的最有用的建议就是将所有数据作为字符串保留在表中,并在必要时将它们转换为数字。但我希望有更好的方法来解决这个问题。

azure-functions azure-table-storage
1个回答
0
投票

使用下面的代码,我能够使用事件中心trigger更新Azure存储表中的数据。

  • 以下代码用于传入消息的 Azure 事件中心,并将接收到的数据插入到 Azure 表存储中。

enter image description here

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);
        }
    }
});

输出:

enter image description here

enter image description here

Azure 存储表:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.