Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const rest = try chilkat.Rest.init();
defer rest.deinit();
// 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
const auth_aws = try chilkat.AuthAws.init();
defer auth_aws.deinit();
auth_aws.setAccessKey("AWS_ACCESS_KEY");
auth_aws.setSecretKey("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.)
auth_aws.setRegion("us-west-2");
auth_aws.setServiceName("dynamodb");
// SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
rest.setAuthAws(auth_aws) catch {};
// URL: https://dynamodb.us-west-2.amazonaws.com/
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
// Don't forget to change the region domain (us-west-2.amazonaws.com) to your particular region.
rest.connect("dynamodb.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect) catch {
std.debug.print("ConnectFailReason: {d}\n", .{rest.getConnectFailReason()});
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// 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"
// }
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
var i: i32 = 0;
json.setI(i);
json.updateString("AttributeDefinitions[i].AttributeName", "ForumName") catch {};
json.updateString("AttributeDefinitions[i].AttributeType", "S") catch {};
i = i + 1;
json.setI(i);
json.updateString("AttributeDefinitions[i].AttributeName", "Subject") catch {};
json.updateString("AttributeDefinitions[i].AttributeType", "S") catch {};
i = i + 1;
json.setI(i);
json.updateString("AttributeDefinitions[i].AttributeName", "LastPostDateTime") catch {};
json.updateString("AttributeDefinitions[i].AttributeType", "S") catch {};
json.updateString("TableName", "Thread") catch {};
i = 0;
json.setI(i);
json.updateString("KeySchema[i].AttributeName", "ForumName") catch {};
json.updateString("KeySchema[i].KeyType", "HASH") catch {};
i = i + 1;
json.setI(i);
json.updateString("KeySchema[i].AttributeName", "Subject") catch {};
json.updateString("KeySchema[i].KeyType", "RANGE") catch {};
i = 0;
json.setI(i);
var j: i32 = 0;
json.setJ(j);
json.updateString("LocalSecondaryIndexes[i].IndexName", "LastPostIndex") catch {};
json.updateString("LocalSecondaryIndexes[i].KeySchema[j].AttributeName", "ForumName") catch {};
json.updateString("LocalSecondaryIndexes[i].KeySchema[j].KeyType", "HASH") catch {};
j = j + 1;
json.setJ(j);
json.updateString("LocalSecondaryIndexes[i].KeySchema[j].AttributeName", "LastPostDateTime") catch {};
json.updateString("LocalSecondaryIndexes[i].KeySchema[j].KeyType", "RANGE") catch {};
json.updateString("LocalSecondaryIndexes[i].Projection.ProjectionType", "KEYS_ONLY") catch {};
json.updateInt("ProvisionedThroughput.ReadCapacityUnits", 5) catch {};
json.updateInt("ProvisionedThroughput.WriteCapacityUnits", 5) catch {};
json.updateString("Tags[0].Key", "Owner") catch {};
json.updateString("Tags[0].Value", "BlueTeam") catch {};
rest.addHeader("Content-Type", "application/x-amz-json-1.0") catch {};
rest.addHeader("X-Amz-Target", "DynamoDB_20120810.CreateTable") catch {};
rest.addHeader("Accept-Encoding", "identity") catch {};
const sb_request_body = try chilkat.StringBuilder.init();
defer sb_request_body.deinit();
json.emitSb(sb_request_body) catch {};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestSb("POST", "/", sb_request_body, sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const resp_status_code = rest.getResponseStatusCode();
std.debug.print("response status code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
return;
}
const json_response = try chilkat.JsonObject.init();
defer json_response.deinit();
json_response.loadSb(sb_response_body) catch {};
json_response.setEmitCompact(false);
std.debug.print("{s}\n", .{try json_response.emit(alloc)});
// 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
var attribute_name: [:0]const u8 = "";
var attribute_type: [:0]const u8 = "";
var key_type: [:0]const u8 = "";
var index_arn: [:0]const u8 = "";
var index_name: [:0]const u8 = "";
var index_size_bytes: i32 = 0;
var item_count: i32 = 0;
var projection_projection_type: [:0]const u8 = "";
var count_j: i32 = 0;
_ = try json_response.stringOf(alloc, "TableDescription.TableArn");
_ = try json_response.stringOf(alloc, "TableDescription.CreationDateTime");
_ = json_response.intOf("TableDescription.ItemCount");
_ = json_response.intOf("TableDescription.ProvisionedThroughput.NumberOfDecreasesToday");
_ = json_response.intOf("TableDescription.ProvisionedThroughput.ReadCapacityUnits");
_ = json_response.intOf("TableDescription.ProvisionedThroughput.WriteCapacityUnits");
_ = try json_response.stringOf(alloc, "TableDescription.TableName");
_ = json_response.intOf("TableDescription.TableSizeBytes");
_ = try json_response.stringOf(alloc, "TableDescription.TableStatus");
i = 0;
var count_i: i32 = json_response.sizeOfArray("TableDescription.AttributeDefinitions");
while (i < count_i) {
json_response.setI(i);
attribute_name = try json_response.stringOf(alloc, "TableDescription.AttributeDefinitions[i].AttributeName");
attribute_type = try json_response.stringOf(alloc, "TableDescription.AttributeDefinitions[i].AttributeType");
i = i + 1;
}
i = 0;
count_i = json_response.sizeOfArray("TableDescription.KeySchema");
while (i < count_i) {
json_response.setI(i);
attribute_name = try json_response.stringOf(alloc, "TableDescription.KeySchema[i].AttributeName");
key_type = try json_response.stringOf(alloc, "TableDescription.KeySchema[i].KeyType");
i = i + 1;
}
i = 0;
count_i = json_response.sizeOfArray("TableDescription.LocalSecondaryIndexes");
while (i < count_i) {
json_response.setI(i);
index_arn = try json_response.stringOf(alloc, "TableDescription.LocalSecondaryIndexes[i].IndexArn");
index_name = try json_response.stringOf(alloc, "TableDescription.LocalSecondaryIndexes[i].IndexName");
index_size_bytes = json_response.intOf("TableDescription.LocalSecondaryIndexes[i].IndexSizeBytes");
item_count = json_response.intOf("TableDescription.LocalSecondaryIndexes[i].ItemCount");
projection_projection_type = try json_response.stringOf(alloc, "TableDescription.LocalSecondaryIndexes[i].Projection.ProjectionType");
j = 0;
count_j = json_response.sizeOfArray("TableDescription.LocalSecondaryIndexes[i].KeySchema");
while (j < count_j) {
json_response.setJ(j);
attribute_name = try json_response.stringOf(alloc, "TableDescription.LocalSecondaryIndexes[i].KeySchema[j].AttributeName");
key_type = try json_response.stringOf(alloc, "TableDescription.LocalSecondaryIndexes[i].KeySchema[j].KeyType");
j = j + 1;
}
i = i + 1;
}
}