首頁  >  文章  >  後端開發  >  如何修復 Query dynamodb 請求的錯誤?

如何修復 Query dynamodb 請求的錯誤?

WBOY
WBOY轉載
2024-02-11 22:50:09654瀏覽

如何修复 Query dynamodb 请求的错误?

php小編魚仔為您呈現:如何修復Query dynamodb請求的錯誤? DynamoDB是亞馬遜提供的一種高度可擴充的NoSQL資料庫服務。使用DynamoDB進行查詢時,可能會遇到各種錯誤,影響系統的正常運作。要修復這些錯誤,首先需要仔細檢查查詢語句的正確性,並確保提供的參數和表格結構相符。另外,還可以透過增加Read Capacity來提高查詢效能,使用適當的索引來最佳化查詢速度。此外,了解並遵循DynamoDB的最佳實踐,例如合理使用批次操作和條件表達式等,也能有效避免查詢錯誤。透過上述方法,您可以輕鬆修復和優化DynamoDB查詢中的錯誤,提升系統的穩定性和效能。

問題內容

在 dynamodb 中,我有一個表,其中包含:

- email (primary key)
- password (attribute)
- rname (attribute)

我正在使用 aws go sdk v1 來實作僅使用資料庫主鍵執行查詢:

我要解群組的結構是:

type item struct {
    email    string `json:"email"`
    password string `json:"password"`
    rname    string `json:"rname"`
}

與程式碼:

result, err := client.Query(&dynamodb.QueryInput{
        TableName: aws.String("accountsTable"),
        KeyConditions: map[string]*dynamodb.Condition{
            "email": {
                ComparisonOperator: aws.String("EQ"),
                AttributeValueList: []*dynamodb.AttributeValue{
                    {
                        S: aws.String(email),
                    },
                },
            },
        },
    })
    if err != nil {
        return false, err
    }

    item := []Item{}

    err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &item)
    if err != nil {
        return false, err
    }

但是,我收到金鑰無效的問題。我檢查資料庫中的密鑰,它也與我列印到控制台的密鑰相符。

不知道如何解決這個問題,因為我看到的範例似乎適用於他們的並且看起來相同。

任何解決此問題的幫助將不勝感激:)

解決方法

您需要將密碼和rname 的值設為omitempty,以便它不會設定為空值,因為它們不是鍵,它們不應包含在查詢中,因為它會引發無效鍵異常:

type item struct {
    email    string `json:"email" dynamodbav:"email,omitempty"`
    password string `json:"password" dynamodbav:"password,omitempty"`
    rname    string `json:"rname" dynamodbav:"rname,omitempty"`
}

更新

我認為問題是由於您嘗試將整個回應編組到單一命令中,但是,迭代對我有用。 (我不使用 go)。

package main
import (
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/dynamodb"
    "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"

    "fmt"
)


func main() {

    // create session
    sess := session.must(session.newsessionwithoptions(session.options{
        sharedconfigstate: session.sharedconfigenable,
    }))

    // create dynamodb client with logging
    client := dynamodb.new(sess, aws.newconfig())


    type item struct {
        email    string `dynamodbav: "email"`
        password string `dynamodbav: "password,omitempty"`
        rname    string `dynamodbav: "rname,omitempty"`
    }

    result, err := client.query(&dynamodb.queryinput{
        tablename: aws.string("accountstable"),
        keyconditions: map[string]*dynamodb.condition{
            "email": {
                comparisonoperator: aws.string("eq"),
                attributevaluelist: []*dynamodb.attributevalue{
                    {
                        s: aws.string("<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="aec2c6c0c0c9eecfc3cfd4c1c080cdc1c3">[email&#160;protected]</a>"),
                    },
                },
            },
        },
    })

    if err != nil {
        fmt.println("query api call failed:")
        fmt.println((err.error()))
    }



    for _, i := range result.items {
        
        item := item{}
        err = dynamodbattribute.unmarshalmap(i, &item)
    
        if err != nil {
            fmt.println("got error unmarshalling: %s", err)
        }
    
        fmt.println("email: ", item.email)
        fmt.println()
        
    }
}

此外,當您使用email 的單一金鑰時,這意味著最多有1 個項目具有相同的電子郵件地址,這意味著您應該使用getitem 而不是query

package main

import (
    "fmt"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/dynamodb"
    "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

func main() {

    // Item to Get
    type Item struct {
        Email    string `dynamodbav: "email"`
        Password string `dynamodbav: "password,omitempty"`
        Rname    string `dynamodbav: "rname,omitempty"`
    }

    // Create Session
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    // Create DynamoDB Client
    client := dynamodb.New(sess, aws.NewConfig())

    // Get Item
    result, err := client.GetItem(&dynamodb.GetItemInput{
        TableName: aws.String("accountsTable"),
        Key: map[string]*dynamodb.AttributeValue{
            "email": {
                S: aws.String("<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="4a262224242d0a2b272b30252464292527">[email&#160;protected]</a>"),
            },
        },
    })

    // Catch Error
    if err != nil {
        fmt.Println("GetItem API call failed:")
        fmt.Println((err.Error()))
    }

    item := Item{}

    // Unmarhsall
    err = dynamodbattribute.UnmarshalMap(result.Item, &item)

    if err != nil {
        panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
    }

    // If Item Returns Empty
    if item.Email == "" {
        fmt.Println("Could not find Item")
        return
    }

    // Print Result
    fmt.Println("Found item:")
    fmt.Println("Email:  ", item.Email)

}

以上是如何修復 Query dynamodb 請求的錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除