针对 Azure 表存储的空表查询

发布于 2024-10-10 14:07:15 字数 303 浏览 11 评论 0原文

我正在使用 Azure 表存储。当我查询一个空表,并且涉及除 PartitionKey 和 RowKey 以外的参数时,出现异常。当我至少有一行时,不会出现异常。如果我只用PartitionKey和RowKey查询空表,就可以了。

我当然不想进行额外的往返来测试表是否为空。人们通常如何解决这个问题?有没有一种高效的方法来快速检查表是否为空?

我正在使用开发存储,因为我刚刚看到在这种情况下开发存储报告了错误,并且该错误在生产中消失了。但是,我不想保留定制代码仅用于开发存储,有没有一个好的方法可以解决这个问题,以便我可以在本地以及生产云环境中运行相同的代码?

I'm using Azure Table Storage. When I query a table that is empty with parameters other than PartitionKey and RowKey involved, I get an exception. When I have at least one row, the exception doesn't appear. If I query the empty table with just PartitionKey and RowKey, it is OK.

I certainly do not want to make an extra round trip to test if the table is empty. How do people normally solve this problem? Is there a performant way to quickly check if the table is empty?

I am using the development storage, as I just saw there are reported errors in this scenario with the development storage and the error goes away in production. However, I do not want to keep customized code just for development storage, is there a good way to get around this, so I could have the same code running local as well as in production cloud environment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫以 2024-10-17 14:07:15

我通过设置 DataServiceContext 解决了这个问题.IgnoreResoureNotFoundException 属性设置为 true。希望这也对其他人有帮助。

I've got this around by setting DataServiceContext.IgnoreResoureNotFoundException property to true. Hope this helps others too.

十年九夏 2024-10-17 14:07:15

我无法让 IgnoreResourceNotFoundException 工作并把它踢下去。走了一条“顽皮”的路线,只是因为一张空桌子而陷入了异常。下面截图,欣赏一下...

CloudTableClient _tableClient = OurStorageAccount.CreateCloudTableClient();
CloudTable _table = _tableClient.GetTableReference( "customers" );

TableQuery<CustomerEntity> _query = new TableQuery<CustomerEntity>();
var _result = _table.ExecuteQuery( _query );

StringBuilder _sb = new StringBuilder(1024);
try
{ 
	_sb.AppendLine("Begin listing customers....<br/>");
	foreach ( CustomerEntity _customer in _result )
	{
		_sb.AppendFormat( "{0} {1} - {2} - {3}<br/>", _customer.PartitionKey, _customer.RowKey, _customer.Email, _customer.PhoneNumber );
	}
	_sb.AppendLine("End listing customers....<br/>");
}
catch ( System.NullReferenceException _nullEx )
{ 
	_sb.Append( System.DateTime.Now.ToString() );
	_sb.AppendLine( ": no customer entries found<br/>" );
	System.Diagnostics.Debug.WriteLine( _nullEx.ToString());
	// _sb.AppendLine( _nullEx.ToString() );
}
catch (Exception _ex)
{
	_sb.AppendLine("unkown exception thrown, good luck<br/>");
	_sb.AppendLine( _ex.ToString() );
}

Label_Output.Text = _sb.ToString();

I couldn't ever get the IgnoreResourceNotFoundException to work and punted it. Took a 'naughty' route and just exception trapped for an empty table. Snip below, enjoy...

CloudTableClient _tableClient = OurStorageAccount.CreateCloudTableClient();
CloudTable _table = _tableClient.GetTableReference( "customers" );

TableQuery<CustomerEntity> _query = new TableQuery<CustomerEntity>();
var _result = _table.ExecuteQuery( _query );

StringBuilder _sb = new StringBuilder(1024);
try
{ 
	_sb.AppendLine("Begin listing customers....<br/>");
	foreach ( CustomerEntity _customer in _result )
	{
		_sb.AppendFormat( "{0} {1} - {2} - {3}<br/>", _customer.PartitionKey, _customer.RowKey, _customer.Email, _customer.PhoneNumber );
	}
	_sb.AppendLine("End listing customers....<br/>");
}
catch ( System.NullReferenceException _nullEx )
{ 
	_sb.Append( System.DateTime.Now.ToString() );
	_sb.AppendLine( ": no customer entries found<br/>" );
	System.Diagnostics.Debug.WriteLine( _nullEx.ToString());
	// _sb.AppendLine( _nullEx.ToString() );
}
catch (Exception _ex)
{
	_sb.AppendLine("unkown exception thrown, good luck<br/>");
	_sb.AppendLine( _ex.ToString() );
}

Label_Output.Text = _sb.ToString();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文