2015-10-05 13 views
6

Ecco il mio codice.
Voglio inserire asset_data json nella colonna asset_data. sto usando aws sdk. Si dice che aws sdk ora ha il supporto per JSON. http://aws.amazon.com/releasenotes/SDK/JavaScript/1691866671551861Come inserire json in dynamodb

var asset_data = { 
    "name": "name" + i, 
    "contentUrl": "http://www.hdwallpapersimages.com/nature-beauty-desktop-images/94892/", 
    "size": 300, 
    "headline": "headline", 
    "description": "assetUrl reference for the creator", 
    "encodingFormat": 'jpeg' 
    }; 

    var params = { 
    TableName: 'xyz', 
    Item: { // a map of attribute name to AttributeValue 
     "asset_id": {S: "asset" + i}, 
     "hit_id": {S: "0"}, 
     "created_date": {"S": Date.now().toString()}, 
     "status": {N: "0"}, 
     "operation": {S: "image_tagging"}, 
     "asset_data": {L: asset_data}, 
     "source": {S: "DAM"}, 
     "completed_date": {S: Date.now().toString()}, 
     "response_data": {S: "taged value"} 
     // more attributes... 
    }, 

    ReturnValues: 'NONE', // optional (NONE | ALL_OLD) 
    ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES) 
    ReturnItemCollectionMetrics: 'NONE' // optional (NONE | SIZE) 
    }; 

    db.putItem(params, function (err, data) { 
    if (err) console.log(err); // an error occurred 
    else console.log("inserted..."); // successful response 
    }); 

risposta

7

È possibile utilizzare il documento DynamoDB Client SDK:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property

Il cliente documento semplifica il lavoro con gli elementi in Amazzonia DynamoDB astraendo via la nozione di valori degli attributi. Questa astrazione annota i tipi nativi di JavaScript forniti come parametri di input, come e converte i dati di risposta annotati in tipi di JavaScript nativi.

Per il vostro caso specifico cerca nella mappa attributo value (M) essere passato come MapAttribute nell'esempio seguente estratto dalla documentazione ufficiale. L'API client documento si prende cura del corretto smistamento/unmarshalling tra i tipi di Javascript e DynamoDB (il che significa che non c'è bisogno di specificare il Attribute Values (S,N,L,...) in quanto è necessaria quando si utilizza l'SDK non basato su documento):

var params = { 
    TableName = 'Table', 
    Item: { 
    HashKey: 'haskey', 
    NumAttribute: 1, 
    BoolAttribute: true, 
    ListAttribute: [1, 'two', false], 
    MapAttribute: { foo: 'bar'}, 
    NullAttribute: null 
    } 
}; 

var docClient = new AWS.DynamoDB.DocumentClient(); 

docClient.put(params, function(err, data) { 
    if (err) console.log(err); 
    else console.log(data); 
}); 
+1

C'è un modo per usare questa cosa DocumentClient() dalla CLI per fare un put-item? Non posso vederlo nella documentazione. Grazie! – Gopala

Problemi correlati