1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

aws-sdk-goでDynamoDBを取り扱うサンプル

Last updated at Posted at 2022-08-21

テーブルのセット

image.png

この3つの項目で構成する

  • userid number
  • priolity number
  • description string

PUT

package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
)

func main() {
	ddb := dynamodb.New(session.New(), aws.NewConfig().WithRegion("ap-northeast-1"))

	param1 := &dynamodb.PutItemInput{
		TableName: aws.String("testdynamo"),
		Item: map[string]*dynamodb.AttributeValue{
			"userid": {
				N: aws.String("2"), //number
			},
			"priolity": {
				N: aws.String("100"), //number
			},
			"description": {
				S: aws.String("aiueo"), //string
			},
		},
	}

	result, err := ddb.PutItem(param1)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(result)
}

GET

package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
)

func main() {
	ddb := dynamodb.New(session.New(), aws.NewConfig().WithRegion("ap-northeast-1"))

	input := &dynamodb.GetItemInput{
		TableName: aws.String("testdynamo"),

		Key: map[string]*dynamodb.AttributeValue{
			"userid": {
				N: aws.String("2"),
			},
		},
		AttributesToGet: []*string{
			aws.String("userid"),
			aws.String("description"),
		},
		ConsistentRead: aws.Bool(true),

		ReturnConsumedCapacity: aws.String("NONE"),
	}

	resp, err := ddb.GetItem(input)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(*resp.Item["userid"].N, *resp.Item["description"].S)
}

Query

package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
)

func main() {
	ddb := dynamodb.New(session.New(), aws.NewConfig().WithRegion("ap-northeast-1"))

	input := &dynamodb.QueryInput{
		TableName: aws.String("testdynamo"),
		ExpressionAttributeNames: map[string]*string{
			"#userid": aws.String("userid"),
		},
		ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
			":userid": {
				N: aws.String("2"),
			},
		},
		KeyConditionExpression: aws.String("#userid = :userid"),
	}

	result, err := ddb.Query(input)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(result)
}

scan

  • FilterExpression: でフィルタができる
  • 少量のデータ出れば、scanして手元でいじったほうが早そうではある
package main

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
)

func main() {
	ddb := dynamodb.New(session.New(), aws.NewConfig().WithRegion("ap-northeast-1"))

	input := &dynamodb.ScanInput{
		TableName: aws.String("testdynamo2"),
		ExpressionAttributeNames: map[string]*string{
			"#userid":   aws.String("userid"),
			"#priolity": aws.String("priolity"),
		},
		ProjectionExpression: aws.String("#userid, #priolity"),
	}

	result, err := ddb.Scan(input)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(result)
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?