【问题标题】:Azure Table Storage only supports the String as a Data Type?Azure 表存储只支持字符串作为数据类型?
【发布时间】:2021-04-08 16:34:47
【问题描述】:

我正在尝试将数据插入 Azure 表,但所有内容都转换为字符串。

例如我正在插入数字/布尔值

var test={ PartitionKey : '4', RowKey : '2', foo: 4, bar: true };
tableService.insertEntity('mytable', test, ...);

但是

tableService.queryEntity('mytable', '4', '2', ...);

返回

{ id: 'http://127.0.0.1:10002/devstoreaccount1/identid(PartitionKey=\'4\',RowKey=\'2\')',
  link: 'identid(PartitionKey=\'4\',RowKey=\'2\')',
  updated: '2012-12-12T10:26:44Z',
  etag: 'W/"datetime\'2012-12-12T10%3A26%3A44.547Z\'"',
  PartitionKey: '4',
  RowKey: '2',
  Timestamp: '2012-12-12T10:20:44.897Z',
  foo: '4',
  bar: 'true' }

如何指定数据类型?


好的,刚刚在SDK中看到可以指定数据类型

var test={ PartitionKey : '4', RowKey : '2', 
  foo: { '@': { type: 'Edm.Int32' }, '#': 4 } };

但是有任何辅助函数可以自动添加类型吗?

【问题讨论】:

  • A hack:我发现如果您使用正确类型的虚拟对象(使用 azurestorageexplorer.codeplex.com 或类似对象)为表播种,则后续插入将被正确键入(以及后续插入类型错误会抛出错误)。
  • 有趣。我不知道表存储支持模式之类的东西。
  • 您可以向实体添加更多字段而不会导致错误(并忽略现有的),但如果您已经有一个名为“test”的字段,其中包含一个布尔值,您将获得一个invalid input 如果您尝试将字符串(甚至是“真”)放入该字段,则会出错。因此,创建一个虚拟对象似乎创建了一种隐式模式。

标签: node.js azure azure-table-storage


【解决方案1】:

由于 SDK 似乎没有包含任何有用的东西,我现在写了这些:

function azType(type, v) { return { "@": { type: type }, "#": v }; }
function azBool(v) { return azType("Edm.Boolean", v); }
function azBinary(v) { return azType("Edm.Binary", v); }
function azByte(v) { return azType("Edm.Byte", v); }
function azDateTime(v) { return azType("Edm.DateTime", v); }
function azDateTimeOffset(v) { return azType("Edm.DateTimeOffset", v); }
function azDecimal(v) { return azType("Edm.Decimal", v); }
function azDouble(v) { return azType("Edm.Double", v); }
function azGuid(v) { return azType("Edm.Guid", v); }
function azInt64(v) { return azType("Edm.Int64", v); }
function azInt32(v) { return azType("Edm.Int32", v); }
function azInt16(v) { return azType("Edm.Int16", v); }
function azSByte(v) { return azType("Edm.SByte", v); }
function azSingle(v) { return azType("Edm.Single", v); }
function azString(v) { return azType("Edm.String", v); }
function azTime(v) { return azType("Edm.Time", v); }

如

var test={ PartitionKey : '4', RowKey : '2', foo: azInt32(4) };

我不知道他们为什么要更改它,但是从 0.6.10 开始需要替换 azType 函数:

function azType(type, v) { return { "$": { type: type }, "_": v }; }

【讨论】:

    【解决方案2】:

    根据here,表服务数据模型只支持这8种:

    • Edm.Binary
    • Edm.Boolean
    • Edm.DateTime
    • Edm.Double
    • Edm.Guid
    • Edm.Int32
    • Edm.Int64
    • Edm.String

    注意对象中的 id 属性:

    { PartitionKey : '4', RowKey : '2', id: azInt32(4) };

    如果你尝试这样做,错误信息就不那么明显了:

    Error inserting : { [Error: [object Object]] code: 'PropertiesNeedValue', message:{ _: 'The values are not specified for all properties in the entity...','$': { 'xml:lang': 'en-US' } } }

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 2017-10-23
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 2020-06-04
      • 2011-11-07
      相关资源
      最近更新 更多