dynamodb

package
v0.0.0-...-b425644 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

DynamoDB

Usage

package main

import (
	"log"
	"os"
	"strings"

	"github.com/dynport/gocloud/aws"
	"github.com/dynport/gocloud/aws/dynamodb"
)

var tableName = os.Getenv("TABLE_NAME")

func run() error {
	client := aws.NewFromEnv()

	// list tables
	header("ListTables")
	listTabels := &dynamodb.ListTables{}
	if rsp, e := listTabels.Execute(client); e != nil {
		return e
	} else {
		log.Printf("tables: %v", rsp.TableNames)
	}

	// describe table
	header("DescribeTable")
	describeTable := &dynamodb.DescribeTable{TableName: tableName}
	if rsp, e := describeTable.Execute(client); e != nil {
		return e
	} else if rsp.Table != nil {
		log.Printf("name: %q", rsp.Table.TableName)
		log.Printf("status: %q", rsp.Table.TableStatus)
		log.Printf("item count: %d", rsp.Table.ItemCount)
		log.Printf("size in bytes: %d", rsp.Table.TableSizesBytes)
		if rsp.Table.ProvisionedThroughput != nil {
			log.Printf("read: %d", rsp.Table.ProvisionedThroughput.ReadCapacityUnits)
			log.Printf("write: %d", rsp.Table.ProvisionedThroughput.WriteCapacityUnits)
			log.Printf("decreases: %d", rsp.Table.ProvisionedThroughput.NumberOfDecreasesToday)
		}
	}

	// put item
	header("PutItem")
	put := &dynamodb.PutItem{
		TableName: tableName,
		Item: dynamodb.Item{
			"Key":   {S: "hello"},
			"Value": {S: "world"},
		},
		ReturnConsumedCapacity: "TOTAL",
	}

	if _, e := put.Execute(client); e != nil {
		return e
	} else {
		log.Printf("put item!")
	}

	// get item
	header("GetItem")
	item := &dynamodb.Item{"Key": {S: "hello"}}
	getItem := &dynamodb.GetItem{
		TableName: tableName,
		Key:       item,
		ReturnConsumedCapacity: "TOTAL",
		ConsistentRead:         "true",
	}

	if rsp, e := getItem.Execute(client); e != nil {
		return e
	} else {
		if rsp.ConsumedCapacity != nil {
			log.Printf("consumed capacity: %.1f", rsp.ConsumedCapacity.CapacityUnits)
		}
		for k, v := range rsp.Item {
			log.Printf("%s: %#v", k, v)

		}
	}

	// scan
	header("Scan")
	scan := &dynamodb.Scan{
		TableName: tableName,
		Limit:     10,
		ReturnConsumedCapacity: "TOTAL",
	}
	if sr, e := scan.Execute(client); e != nil {
		return e
	} else {
		log.Printf("Count: %d", sr.Count)
		if sr.ConsumedCapacity != nil {
			log.Printf("ConsumedCapacity: %.1f", sr.ConsumedCapacity.CapacityUnits)
		}
		for _, i := range sr.Items {
			for k, v := range i {
				log.Printf("%s: %#v", k, v)

			}
		}
	}
	return nil
}

func main() {
	log.SetFlags(0)
	e := run()
	if e != nil {
		log.Printf("ERROR: %q", e.Error())
		log.Fatal(e)
	}
}

func header(message string) {
	log.Printf("%s %s %s", strings.Repeat("*", 50), message, strings.Repeat("*", 50))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiError

type ApiError struct {
	Type    string `json:"__type"`
	Message string `json:"message"`
}

type AttributeDefinition

type AttributeDefinition struct {
	AttributeType string
	AtributeName  string
}

type AttributeUpdate

type AttributeUpdate struct {
	Action string          `json:"Action"`
	Value  *ItemDefinition `json:"ItemDefinition,omitempty"`
}

type ConsumedCapacity

type ConsumedCapacity struct {
	CapacityUnits float64 `json:"CapacityUnits,omitempty"`
	TableName     string  `json:"TableName,omitempty"`
}

type DescribeTable

type DescribeTable struct {
	TableName string
}

func (*DescribeTable) Execute

func (d *DescribeTable) Execute(client *aws.Client) (*DescribeTableResponse, error)

type DescribeTableResponse

type DescribeTableResponse struct {
	Table *Table `json:"Table,omitempty"`
}

type Expected

type Expected struct {
	Exists string          `json:"Exists,omitempty"`
	Value  *ItemDefinition `json:"Value,omitempty"`
}

type GetItem

type GetItem struct {
	AttributesToGet        []string `json:"AttributesToGet,omitempty"`
	ConsistentRead         string   `json:"ConsistentRead,omitempty"`
	Key                    *Item    `json:"Key,omitempty"`
	ReturnConsumedCapacity string   `json:"ReturnConsumedCapacity,omitempty"`
	TableName              string   `json:"TableName,omitempty"`
}

func (*GetItem) Execute

func (g *GetItem) Execute(client *aws.Client) (*GetItemResponse, error)

type GetItemResponse

type GetItemResponse struct {
	ConsumedCapacity *ConsumedCapacity `json:"ConsumedCapacity,omitempty"`
	Item             Item              `json:"Item,omitempty"`
}

type Item

type Item map[string]*ItemDefinition

type ItemDefinition

type ItemDefinition struct {
	S  string   `json:"S,omitempty"`
	SS []string `json:"SS,omitempty"`
	B  string   `json:"B,omitempty"`
	BS []string `json:"BS,omitempty"`
	N  string   `json:"N,omitempty"`
	NS []string `json:"NS,omitempty"`
}

type KeySchema

type KeySchema struct {
	KeyType       string
	AttributeName string
}

type ListTables

type ListTables struct {
	ExclusiveStartTableName string `json:"ExclusiveStartTableName,omitempty"`
	Limit                   int    `json:"Limit,omitempty"`
}

func (*ListTables) Execute

func (l *ListTables) Execute(client *aws.Client) (*ListTablesResponse, error)

type ListTablesResponse

type ListTablesResponse struct {
	LastEvaluatedTableName string   `json:"LastEvaluatedTableName,omitempty"`
	TableNames             []string `json:"TableNames,omitempty"`
}

type NullWriter

type NullWriter struct {
}

func (*NullWriter) Write

func (w *NullWriter) Write([]byte) (int, error)

type ProvisionedThroughput

type ProvisionedThroughput struct {
	WriteCapacityUnits     int
	ReadCapacityUnits      int
	NumberOfDecreasesToday int
}

type PutItem

type PutItem struct {
	TableName                   string               `json:"TableName,omitempty"`
	Expected                    map[string]*Expected `json:"Expected,omitempty"`
	Item                        Item                 `json:"Item,omitempty"`
	ReturnValues                string               `json:"ReturnValues,omitempty"`
	ReturnConsumedCapacity      string               `json:"ReturnConsumedCapacity,omitempty"`
	ReturnItemCollectionMetrics string               `json:"ReturnItemCollectionMetrics,omitempty"`
}

func (*PutItem) Execute

func (p *PutItem) Execute(client *aws.Client) (*PutItemResponse, error)

type PutItemResponse

type PutItemResponse struct{}

type Scan

type Scan struct {
	TableName              string                 `json:"TableName,omitempty"`
	TotalSegments          string                 `json:"TotalSegments,omitempty"`
	AttributesToGet        []string               `json:"AttributesToGet,omitempty"`
	ExclusiveStartKey      Item                   `json:"ExclusiveStartKey,omitempty"`
	Limit                  int                    `json:"Limit,omitempty"`
	ReturnConsumedCapacity string                 `json:"ReturnConsumedCapacity,omitempty"` // INDEXES, TOTAL or NONE
	ScanFilter             map[string]*ScanFilter `json:"ScanFilter,omitempty"`
	Segment                string                 `json:"Segment,omitempty"`
	Select                 string                 `json:"Select,omitempty"`
}

func (*Scan) Execute

func (s *Scan) Execute(client *aws.Client) (*ScanResponse, error)

type ScanFilter

type ScanFilter struct {
	AttributeValueList []*ItemDefinition `json:"AttributeValueList,omitempty"`
	ComparisonOperator string            `json:"parisonOperator,omitempty"`
}

type ScanResponse

type ScanResponse struct {
	ConsumedCapacity *ConsumedCapacity
	Count            int    `json:"Count,omitempty"`
	ScannedCount     int    `json:"ScannedCount,omitempty"`
	Items            []Item `json:"Items,omitempty"`
	LastEvaluatedKey Item   `json:"tEvaluatedKey,omitempty"`
}

type Table

type Table struct {
	TableStatus           string                 `json:"TableStatus,omitempty"`
	TableSizesBytes       int                    `json:"TableSizesBytes,omitempty"`
	TableName             string                 `json:"TableName,omitempty"`
	ProvisionedThroughput *ProvisionedThroughput `json:"ProvisionedThroughput,omitempty"`
	KeySchema             []*KeySchema           `json:"KeySchema"`
	ItemCount             int                    `json:"ItemCount,omitempty"`
	CreationDateTime      float64                `json:"CreationDateTime,omitempty"`
	AttributeDefinitions  []*AttributeDefinition `json:"AttributeDefinitions,omitempty"`
}

type UpdateItem

type UpdateItem struct {
	AttributeUpdates            map[string]*AttributeUpdate `json:"AttributeUpdate"`
	Expected                    *Expected                   `json:"Expected"`
	Key                         *Item                       `json:"Key"`
	ReturnConsumedCapacity      string                      `json:"ReturnConsumedCapacity"`      // "string",
	ReturnItemCollectionMetrics string                      `json:"ReturnItemCollectionMetrics"` // "string",
	ReturnValues                string                      `json:"ReturnValues"`                // "string",
	TableName                   string                      `json:"TableName"`                   // "string"
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL