実行してみます。
{“document”=>{“host”=>”127.0.0.1”, “path”=>”/”, “method”=>”GET”, “referer”=>”-“, “code”=>”200”, “user”=>”-“, “agent”=>”curl/7.35.0”, “size”=>”11764”}}
ドキュメントで返ってきたのがわかります。
2.ドキュメントの一部を読み込み
ドキュメントの一部を取得してみます。
1#!/usr/bin/env ruby23# coding: utf-845require 'aws-sdk'6require 'json'78dynamo_db = Aws::DynamoDB::Client.new(9 :region => "ap-northeast-1",10 :access_key_id => "xxxxxxxxxxxxxxxxxxxxxxxxxxx",11 :secret_access_key => "xxxxxxxxxxxxxxxxxxxxxxxxxxx"12)1314resp = dynamo_db.get_item(15 table_name: 'devknowcom',16 key: {17 'host' => '127.0.0.1',18 },19 projection_expression: "document.code",20 consistent_read: true21)2223puts resp.item.to_s一部だけ取得できているのがわかります。
1{"document"=>{"code"=>"200"}}3.アイテムのアップデート
アイテムのアップデートを試してみます。
pathに「index.html」を付けてみます。
1#!/usr/bin/env ruby23# coding: utf-845require 'aws-sdk'6require 'json'78dynamo_db = Aws::DynamoDB::Client.new(9 :region => "ap-northeast-1",10 :access_key_id => "xxxxxxxxxxxxxxxxxxxxxxxxxxx",11 :secret_access_key => "xxxxxxxxxxxxxxxxxxxxxxxxxxx"12)1314dynamo_db.update_item(15 table_name: 'devknowcom',16 key: {17 'host' => '127.0.0.1',18 },19 attribute_updates: {20 "document" => {21 value: {22 "host"=>"127.0.0.1",23 "path"=>"/index.html",24 "method"=>"GET",25 "referer"=>"-",26 "code"=>"200",27 "user"=>"-",28 "agent"=>"curl/7.35.0",29 "size"=>"11764"30 },31 action: "PUT"32 }33 }34)3536resp = dynamo_db.get_item(37 table_name: 'devknowcom',38 key: {39 'host' => '127.0.0.1',40 },41 attributes_to_get: ["document"],42 consistent_read: true43)4445puts resp.item.to_s更新されているのが分かります。
1{"document"=>{"host"=>"127.0.0.1", "path"=>"/index.html", "method"=>"GET", "referer"=>"-", "code"=>"200", "user"=>"-", "agent"=>"curl/7.35.0", "size"=>"11764"}}いかがでしたでしょうか?
次回もお楽しみに!!!