我想查询名为 message 的列以以下开头的行:metric="foo"
我尝试使用百分比和十六进制代码进行编码=
,"
但它不起作用。
微软文档说必须对特殊字符进行编码,但没有说明如何编码:https ://docs.microsoft.com/en-us/rest/api/storageservices/querying-tables-and-entities#query-string-encoding
当被比较的值包含特殊字符时,查询应该是什么样子?
我想查询名为 message 的列以以下开头的行:metric="foo"
我尝试使用百分比和十六进制代码进行编码=
,"
但它不起作用。
微软文档说必须对特殊字符进行编码,但没有说明如何编码:https ://docs.microsoft.com/en-us/rest/api/storageservices/querying-tables-and-entities#query-string-encoding
当被比较的值包含特殊字符时,查询应该是什么样子?
如果您使用的是 azure sdk,那么 sdk 已经为您解决了难题。
在我的测试中,我使用的是最新的 azure table storage sdk Microsoft.Azure.Cosmos.Table 1.0.4 版。
测试代码:
static void Main(string[] args)
{
string connstr = "xxxx";
var storageAccount = CloudStorageAccount.Parse(connstr);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("myCustomer123");
TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>();
string myfilter = TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "ivan"),
TableOperators.And,
//for metric="foo", like below.
TableQuery.GenerateFilterCondition("PhoneNumber", QueryComparisons.Equal, "metric=\"foo\"")
);
query.FilterString = myfilter;
var items = table.ExecuteQuery(query);
foreach (var item in items)
{
Console.WriteLine(item.RowKey);
Console.WriteLine(item.PhoneNumber);
}
Console.WriteLine("*****end******");
Console.ReadLine();
}
测试结果:
如果要使用参数过滤结果,可以使用?$filter=<your parameter>%20eq%20'<vaule>'
. 例如
var date = DateTime.Now.ToUniversalTime().AddYears(1).ToString("R");
var CanonicalizedResource = "/" + StorageAccountName + "/people";
var StringToSign = date + "\n" + CanonicalizedResource;
// List the containers in a storage account.
// ListContainersAsyncREST(StorageAccountName, StorageAccountKey, CancellationToken.None).GetAwaiter().GetResult();
var hmacsha = new HMACSHA256();
hmacsha.Key = Convert.FromBase64String(StorageAccountKey);
var sig= hmacsha.ComputeHash(UTF8Encoding.UTF8.GetBytes(StringToSign));
var sig1 = Convert.ToBase64String(sig);
Console.WriteLine(sig1);
String uri = "https://jimtestperfdiag516.table.core.windows.net/people" + "?$filter=PartitionKey%20eq%20'Jim'";
HttpClient client = new HttpClient();
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
httpRequestMessage.Headers.Add("x-ms-date", date);
var str = "SharedKeyLite " + StorageAccountName + ":" + sig1;
httpRequestMessage.Headers.TryAddWithoutValidation("Authorization", str);
httpRequestMessage.Headers.Add("x-ms-version", "2017-04-17");
httpRequestMessage.Headers.Add("Accept", "application/json;odata=fullmetadata");
var results = client.SendAsync(httpRequestMessage).Result;
var response = results.Content.ReadAsStringAsync().Result;
var objs = JsonConvert.DeserializeObject(response);
Console.WriteLine(objs);