简体   繁体   English

无法在Unity中查询或扫描DynamoDB

[英]Cannot query or scan DynamoDB in Unity

TLDR: Can't create an AsyncSearch object from QueryAsync or ScanAsync without running into a weird ArgumentNullException deep inside the AWS library. TLDR:如果不运行到AWS库深处的怪异ArgumentNullException,就无法通过QueryAsync或ScanAsync创建AsyncSearch对象。

I'm developing an online game in Unity and want to synchronize data with DynamoDB. 我正在Unity中开发一个在线游戏,想与DynamoDB同步数据。 When I only had a table with player data this worked very well. 当我只有一张包含玩家数据的表格时,这种方法效果很好。 I would just use "DBContext.LoadAsync(id, callback);" 我将只使用“ DBContext.LoadAsync(id,callback);” and it works flawlessly. 它可以完美地工作。 Saving with "SaveAsync" works just as well. 使用“ SaveAsync”进行保存同样有效。

Now I introduced some more tables and I need to perform scans and queries. 现在,我介绍了更多表,并且需要执行扫描和查询。 For testing purposes, I gave my Unity app all the rights it can possibly have (I used "dynamodb:*" and "Resource": ["*"] in IAM). 为了进行测试,我为Unity应用程序赋予了它可能拥有的所有权限(我在IAM中使用了“ dynamodb:*”和“ Resource”:[“ *”])。

I tried basically all the possible versions of ScanAsync, QueryAsync and FromQueryAsync with different parameters but I just always get an error. 我基本上尝试了使用不同参数的所有可能版本的ScanAsync,QueryAsync和FromQueryAsync,但是我总是会遇到错误。

Simple example: I want to download all departments (of my university): 一个简单的例子:我想下载我大学的所有系:

AsyncSearch<DepartmentData> search = AWSManager.Instance.DBContext.ScanAsync<DepartmentData>();

This call already fails, before I can do anything like "search.GetRemainingAsync(callback)". 在执行“ search.GetRemainingAsync(callback)”之类的操作之前,此调用已失败。 The same goes for for simple queries like: 对于简单的查询也是如此:

AsyncSearch<POIVisit> query = AWSManager.Instance.DBContext.QueryAsync<POIVisit>(poiType);

The error is the following and is somewhere deep in the AWS library, so I can't really go in there, set some breakpoints and see what exactly is failing: 错误如下,并且在AWS库的某个深处,所以我不能真正进入那里,设置一些断点,看看到底是什么失败了:

ArgumentNullException: Argument cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary`2[System.Type,Amazon.Runtime.IExceptionHandler].TryGetValue (System.Type key, IExceptionHandler& value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:588)
Amazon.Runtime.Internal.ErrorHandler.ProcessException (IExecutionContext executionContext, System.Exception exception) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/ErrorHandler/ErrorHandler.cs:202)
Amazon.Runtime.Internal.ErrorHandler.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/ErrorHandler/ErrorHandler.cs:78)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/PipelineHandler.cs:57)
Amazon.Runtime.Internal.CallbackHandler.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/Handlers/CallbackHandler.cs:45)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/PipelineHandler.cs:57)
Amazon.Runtime.Internal.Signer.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/Handlers/Signer.cs:38)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/PipelineHandler.cs:57)
Amazon.Runtime.Internal.CredentialsRetriever.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/Handlers/CredentialsRetriever.cs:72)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/PipelineHandler.cs:57)
Amazon.Runtime.Internal.RetryHandler.InvokeSync (IExecutionContext executionContext) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Core/Amazon.Runtime/Pipeline/RetryHandler/RetryHandler.cs:74)

It gets even worse, if I want to query based on a secondary index. 如果我想基于二级索引进行查询,情况将更加糟糕。 For that, I need to attach a DynamoDBOperationConfig after the conditions for hash and range key like so: 为此,我需要在哈希和范围键的条件之后附加一个DynamoDBOperationConfig,如下所示:

var queryConfig = new DynamoDBOperationConfig() { IndexName = "PlayerIndex" };
AsyncSearch<POIVisit> query = AWSManager.Instance.DBContext.QueryAsync<POIVisit>(poiType, QueryOperator.Equal, PlayerID, queryConfig);

Now, it somehow seems to think, the queryConfig is just another value for the range key (so it basically chooses the wrong overloaded version of QueryAsync). 现在,似乎以某种方式认为,queryConfig只是范围键的另一个值(因此,它基本上选择了错误的QueryAsync重载版本)。 Or at least I think that's what happens, because I get a weird conversion error now: 或者至少我认为是这样,因为现在我收到一个奇怪的转换错误:

InvalidCastException: Cannot cast from source type to destination type.
Amazon.DynamoDBv2.Converter`1[T].TryTo (System.Object value, Amazon.DynamoDBv2.DocumentModel.Primitive& p) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Services/DynamoDBv2/Custom/Conversion/DynamoDBEntryConversion.cs:630)
…
Amazon.DynamoDBv2.DataModel.DynamoDBContext.QueryAsync[POIVisit] (System.Object hashKeyValue, QueryOperator op, System.Object[] values) (at E:/JenkinsWorkspaces/v3-trebuchet-release/AWSDotNetPublic/sdk/src/Services/DynamoDBv2/Custom/DataModel/_unity/Context.Async.cs:550)
DBManager.RetrievePlayerVisits (System.String poiType, UnityEngine.Events.UnityAction`1 callback) (at Assets/Scripts/Managers/DBManager.cs:359)

My Unity version is 5.6.3f1. 我的Unity版本是5.6.3f1。 I tried AWSSDK.DynamoDBv2.3.3.4.18 and the older AWSSDK.DynamoDBv2.3.3.4.14 Unity packages containing the AWS SDK downloaded directly from Amazon. 我尝试了AWSSDK.DynamoDBv2.3.3.4.18和较旧的AWSSDK.DynamoDBv2.3.3.4.14 Unity软件包,其中包含直接从亚马逊下载的AWS开发工具包。

If someone has a hint what I could be doing wrong, I'd be really grateful, thanks! 如果有人暗示我可能做错了什么,我将非常感谢,谢谢!

You need to build a request object before you create a query. 创建查询之前,您需要构建一个请求对象。 I've never used Unity but it should be something like 我从没用过Unity,但是应该像

var request = new QueryRequest();
request.TableName = "YourTable";   
request.KeyConditions = new Dictionary<string, Condition>()
{
    { 
        "YourId",  new Condition()
        { 
            ComparisonOperator = "EQ",
            AttributeValueList = new List<AttributeValue>()
            {
                new AttributeValue { S = YourId }
            }
        }
    }
};

client.QueryAsync(request,(result));

Specifically, it looks like you are not including your keycondition as part of the request, which is required. 具体来说,您似乎没有将关键条件作为要求的一部分包括在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM