Created
November 1, 2023 09:34
-
-
Save mortymacs/6cc0dfc258d6035bfc2f896c0d61a20c to your computer and use it in GitHub Desktop.
DynamoDB sample Go code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | |
"github.com/aws/aws-sdk-go/service/dynamodb/expression" | |
"github.com/k0kubun/pp/v3" | |
) | |
const ( | |
Profile = "mort" | |
Region = "us-east-1" | |
TableName = "users" | |
) | |
type Input struct { | |
ID string | |
Username string | |
Metadata struct { | |
Name string | |
TradeName string | |
} | |
} | |
func main() { | |
sess, err := session.NewSessionWithOptions(session.Options{ | |
Profile: Profile, | |
Config: aws.Config{ | |
Region: aws.String(Region), // Replace with your desired AWS region | |
// Endpoint: aws.String("http://127.0.0.1:4566"), // Replace with your desired endpoint URL | |
}, | |
}) | |
if err != nil { | |
log.Fatalf("unable to load SDK config, %v", err) | |
} | |
svc := dynamodb.New(sess) | |
// ------------------------- Get list of tables. | |
inp := &dynamodb.ListTablesInput{} | |
resp, err := svc.ListTables(inp) | |
if err != nil { | |
log.Fatalf("failed to list tables, %v", err) | |
} | |
fmt.Println("Tables:") | |
for _, tableName := range resp.TableNames { | |
fmt.Printf("-- %s\n", *tableName) | |
} | |
// ------------------------- Insert a new item | |
newItem := Input{ | |
ID: "12345", | |
Username: "Mortymacs", | |
Metadata: struct { | |
Name string | |
TradeName string | |
}{ | |
Name: "Mort", | |
TradeName: "mort.na", | |
}, | |
} | |
inputData, err := dynamodbattribute.MarshalMap(newItem) | |
if err != nil { | |
log.Fatalf("Input serialization failed: %v", err) | |
} | |
item := &dynamodb.PutItemInput{ | |
Item: inputData, | |
TableName: aws.String(TableName), | |
} | |
_, err = svc.PutItem(item) | |
if err != nil { | |
log.Fatalf("putting item got failed: %v", err) | |
} | |
// ------------------------- Get an item | |
result, err := svc.GetItem(&dynamodb.GetItemInput{ | |
TableName: aws.String(TableName), | |
Key: map[string]*dynamodb.AttributeValue{ | |
"Username": { | |
S: aws.String("Mortymacs"), | |
}, | |
}, | |
}) | |
if err != nil { | |
log.Fatalf("getting item got failed:", err) | |
} | |
if result.Item == nil { | |
log.Fatalf("nothing found.") | |
} | |
resultItem := &Input{} | |
if err = dynamodbattribute.UnmarshalMap(result.Item, resultItem); err != nil { | |
log.Fatalf("Unmarshaling the result has failed.") | |
} | |
pp.Print(resultItem) | |
// ------------------------- Filter items | |
filter := expression.Name("Metadata.TradeName").Equal(expression.Value("mort.na")) | |
fields := expression.NamesList(expression.Name("ID"), expression.Name("Username")) | |
expr, err := expression.NewBuilder().WithFilter(filter).WithProjection(fields).Build() | |
if err != nil { | |
log.Fatalf("building expression got failed:", err) | |
} | |
params := &dynamodb.ScanInput{ | |
ExpressionAttributeNames: expr.Names(), | |
ExpressionAttributeValues: expr.Values(), | |
FilterExpression: expr.Filter(), | |
ProjectionExpression: expr.Projection(), | |
TableName: aws.String(TableName), | |
} | |
resultItems, err := svc.Scan(params) | |
if err != nil { | |
log.Fatalf("scanning got failed: %v", err) | |
} | |
pp.Print(resultItems) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment