テーブルのセット
この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)
}