kotlin 从AWS DynamoDB查询数据

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

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

fun getUserData(email: String) {
    println("We will get the userdata!!!!!!!!")
    Amplify.DataStore.query(
        User::class.java,
        { items ->
            while (items.hasNext()) {
                val item = items.next()
                Log.i("Amplify", "Queried item: " + item.name)
            }
        },
        { failure -> Log.e("Tutorial", "Could not query DataStore", failure) }
    )
}

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表中查询数据,请使用以下代码:

package com.kotlin.dynamodb

// snippet-start:[dynamodb.kotlin.query.import]
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
import aws.sdk.kotlin.services.dynamodb.model.QueryRequest
import kotlin.system.exitProcess
// snippet-end:[dynamodb.kotlin.query.import]

/**
Before running this Kotlin code example, set up your development environment,
including your credentials.
For more information, see the following documentation topic:
https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html
*/

suspend fun main(args: Array<String>) {

    val usage = """
    Usage:
        <tableName>
    Where:
        tableName - The Amazon DynamoDB table to scan (for example, Music3).
        partitionKeyName - The partition key name of the Amazon DynamoDB table (for example, Artist).
        partitionKeyVal - The value of the partition key that should match (for example, Famous Band).
    """

    if (args.size != 3) {
        println(usage)
        exitProcess(0)
    }

    val tableName = args[0]
    val partitionKeyName = args[1]
    val partitionKeyVal = args[2]

    // For more information about an alias, see:
    // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html
    val partitionAlias = "#a"
    val count = queryDynTable(tableName, partitionKeyName, partitionKeyVal, partitionAlias)
    print("There is $count item in $tableName")
}

// snippet-start:[dynamodb.kotlin.query.main]
suspend fun queryDynTable(
    tableNameVal: String,
    partitionKeyName: String,
    partitionKeyVal: String,
    partitionAlias: String
): Int {

    val attrNameAlias = mutableMapOf<String, String>()
    attrNameAlias[partitionAlias] = partitionKeyName

    // Set up mapping of the partition name with the value.
    val attrValues = mutableMapOf<String, AttributeValue>()
    attrValues[":$partitionKeyName"] = AttributeValue.S(partitionKeyVal)

    val request = QueryRequest {
        tableName = tableNameVal
        keyConditionExpression = "$partitionAlias = :$partitionKeyName"
        expressionAttributeNames = attrNameAlias
        this.expressionAttributeValues = attrValues
    }

    DynamoDbClient { region = "us-east-1" }.use { ddb ->
        val response = ddb.query(request)
        return response.count
    }
}
// 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访问。

相关问题