1

DynamoDB

 ArrayList<AttributeDefinition> attributeDefinitions = new 
        ArrayList<>();
        attributeDefinitions.add(new AttributeDefinition()
                    .withAttributeName("ID").withAttributeType("S"));
     
       ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
       keySchema.add(new KeySchemaElement()
                    .withAttributeName("ID").withKeyType(KeyType.HASH));
     
       CreateTableRequest createTableReq = new CreateTableRequest()
                    .withTableName("Product")
                    .withAttributeDefinitions(attributeDefinitions)
                    .withKeySchema(keySchema)
                    .withProvisionedThroughput(new 
                     ProvisionedThroughput()
                            .withReadCapacityUnits(10L)
                            .withWriteCapacityUnits(5L));
       CreateTableResult result =  dynamodb.createTable(createTableReq);
       System.out.println(result.toString());

If I want to create a table like this where can I put this code to work just once, is there any migration script tool or something like that?

Grigor
  • 58
  • 10
  • 1
    Create table if not exists isn't supported by DynamoDB, if this is what you are asking for. See https://stackoverflow.com/questions/37330575/dynamodb-createtable-if-not-exists for some options. – kgiannakakis Nov 17 '21 at 11:37
  • https://stackoverflow.com/users/24054/kgiannakakis. No for example when I use postgreSql I use migartion with sql to keep orders like delete update etc,I want to do the same with dynamodb – Grigor Nov 17 '21 at 11:51

1 Answers1

1

DynamoDB is a NoSQL database, meaning you are out of luck if you want a simple query language to interact with it.

See reference: Automating dynamodb scripts

Now there might be good news, (If you know Javascript) :)

DynamoDB offers a simple SDK for Javascript, that you could create an xyz.js JavaScript file, and run this script using node

Example for Adding Entries:

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Load credentials and set Region from JSON file
AWS.config.loadFromPath('./config.json');

// Create DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

var myTable = 'TABLE_NAME';

// Add the four results for spades
var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '0'}, 'imageFile' : {S: 'spad_a.png'}
  }
};
post();

var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '1'}, 'imageFile' : {S: 'spad_k.png'}
  }
};
post();

var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '2'}, 'imageFile' : {S: 'spad_q.png'}
  }
};
post();

var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '3'}, 'imageFile' : {S: 'spad_j.png'}
  }
};
post();

// Add the four results for hearts
.
.
.

// Add the four results for diamonds
.
.
.

// Add the four results for clubs
var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '12'}, 'imageFile' : {S: 'club_a.png'}
  }
};
post();

var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '13'}, 'imageFile' : {S: 'club_k.png'}
  }
};
post();

var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '14'}, 'imageFile' : {S: 'club_q.png'}
  }
};
post();

var params = {
  TableName: myTable,
  Item: {'slotPosition' : {N: '15'}, 'imageFile' : {S: 'club_j.png'}
  }
};
post();


function post () {
  ddb.putItem(params, function(err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      console.log("Success", data);
    }
  });
}

For reference: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-lambda-ddb-setup.html

JCompetence
  • 6,997
  • 3
  • 19
  • 26