PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"
Procedure ChilkatExample()
success.i = 0
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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
authAws.i = CkAuthAws::ckCreate()
If authAws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
CkAuthAws::setCkSecretKey(authAws, "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.)
CkAuthAws::setCkRegion(authAws, "us-west-2")
CkAuthAws::setCkServiceName(authAws, "dynamodb")
; SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
CkRest::ckSetAuthAws(rest,authAws)
; URL: https://dynamodb.us-west-2.amazonaws.com/
bTls.i = 1
port.i = 443
bAutoReconnect.i = 1
; Don't forget to change the region domain (us-west-2.amazonaws.com) to your particular region.
success = CkRest::ckConnect(rest,"dynamodb.us-west-2.amazonaws.com",port,bTls,bAutoReconnect)
If success <> 1
Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
ProcedureReturn
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"
; }
; ]
; }
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
CkJsonObject::setCkI(json, i)
CkJsonObject::ckUpdateString(json,"AttributeDefinitions[i].AttributeName","ForumName")
CkJsonObject::ckUpdateString(json,"AttributeDefinitions[i].AttributeType","S")
i = i + 1
CkJsonObject::setCkI(json, i)
CkJsonObject::ckUpdateString(json,"AttributeDefinitions[i].AttributeName","Subject")
CkJsonObject::ckUpdateString(json,"AttributeDefinitions[i].AttributeType","S")
i = i + 1
CkJsonObject::setCkI(json, i)
CkJsonObject::ckUpdateString(json,"AttributeDefinitions[i].AttributeName","LastPostDateTime")
CkJsonObject::ckUpdateString(json,"AttributeDefinitions[i].AttributeType","S")
CkJsonObject::ckUpdateString(json,"TableName","Thread")
i = 0
CkJsonObject::setCkI(json, i)
CkJsonObject::ckUpdateString(json,"KeySchema[i].AttributeName","ForumName")
CkJsonObject::ckUpdateString(json,"KeySchema[i].KeyType","HASH")
i = i + 1
CkJsonObject::setCkI(json, i)
CkJsonObject::ckUpdateString(json,"KeySchema[i].AttributeName","Subject")
CkJsonObject::ckUpdateString(json,"KeySchema[i].KeyType","RANGE")
i = 0
CkJsonObject::setCkI(json, i)
j.i = 0
CkJsonObject::setCkJ(json, j)
CkJsonObject::ckUpdateString(json,"LocalSecondaryIndexes[i].IndexName","LastPostIndex")
CkJsonObject::ckUpdateString(json,"LocalSecondaryIndexes[i].KeySchema[j].AttributeName","ForumName")
CkJsonObject::ckUpdateString(json,"LocalSecondaryIndexes[i].KeySchema[j].KeyType","HASH")
j = j + 1
CkJsonObject::setCkJ(json, j)
CkJsonObject::ckUpdateString(json,"LocalSecondaryIndexes[i].KeySchema[j].AttributeName","LastPostDateTime")
CkJsonObject::ckUpdateString(json,"LocalSecondaryIndexes[i].KeySchema[j].KeyType","RANGE")
CkJsonObject::ckUpdateString(json,"LocalSecondaryIndexes[i].Projection.ProjectionType","KEYS_ONLY")
CkJsonObject::ckUpdateInt(json,"ProvisionedThroughput.ReadCapacityUnits",5)
CkJsonObject::ckUpdateInt(json,"ProvisionedThroughput.WriteCapacityUnits",5)
CkJsonObject::ckUpdateString(json,"Tags[0].Key","Owner")
CkJsonObject::ckUpdateString(json,"Tags[0].Value","BlueTeam")
CkRest::ckAddHeader(rest,"Content-Type","application/x-amz-json-1.0")
CkRest::ckAddHeader(rest,"X-Amz-Target","DynamoDB_20120810.CreateTable")
CkRest::ckAddHeader(rest,"Accept-Encoding","identity")
sbRequestBody.i = CkStringBuilder::ckCreate()
If sbRequestBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckEmitSb(json,sbRequestBody)
sbResponseBody.i = CkStringBuilder::ckCreate()
If sbResponseBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkRest::ckFullRequestSb(rest,"POST","/",sbRequestBody,sbResponseBody)
If success <> 1
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
ProcedureReturn
EndIf
respStatusCode.i = CkRest::ckResponseStatusCode(rest)
Debug "response status code = " + Str(respStatusCode)
If respStatusCode >= 400
Debug "Response Status Code = " + Str(respStatusCode)
Debug "Response Header:"
Debug CkRest::ckResponseHeader(rest)
Debug "Response Body:"
Debug CkStringBuilder::ckGetAsString(sbResponseBody)
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
ProcedureReturn
EndIf
jsonResponse.i = CkJsonObject::ckCreate()
If jsonResponse.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)
CkJsonObject::setCkEmitCompact(jsonResponse, 0)
Debug CkJsonObject::ckEmit(jsonResponse)
; 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
AttributeName.s
AttributeType.s
KeyType.s
IndexArn.s
IndexName.s
IndexSizeBytes.i
ItemCount.i
ProjectionProjectionType.s
count_j.i
TableDescriptionTableArn.s = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.TableArn")
TableDescriptionCreationDateTime.s = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.CreationDateTime")
TableDescriptionItemCount.i = CkJsonObject::ckIntOf(jsonResponse,"TableDescription.ItemCount")
TableDescriptionProvisionedThroughputNumberOfDecreasesToday.i = CkJsonObject::ckIntOf(jsonResponse,"TableDescription.ProvisionedThroughput.NumberOfDecreasesToday")
TableDescriptionProvisionedThroughputReadCapacityUnits.i = CkJsonObject::ckIntOf(jsonResponse,"TableDescription.ProvisionedThroughput.ReadCapacityUnits")
TableDescriptionProvisionedThroughputWriteCapacityUnits.i = CkJsonObject::ckIntOf(jsonResponse,"TableDescription.ProvisionedThroughput.WriteCapacityUnits")
TableDescriptionTableName.s = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.TableName")
TableDescriptionTableSizeBytes.i = CkJsonObject::ckIntOf(jsonResponse,"TableDescription.TableSizeBytes")
TableDescriptionTableStatus.s = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.TableStatus")
i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jsonResponse,"TableDescription.AttributeDefinitions")
While i < count_i
CkJsonObject::setCkI(jsonResponse, i)
AttributeName = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.AttributeDefinitions[i].AttributeName")
AttributeType = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.AttributeDefinitions[i].AttributeType")
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"TableDescription.KeySchema")
While i < count_i
CkJsonObject::setCkI(jsonResponse, i)
AttributeName = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.KeySchema[i].AttributeName")
KeyType = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.KeySchema[i].KeyType")
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jsonResponse,"TableDescription.LocalSecondaryIndexes")
While i < count_i
CkJsonObject::setCkI(jsonResponse, i)
IndexArn = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].IndexArn")
IndexName = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].IndexName")
IndexSizeBytes = CkJsonObject::ckIntOf(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].IndexSizeBytes")
ItemCount = CkJsonObject::ckIntOf(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].ItemCount")
ProjectionProjectionType = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].Projection.ProjectionType")
j = 0
count_j = CkJsonObject::ckSizeOfArray(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].KeySchema")
While j < count_j
CkJsonObject::setCkJ(jsonResponse, j)
AttributeName = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].KeySchema[j].AttributeName")
KeyType = CkJsonObject::ckStringOf(jsonResponse,"TableDescription.LocalSecondaryIndexes[i].KeySchema[j].KeyType")
j = j + 1
Wend
i = i + 1
Wend
CkRest::ckDispose(rest)
CkAuthAws::ckDispose(authAws)
CkJsonObject::ckDispose(json)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jsonResponse)
ProcedureReturn
EndProcedure