kotlin 从AWS DynamoDB查询数据

ruoxqz4g  于 2023-05-01  发布在  Kotlin
关注(0)|答案(2)|浏览(261)

我正在尝试使用Kotlin在我的android应用程序中从DynamoDB查询数据。代码应该是正确的,但是查看日志,它似乎没有执行代码,因为我没有收到任何类型的结果。想知道是否有人知道根据日志发生了什么?
https://i.stack.imgur.com/LzZ5D.png

  1. fun getUserData(email: String) {
  2. println("We will get the userdata!!!!!!!!")
  3. Amplify.DataStore.query(
  4. User::class.java,
  5. { items ->
  6. while (items.hasNext()) {
  7. val item = items.next()
  8. Log.i("Amplify", "Queried item: " + item.name)
  9. }
  10. },
  11. { failure -> Log.e("Tutorial", "Could not query DataStore", failure) }
  12. )
  13. }

023-04-21 19:41:19.981 16806-16893 TrafficStats com.example.aupairconnect D tagSocket(119)with statsTag=0xffffffff,statsUid=-1 2023-04-21 19:41:20.011 16806-16806兼容.. geReporter com.example.aupairconnect D Compat change id reported:171228096; UID 10162;状态:已启用2023-04-21 19:41:20。211 16806-16881 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock released.2023-04-21 19:41:20.211 16806-16881 MyAmplifyApp com.example.aupairconnect I DataStore started 2023-04-21 19:41:20.531 16806-16887 e.aupairconnect com.example. aupairconnectWLong监视void com处与所有者池-5-线程-1(16886)的争用。扩增框架api.aws.SubscriptionEndpoint.requestSubscription(com.扩增框架api.graphql.GraphQLRequest,com.扩增框架API.aws.AuthorizationType,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架action)(SubscriptionEndpoint.java:196)waiters=0 in void com.扩增框架api.aws.SubscriptionEndpoint.requestSubscription(com.扩增框架api.graphql.GraphQLRequest,com.扩增框架API.aws.AuthorizationType,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架核心.动作)为562 ms 2023-04-21 19:41:20.666 16806-16888 e.aupairconnect com.example. aupairconnectWLong监视void com处与所有者池-5-线程-1(16886)的争用。扩增框架api.aws.SubscriptionEndpoint.requestSubscription(com.扩增框架api.graphql.GraphQLRequest,com.扩增框架API.aws.AuthorizationType,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架action)(SubscriptionEndpoint.java:196)waiters=1 in void com.扩增框架api.aws.SubscriptionEndpoint.requestSubscription(com.扩增框架api.graphql.GraphQLRequest,com.扩增框架API.aws.AuthorizationType,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架core.Consumer,com.扩增框架核心.动作)为695 ms 2023-04-21 19:41:20.793 16806-16880放大:aws-datastore com.example.aupairconnect I Started订阅处理器型号:类型[ON_CREATE,ON_UPDATE,ON_DELETE]的[用户]。2023-04-21 19:41:20.801 16806-16893 TrafficStats com.example.aupairconnect D tagSocket(120)with statsTag=0xffffffff,statsUid=-1 2023-04-21 19:41:20.975 16806-16860 EGL_emulsion com.example.aupairconnect D app_time_stats:平均值=73.29ms min=0。58 ms最大值=656。79 ms计数=17 2023-04-21 19:41:21。041 16806-16806 OnBackInvokedCallback com.example.aupairconnect W OnBackInvokedCallback未为应用程序启用。在应用程序清单中设置'android:enableOnBackInvokedCallback=“true”'。2023-04-21 19:41:21.060 16806-16904 amplify:aws-datastore com.example.aupairconnect I成功地从云中同步了模型状态。2023-04-21 19:41:21.061 16806-16880 amplify:aws-datastore com.example.aupairconnect I开始处理订阅数据缓冲区。2023-04-21 19:41:21.061 16806-16880 amplify:aws-datastore com.example.aupairconnect我在API同步模式下启动了orchestrator。2023-04-21 19:41:21.061 16806-16879 amplify:aws-datastore com.example.aupairconnect我开始处理mutation发件箱。待决的变化将被发布到云。2023-04-21 19:41:21.068 16806-16806系统out com.example.aupairconnect I我们将获得用户数据!!!!!!!2023-04-21 19:41:21.068 16806-16806 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock acquired.2023-04-21 19:41:21.068 16806-16885 amplify:aws-datastore com.example.aupairconnect I DataStore插件初始化。2023-04-21 19:41:21.074 16806-16806系统out com.example.aupairconnect I我们将获得用户数据!!!!!!!2023-04-21 19:41:21.321 16806-16885 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock released.2023-04-21 19:41:21.321 16806-16806 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock acquired.2023-04-21 19:41:21.322 16806-16882 amplify:aws-datastore com.example.aupairconnect I DataStore插件初始化。2023-04-21 19:41:21.575 16806-16882 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock released.2023-04-21 19:41:24.656 16806-16911 ProfileInstaller com.example.aupairconnect D正在为com.example.aupairconnect
https://i.stack.imgur.com/auf0U.png
我试图通过放大器的文档寻找更多的澄清,如果我错过了什么,但它看起来像是所有在那里。

g52tjvyc

g52tjvyc1#

如果你想使用Kotlin调用Amazon DynamoDB(和其他AWS服务),那么我强烈建议你使用AWS SDK for Kotlin和**DynamoDbClient**。您可以在这里阅读DEV指南:
AWS SDK for Kotlin Developer Guide
要使用KotlinSDK从Amazon DynamoDB表中查询数据,请使用以下代码:

  1. package com.kotlin.dynamodb
  2. // snippet-start:[dynamodb.kotlin.query.import]
  3. import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
  4. import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
  5. import aws.sdk.kotlin.services.dynamodb.model.QueryRequest
  6. import kotlin.system.exitProcess
  7. // snippet-end:[dynamodb.kotlin.query.import]
  8. /**
  9. Before running this Kotlin code example, set up your development environment,
  10. including your credentials.
  11. For more information, see the following documentation topic:
  12. https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html
  13. */
  14. suspend fun main(args: Array<String>) {
  15. val usage = """
  16. Usage:
  17. <tableName>
  18. Where:
  19. tableName - The Amazon DynamoDB table to scan (for example, Music3).
  20. partitionKeyName - The partition key name of the Amazon DynamoDB table (for example, Artist).
  21. partitionKeyVal - The value of the partition key that should match (for example, Famous Band).
  22. """
  23. if (args.size != 3) {
  24. println(usage)
  25. exitProcess(0)
  26. }
  27. val tableName = args[0]
  28. val partitionKeyName = args[1]
  29. val partitionKeyVal = args[2]
  30. // For more information about an alias, see:
  31. // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html
  32. val partitionAlias = "#a"
  33. val count = queryDynTable(tableName, partitionKeyName, partitionKeyVal, partitionAlias)
  34. print("There is $count item in $tableName")
  35. }
  36. // snippet-start:[dynamodb.kotlin.query.main]
  37. suspend fun queryDynTable(
  38. tableNameVal: String,
  39. partitionKeyName: String,
  40. partitionKeyVal: String,
  41. partitionAlias: String
  42. ): Int {
  43. val attrNameAlias = mutableMapOf<String, String>()
  44. attrNameAlias[partitionAlias] = partitionKeyName
  45. // Set up mapping of the partition name with the value.
  46. val attrValues = mutableMapOf<String, AttributeValue>()
  47. attrValues[":$partitionKeyName"] = AttributeValue.S(partitionKeyVal)
  48. val request = QueryRequest {
  49. tableName = tableNameVal
  50. keyConditionExpression = "$partitionAlias = :$partitionKeyName"
  51. expressionAttributeNames = attrNameAlias
  52. this.expressionAttributeValues = attrValues
  53. }
  54. DynamoDbClient { region = "us-east-1" }.use { ddb ->
  55. val response = ddb.query(request)
  56. return response.count
  57. }
  58. }
  59. // snippet-end:[dynamodb.kotlin.query.main]

KotlinSDK也非常适合在Android Studio项目中使用。下面是一个教程,向您展示如何:
Creating a publish/subscription Android application that translates messages using the AWS SDK for Kotlin

展开查看全部
vwkv1x7d

vwkv1x7d2#

我发现数据没有通过,因为数据不在DynamoDb上的AmplifyDataStore表中,但奇怪的是它在User表中。在应用程序中删除然后再次保存数据,它能够保存到两个表,因此能够通过API访问。

相关问题