DynamoDB を aws_dynamo から触ってみた

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Java や Ruby, PHP の AWS SDK は公式から配布されていますが、C 言語向けの SDK はないため、有志でいろんな方がつくられているようです。それらの中から今回は C 言語から DynamoDB にアクセスするためのライブラリである aws_dynamo を触ってみました。

devicescape/aws_dynamo · GitHub

インストールと準備

まず、インストールですが、ビルドには autotools と libtool が、ライブラリには openssl と yajl がそれぞれ必要です。

$ brew install automake libtool openssl yajl
$ git clone https://github.com/devicescape/aws_dynamo.git
$ cd aws_dynamo
$ autoreconf -fiv
$ ./configure
$ make install

接続できるか確認するために今回はサーバとして dynamodb-local を使うことにします。(Java が必要ですが、インストール方法については他のサイトをご参考ください。)

$ brew install dynamodb-local
$ dynamodb-local

実際に aws_dynamo を使ったクライアントを用意します。具体的には aws_dynamo に同梱の examples を使用します。

$ cd aws_dynamo/examples
$ vi v2-exmaple.c

dynamodb-local に接続するためにコードを修正します。

diff --git a/examples/v2-example.c b/examples/v2-example.c
index 16bf540..96a766e 100644
--- a/examples/v2-example.c
+++ b/examples/v2-example.c
@@ -44,7 +44,10 @@ static void example_v2_query(struct aws_handle *aws)

int main(int argc, char *argv[])
{
- struct aws_handle *aws = aws_init(NULL, NULL);
+ struct aws_handle *aws = aws_init("", "");
+ aws_dynamo_set_endpoint(aws, "localhost", "local");
+ aws_dynamo_set_port(aws, 8000);
+ aws_dynamo_set_https(aws, 0);

if (aws == NULL) {
printf("Could not initialize.n");

実行

修正後にコンパイルし、実行してみます。

$ gcc v2-example.c -o v2-example.exe -laws_dynamo
$ ./v2-example.exe
DEBUG: aws_post: '{ "IndexName": "bssid_index", "ExpressionAttributeValues": { ":bssid":{"S":"001122334455"}}, "KeyConditionExpression": "bssid = :bssid", "TableName": "tdssp_bss"}'
DEBUG: aws_post response: '{"__type":"com.amazonaws.dynamodb.v20120810#ResourceNotFoundException","Message":"Cannot do operations on a non-existent table"}'
DEBUG: error_response_parser_start_map, enter state 0
DEBUG: error_response_parser_start_map exit 1
DEBUG: error_response_parser_map_key, val = __type, enter state 1
DEBUG: error_response_parser_map_key exit 2
DEBUG: error_response_parser_string, val = com.amazonaws.dynamodb.v20120810#ResourceNotFoundException, enter state 2
DEBUG: error_response_parser_string exit 1
DEBUG: error_response_parser_map_key, val = Message, enter state 1
DEBUG: error_response_parser_map_key exit 3
DEBUG: error_response_parser_string, val = Cannot do operations on a non-existent table, enter state 3
DEBUG: error_response_parser_string exit 1
DEBUG: error_response_parser_end_map enter 1
DEBUG: error_response_parser_end_map exit 0
WARN: aws_dynamo_request: Aborting request. http code = 400, target='DynamoDB_20120810.Query' body='{ "IndexName": "bssid_index", "ExpressionAttributeValues": { ":bssid":{"S":"001122334455"}}, "KeyConditionExpression": "bssid = :bssid", "TableName": "tdssp_bss"}' response='{"__type":"com.amazonaws.dynamodb.v20120810#ResourceNotFoundException","Message":"Cannot do operations on a non-existent table"}'
failure performnig query

このサンプルは存在しないテーブルにアクセスするようになっているため、その旨のエラーが表示されますが、dynamodb-local 側にもログが出るため、アクセスできていることがわかります。

$ dynamodb-local
7 07, 2015 1:41:25 午後 com.almworks.sqlite4java.Internal log
情報: [sqlite] DB[1]: instantiated [_local.db]
7 07, 2015 1:41:25 午後 com.almworks.sqlite4java.Internal log
情報: [sqlite] Internal: loaded sqlite4java-osx from /usr/local/Cellar/dynamodb-local/2015-04-27_1.0/libexec/DynamoDBLocal_lib/libsqlite4java-osx.dylib
7 07, 2015 1:41:25 午後 com.almworks.sqlite4java.Internal log
情報: [sqlite] Internal: loaded sqlite 3.8.7, wrapper 1.3
7 07, 2015 1:41:25 午後 com.almworks.sqlite4java.Internal log
情報: [sqlite] DB[1]: opened

まとめ

実際に C 言語で aws を利用することは稀だとは思いますが、他にも S3 にアクセスできる libs3 などもありますので、興味のある方は見てみてはいかがでしょうか?