2016-06-29 7 views
2

Ich möchte eine JSON-Antwort in aws-dynamodb speichern, verwende ich aws-dynamodb-sdk. Was ich zur Zeit mache ist:So speichern Sie JSON-Antwort in dynamodb in GO

func (e *DB) saveToDynamodb(data map[string]interface{}){ 
params := &dynamodb.PutItemInput{ 
    Item: map[string]*dynamodb.AttributeValue{ 
     "Key": { 
      M: data, 
     }, 
    }, 
    TableName: aws.String("Asset_Data"), 
} 
resp, err := e.dynamodb.PutItem(params) 

if err != nil { 
    fmt.Println(err.Error()) 
    return 
} 
fmt.Println(resp) 
} 

Aber wie Sie Daten ist von map [string] Schnittstelle {} Typ, während die erwarteten Typ Karte ist sehen [string] * Attribute, deshalb Kompilierungsfehler geben.

Gibt es eine Problemumgehung zum Speichern einer JSON-Antwort?

Antwort

0

JSON zu setzen in aws-DynamoDB wir zuerst über jedes Attribut von JSON-Struktur und wandelt es in dynamodb.AttributeValue in der folgenden Weise durchlaufen müssen:

func (e *DB) saveToDynamodb(data map[string]interface{}){ 
var vv=make(map[string]*dynamodb.AttributeValue) 

for k,v:=range data{ 
    x:=(v.(string)) //assert string type 
    xx:=&(x) 
    vv[k]=&dynamodb.AttributeValue{S: xx,} 
} 
//s:=data["asset_id"].(string) 
params := &dynamodb.PutItemInput{ 
    Item: vv, 
    TableName: aws.String("Asset_Data"), // Required 
} 
resp, err := e.dynamodb.PutItem(params) 

if err != nil { 
    // Print the error, cast err to awserr.Error to get the Code and 
    // Message from an error. 
    fmt.Println(err.Error()) 
    return 
} 

// Pretty-print the response data. 
fmt.Println(resp) 
} 
0

In diesem Fall sollten Sie type assertion verwenden.

es versuchen diese geben:

func (e *DB) saveToDynamodb(data map[string]interface{}){ 
    params := &dynamodb.PutItemInput{ 
     Item: map[string]*dynamodb.AttributeValue{ 
      "Key": { 
       M: data.(map[string]*dynamodb.AttributeValue), // assert interface to *dynamodb.AttributeValue 
      }, 
     }, 
     TableName: aws.String("Asset_Data"), 
    } 
    resp, err := e.dynamodb.PutItem(params) 

    if err != nil { 
     fmt.Println(err.Error()) 
     return 
    } 
    fmt.Println(resp) 
} 
+0

Sie können nicht, dass es eine ungültige Assertion ist, weil Daten keine Schnittstelle sind. –

2

Der beste Weg, um Json in DynamoDB zu setzen, ist die Verwendung der Hilfsfunktionen.

func ExampleMarshal() (map[string]*dynamodb.AttributeValue, error) {                          
    type Record struct {                          
    Bytes []byte                           
    MyField string                           
    Letters []string                          
    Numbers []int                           
    }                               

    r := Record{                            
    Bytes: []byte{48, 49},                        
    MyField: "MyFieldValue",                        
    Letters: []string{"a", "b", "c", "d"},                     
    Numbers: []int{1, 2, 3},                        
    }                               
    av, err := dynamodbattribute.Marshal(r) 
    return map[string]*dynamodb.AttributeValue{"object":av}, err 
} 
+0

wo ist dynamobattribute definiert? – Homan

+0

[Paket] (https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/dynamodbattribute/) – Xibz