Sample code for 30+ languages & platforms
Lianja

DynamoDB - CreateTable

See more Amazon DynamoDB Examples

The CreateTable operation adds a new table to your account. In an AWS account, table names must be unique within each Region. That is, you can have two tables with same name if you create the tables in different Regions.

CreateTable is an asynchronous operation. Upon receiving a CreateTable request, DynamoDB immediately returns a response with a TableStatus of CREATING. After the table is created, DynamoDB sets the TableStatus to ACTIVE. You can perform read and write operations only on an ACTIVE table.

You can use the DescribeTable action to check the table status.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

loRest = createobject("CkRest")

// Implements the following CURL command:

// curl -X POST https://dynamodb.us-west-2.amazonaws.com/ \
//   -H "Accept-Encoding: identity" \
//   -H "Content-Type: application/x-amz-json-1.0" \
//   -H "Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=<Headers>, Signature=<Signature>" \
//   -H "X-Amz-Date: <Date>" \
//   -H "X-Amz-Target: DynamoDB_20120810.CreateTable" \
//   -d '{
//     "AttributeDefinitions": [
//         {
//             "AttributeName": "ForumName",
//             "AttributeType": "S"
//         },
//         {
//             "AttributeName": "Subject",
//             "AttributeType": "S"
//         },
//         {
//             "AttributeName": "LastPostDateTime",
//             "AttributeType": "S"
//         }
//     ],
//     "TableName": "Thread",
//     "KeySchema": [
//         {
//             "AttributeName": "ForumName",
//             "KeyType": "HASH"
//         },
//         {
//             "AttributeName": "Subject",
//             "KeyType": "RANGE"
//         }
//     ],
//     "LocalSecondaryIndexes": [
//         {
//             "IndexName": "LastPostIndex",
//             "KeySchema": [
//                 {
//                     "AttributeName": "ForumName",
//                     "KeyType": "HASH"
//                 },
//                 {
//                     "AttributeName": "LastPostDateTime",
//                     "KeyType": "RANGE"
//                 }
//             ],
//             "Projection": {
//                 "ProjectionType": "KEYS_ONLY"
//             }
//         }
//     ],
//     "ProvisionedThroughput": {
//         "ReadCapacityUnits": 5,
//         "WriteCapacityUnits": 5
//     },
//     "Tags": [ 
//       { 
//          "Key": "Owner",
//          "Value": "BlueTeam"
//       }
//    ]
// }'

// Use the following online tool to generate REST code from a CURL command
// Convert a cURL Command to REST Source Code

loAuthAws = createobject("CkAuthAws")
loAuthAws.AccessKey = "AWS_ACCESS_KEY"
loAuthAws.SecretKey = "AWS_SECRET_KEY"
// Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
loAuthAws.Region = "us-west-2"
loAuthAws.ServiceName = "dynamodb"
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
loRest.SetAuthAws(loAuthAws)

// URL: https://dynamodb.us-west-2.amazonaws.com/
llBTls = .T.
lnPort = 443
llBAutoReconnect = .T.
// Don't forget to change the region domain (us-west-2.amazonaws.com) to your particular region.
llSuccess = loRest.Connect("dynamodb.us-west-2.amazonaws.com",lnPort,llBTls,llBAutoReconnect)
if (llSuccess <> .T.) then
    ? "ConnectFailReason: " + str(loRest.ConnectFailReason)
    ? loRest.LastErrorText
    release loRest
    release loAuthAws
    return
endif

// Note: The above code does not need to be repeatedly called for each REST request.
// The rest object can be setup once, and then many requests can be sent.  Chilkat will automatically
// reconnect within a FullRequest* method as needed.  It is only the very first connection that is explicitly
// made via the Connect method.

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "AttributeDefinitions": [
//     {
//       "AttributeName": "ForumName",
//       "AttributeType": "S"
//     },
//     {
//       "AttributeName": "Subject",
//       "AttributeType": "S"
//     },
//     {
//       "AttributeName": "LastPostDateTime",
//       "AttributeType": "S"
//     }
//   ],
//   "TableName": "Thread",
//   "KeySchema": [
//     {
//       "AttributeName": "ForumName",
//       "KeyType": "HASH"
//     },
//     {
//       "AttributeName": "Subject",
//       "KeyType": "RANGE"
//     }
//   ],
//   "LocalSecondaryIndexes": [
//     {
//       "IndexName": "LastPostIndex",
//       "KeySchema": [
//         {
//           "AttributeName": "ForumName",
//           "KeyType": "HASH"
//         },
//         {
//           "AttributeName": "LastPostDateTime",
//           "KeyType": "RANGE"
//         }
//       ],
//       "Projection": {
//         "ProjectionType": "KEYS_ONLY"
//       }
//     }
//   ],
//   "ProvisionedThroughput": {
//     "ReadCapacityUnits": 5,
//     "WriteCapacityUnits": 5
//   },
//   "Tags": [
//     {
//       "Key": "Owner",
//       "Value": "BlueTeam"
//     }
//   ]
// }

loJson = createobject("CkJsonObject")
i = 0
loJson.I = i
loJson.UpdateString("AttributeDefinitions[i].AttributeName","ForumName")
loJson.UpdateString("AttributeDefinitions[i].AttributeType","S")
i = i + 1
loJson.I = i
loJson.UpdateString("AttributeDefinitions[i].AttributeName","Subject")
loJson.UpdateString("AttributeDefinitions[i].AttributeType","S")
i = i + 1
loJson.I = i
loJson.UpdateString("AttributeDefinitions[i].AttributeName","LastPostDateTime")
loJson.UpdateString("AttributeDefinitions[i].AttributeType","S")
loJson.UpdateString("TableName","Thread")
i = 0
loJson.I = i
loJson.UpdateString("KeySchema[i].AttributeName","ForumName")
loJson.UpdateString("KeySchema[i].KeyType","HASH")
i = i + 1
loJson.I = i
loJson.UpdateString("KeySchema[i].AttributeName","Subject")
loJson.UpdateString("KeySchema[i].KeyType","RANGE")

i = 0
loJson.I = i
j = 0
loJson.J = j
loJson.UpdateString("LocalSecondaryIndexes[i].IndexName","LastPostIndex")
loJson.UpdateString("LocalSecondaryIndexes[i].KeySchema[j].AttributeName","ForumName")
loJson.UpdateString("LocalSecondaryIndexes[i].KeySchema[j].KeyType","HASH")
j = j + 1
loJson.J = j
loJson.UpdateString("LocalSecondaryIndexes[i].KeySchema[j].AttributeName","LastPostDateTime")
loJson.UpdateString("LocalSecondaryIndexes[i].KeySchema[j].KeyType","RANGE")
loJson.UpdateString("LocalSecondaryIndexes[i].Projection.ProjectionType","KEYS_ONLY")
loJson.UpdateInt("ProvisionedThroughput.ReadCapacityUnits",5)
loJson.UpdateInt("ProvisionedThroughput.WriteCapacityUnits",5)
loJson.UpdateString("Tags[0].Key","Owner")
loJson.UpdateString("Tags[0].Value","BlueTeam")

loRest.AddHeader("Content-Type","application/x-amz-json-1.0")
loRest.AddHeader("X-Amz-Target","DynamoDB_20120810.CreateTable")
loRest.AddHeader("Accept-Encoding","identity")

loSbRequestBody = createobject("CkStringBuilder")
loJson.EmitSb(loSbRequestBody)
loSbResponseBody = createobject("CkStringBuilder")
llSuccess = loRest.FullRequestSb("POST","/",loSbRequestBody,loSbResponseBody)
if (llSuccess <> .T.) then
    ? loRest.LastErrorText
    release loRest
    release loAuthAws
    release loJson
    release loSbRequestBody
    release loSbResponseBody
    return
endif

lnRespStatusCode = loRest.ResponseStatusCode
? "response status code = " + str(lnRespStatusCode)

if (lnRespStatusCode >= 400) then
    ? "Response Status Code = " + str(lnRespStatusCode)
    ? "Response Header:"
    ? loRest.ResponseHeader
    ? "Response Body:"
    ? loSbResponseBody.GetAsString()
    release loRest
    release loAuthAws
    release loJson
    release loSbRequestBody
    release loSbResponseBody
    return
endif

loJsonResponse = createobject("CkJsonObject")
loJsonResponse.LoadSb(loSbResponseBody)

loJsonResponse.EmitCompact = .F.
? loJsonResponse.Emit()

// Sample Response:

//  {
//     "TableDescription": {
//         "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/Thread",
//         "AttributeDefinitions": [
//             {
//                 "AttributeName": "ForumName",
//                 "AttributeType": "S"
//             },
//             {
//                 "AttributeName": "LastPostDateTime",
//                 "AttributeType": "S"
//             },
//             {
//                 "AttributeName": "Subject",
//                 "AttributeType": "S"
//             }
//         ],
//         "CreationDateTime": 1.36372808007E9,
//         "ItemCount": 0,
//         "KeySchema": [
//             {
//                 "AttributeName": "ForumName",
//                 "KeyType": "HASH"
//             },
//             {
//                 "AttributeName": "Subject",
//                 "KeyType": "RANGE"
//             }
//         ],
//         "LocalSecondaryIndexes": [
//             {
//                 "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/Thread/index/LastPostIndex",
//                 "IndexName": "LastPostIndex",
//                 "IndexSizeBytes": 0,
//                 "ItemCount": 0,
//                 "KeySchema": [
//                     {
//                         "AttributeName": "ForumName",
//                         "KeyType": "HASH"
//                     },
//                     {
//                         "AttributeName": "LastPostDateTime",
//                         "KeyType": "RANGE"
//                     }
//                 ],
//                 "Projection": {
//                     "ProjectionType": "KEYS_ONLY"
//                 }
//             }
//         ],
//         "ProvisionedThroughput": {
//             "NumberOfDecreasesToday": 0,
//             "ReadCapacityUnits": 5,
//             "WriteCapacityUnits": 5
//         },
//         "TableName": "Thread",
//         "TableSizeBytes": 0,
//         "TableStatus": "CREATING"
//     }
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

lcTableDescriptionTableArn = loJsonResponse.StringOf("TableDescription.TableArn")
lcTableDescriptionCreationDateTime = loJsonResponse.StringOf("TableDescription.CreationDateTime")
lnTableDescriptionItemCount = loJsonResponse.IntOf("TableDescription.ItemCount")
lnTableDescriptionProvisionedThroughputNumberOfDecreasesToday = loJsonResponse.IntOf("TableDescription.ProvisionedThroughput.NumberOfDecreasesToday")
lnTableDescriptionProvisionedThroughputReadCapacityUnits = loJsonResponse.IntOf("TableDescription.ProvisionedThroughput.ReadCapacityUnits")
lnTableDescriptionProvisionedThroughputWriteCapacityUnits = loJsonResponse.IntOf("TableDescription.ProvisionedThroughput.WriteCapacityUnits")
lcTableDescriptionTableName = loJsonResponse.StringOf("TableDescription.TableName")
lnTableDescriptionTableSizeBytes = loJsonResponse.IntOf("TableDescription.TableSizeBytes")
lcTableDescriptionTableStatus = loJsonResponse.StringOf("TableDescription.TableStatus")
i = 0
lnCount_i = loJsonResponse.SizeOfArray("TableDescription.AttributeDefinitions")
do while i < lnCount_i
    loJsonResponse.I = i
    lcAttributeName = loJsonResponse.StringOf("TableDescription.AttributeDefinitions[i].AttributeName")
    lcAttributeType = loJsonResponse.StringOf("TableDescription.AttributeDefinitions[i].AttributeType")
    i = i + 1
enddo
i = 0
lnCount_i = loJsonResponse.SizeOfArray("TableDescription.KeySchema")
do while i < lnCount_i
    loJsonResponse.I = i
    lcAttributeName = loJsonResponse.StringOf("TableDescription.KeySchema[i].AttributeName")
    lcKeyType = loJsonResponse.StringOf("TableDescription.KeySchema[i].KeyType")
    i = i + 1
enddo
i = 0
lnCount_i = loJsonResponse.SizeOfArray("TableDescription.LocalSecondaryIndexes")
do while i < lnCount_i
    loJsonResponse.I = i
    lcIndexArn = loJsonResponse.StringOf("TableDescription.LocalSecondaryIndexes[i].IndexArn")
    lcIndexName = loJsonResponse.StringOf("TableDescription.LocalSecondaryIndexes[i].IndexName")
    lnIndexSizeBytes = loJsonResponse.IntOf("TableDescription.LocalSecondaryIndexes[i].IndexSizeBytes")
    lnItemCount = loJsonResponse.IntOf("TableDescription.LocalSecondaryIndexes[i].ItemCount")
    lcProjectionProjectionType = loJsonResponse.StringOf("TableDescription.LocalSecondaryIndexes[i].Projection.ProjectionType")
    j = 0
    lnCount_j = loJsonResponse.SizeOfArray("TableDescription.LocalSecondaryIndexes[i].KeySchema")
    do while j < lnCount_j
        loJsonResponse.J = j
        lcAttributeName = loJsonResponse.StringOf("TableDescription.LocalSecondaryIndexes[i].KeySchema[j].AttributeName")
        lcKeyType = loJsonResponse.StringOf("TableDescription.LocalSecondaryIndexes[i].KeySchema[j].KeyType")
        j = j + 1
    enddo
    i = i + 1
enddo


release loRest
release loAuthAws
release loJson
release loSbRequestBody
release loSbResponseBody
release loJsonResponse