【问题标题】:"Syntax error at position 20 in Timestamp" Azure table storage query problems“时间戳中位置 20 的语法错误” Azure 表存储查询问题
【发布时间】:2018-06-18 11:49:48
【问题描述】:

我正在尝试从我们办公室的表格存储中提取一些数据。除了时间戳之外,我可以让所有类型的查询都能正常工作。他们不断给我一个 HTTP 错误 400 错误请求。检查它时,确切的错误文本是

"'Timestamp eq 中位置 20 的语法错误 2018-06-04T12:05:31.9150000Z'。 请求ID:3949e07b-b002-003f-59f8-065461000000 时间:2018-06-18T11:38:28.7307368Z".

我访问特定数据的代码是; var lowerlimit = DateTime.Now.AddDays(-20);

TableQuery<LogEntity> query = new TableQuery<LogEntity>().Where(TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThan, lowerlimit));

作为示例,此代码有效;

TableQuery<LogEntity> query = new TableQuery<LogEntity>().Where(TableQuery.GenerateFilterCondition("Level", QueryComparisons.Equal, "Warn"));

我找不到和我有同样问题的人,所以我希望你能帮助我!提前致谢!

*edit* here is the full code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure; // Namespace for CloudConfigurationManager
using Microsoft.Azure.Storage; // Namespace for StorageAccounts
using Microsoft.Azure.CosmosDB.Table; // Namespace for Table storage types
using Newtonsoft.Json;

namespace DataPullingViregoLog
{
 class Program
{
    static void Main(string[] args)
    {
        var lowerlimit = DateTimeOffset.Now.AddDays(-20);

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));

        CloudTableClient tableClient = 
        storageAccount.CreateCloudTableClient();
        // Create the CloudTable object that represents the "people" table.
        CloudTable table = tableClient.GetTableReference("{dbname}");


        TableQuery<LogEntity> query = new TableQuery<LogEntity> 
        ().Where(TableQuery.GenerateFilterConditionForDate("Timestamp", 
        QueryComparisons.GreaterThan, lowerlimit));




        IEnumerable<LogEntity> table2 = table.ExecuteQuery(query);

        var listOfEntities = new List<LogEntity>();
        foreach (LogEntity entity in table2)
        {
            listOfEntities.Add(entity);
            Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, 
            entity.RowKey,
            entity.Level, entity.MessageWithLayout);

        }

        var convertedJson = JsonConvert.SerializeObject(listOfEntities, 
        Formatting.Indented);
    }

}

internal class LogEntity : TableEntity
{

    public LogEntity(string PartitionKey, string Rowkey, DateTime Timestamp)
    {
        this.PartitionKey = PartitionKey;
        this.RowKey = RowKey;
        this.Timestamp = Timestamp;

    }

    public LogEntity() { }
    public string LogTimeStamp { get; set; }
    public string Level {get;set;}
    public string LoggerName { get; set; }
    public string Message { get; set; }
    public string MessageWithLayout { get; set; }
    public string MachineName { get; set; }
    public long LogTimeTicks { get; set; } 
    public string Exception { get; set; }
}

/拉斯穆斯

【问题讨论】:

  • 它是自定义列还是标准时间戳列?因为那个是DateTimeOffset,你试过把你的lowerlimit改成那个吗? This answer uses DateTimeOffset
  • 它抛出了同样的异常和同样的错误代码。我不知道它是否是标准,但是当比较字符串 1 到 1 时,它们看起来完全一样。
  • 我无法重现您在我身边提到的问题。我使用带有 Azure 存储帐户的最新 WindowsAzure.Storage 9.20 版本对其进行了测试。如果可能,请使用最新的 Azure 存储并重试。
  • 这很奇怪,因为根据我的理解,查询字符串应该是:Timestamp eq datetime'2018-06-04T12:05:31.9150000Z'
  • 我发送的日期是 2018-06-04T12:05:31.9150000Z 而数据库中的日期看起来像 2018-06-04T12:07:01.371Z 。如您所见,唯一的区别在于尾随零。这可能是问题吗?据我所知,问题是我的代码发出的请求。显然在 20 号空间有错误。@ZhaoxingLu-Microsoft 你是说 ' ' 应该在日期附近吗?

标签: c# .net azure azure-table-storage


【解决方案1】:

根据您的描述,我假设您使用的是Azure cosmos table (Microsoft.Azure.CosmosDB.Table) SDK 来操作 Azure 存储帐号。

如果您操作的是Azure存储表,请使用WindowsAzure.Storage, 它与您提到的图书馆不同。

我建议您可以创建一个安装了WindowsAzure.Storage 9.20 的新项目,然后它应该可以正常工作。

【讨论】:

  • 嗨,汤姆,感谢您的回答。根据我使用的来自 microsoft 的指南:Link 已经熟悉 Azure 表存储的开发人员过去可能使用过 WindowsAzure.Storage 包。建议所有新的表应用程序使用 Azure 存储公共库和 Azure Cosmos DB 表库,但仍支持 WindowsAzure.Storage 包。如果您使用 WindowsAzure.Storage 库,请在 using 语句中包含 Microsoft.WindowsAzure.Storage.Table。
  • @Rasraa Azure 存储公共库仍是预览版。据我所知,目前我们无法使用 CosomsDB 表库来操作 Azure 存储表。
  • 因为我现在使用的是正确的库 (tableStorage) 而不是一些代码(主要是 CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 不起作用,因为它不受该库的支持。您是否偶然知道如何解决此问题?
  • 根据我的理解,您正在尝试使用 Azure 存储连接字符串来创建 Azure Cosmos TableClient。所以你得到错误信息。如果仍想使用该库,您可以尝试创建一个 Azure Cosoms 表 API 来测试它。
  • 我只是按照微软发布的指南进行操作。他们声明这对这两种类型都有效。我认为你是正确的,但事实并非如此。鉴于我当前的代码和我想要完成的工作,我将修改什么以使其与表存储而不是当前代码一起使用。我无法更改有问题的数据库,因为这是为客户制作的。
【解决方案2】:

所以我发现需要另一个库,这在任何微软指南中都没有显示。这就是我的最终代码的样子。感谢你的协助!显然唯一需要的是using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Table; using Microsoft.Azure;

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 2011-07-20
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2019-03-05
    • 1970-01-01
    相关资源
    最近更新 更多