Пример #1
0
// PutItem puts an item on the table.
func (t *Table) PutItem(v interface{}, opts ...option.PutItemInput) error {
	req := &dynamodb.PutItemInput{
		TableName: t.Name,
	}

	var itemMapped map[string]*dynamodb.AttributeValue
	var err error
	if marshaller, ok := v.(item.Marshaler); ok {
		itemMapped, err = marshaller.MarshalItem()
	} else {
		itemMapped, err = dynamodbattribute.ConvertToMap(v)
	}
	if err != nil {
		return err
	}

	req.Item = itemMapped

	for _, f := range opts {
		f(req)
	}

	_, err = t.DynamoDB.PutItem(req)
	return err
}
func main() {
	svc := dynamodb.New(session.New(), &aws.Config{Region: aws.String("eu-central-1")})

	k := Key{
		Email: "*****@*****.**",
	}

	item, err := dynamodbattribute.ConvertToMap(k)
	if err != nil {
		panic(err)
	}

	result, err := svc.GetItem(&dynamodb.GetItemInput{
		TableName: aws.String("Users"),
		Key:       item,
	})
	if err != nil {
		panic(err)
	}

	r := Record{}
	err = dynamodbattribute.ConvertFromMap(result.Item, &r)

	fmt.Println(r)
}
Пример #3
0
func (i *TestItem) PrimaryKey() map[string]*dynamodb.AttributeValue {
	primaryKey := i.PrimaryKeyMap()

	item, _ := dynamodbattribute.ConvertToMap(primaryKey)

	return item
}
// Record marshals the job result into a dynamodb.AttributeValue struct, and writes
// the result item to DyanmoDB.
func (r *ResultRecorder) Record(result *wordfreq.JobResult) error {
	// Construct a result item representing what data we want to write to DynamoDB.
	recordItem := resultRecord{
		Filename: path.Join(result.Job.Bucket, result.Job.Key),
		Words:    map[string]int{},
	}
	for _, w := range result.Words {
		recordItem.Words[w.Word] = w.Count
	}

	// Use the ConvertToX helpers to marshal a Go struct to a dyanmodb.AttributeValue
	// type. This greatly simplifies the code needed to create the attribute
	// value item.
	av, err := dynamodbattribute.ConvertToMap(recordItem)
	if err != nil {
		return fmt.Errorf("unable to serialize result to dyanmoDB.AttributeValue, %v", err)
	}
	_, err = r.svc.PutItem(&dynamodb.PutItemInput{
		TableName: aws.String(r.tableName),
		Item:      av,
	})
	if err != nil {
		return fmt.Errorf("unable to record result, %v", err)
	}

	return nil
}
Пример #5
0
func BenchmarkPutItem(b *testing.B) {
	cfg := aws.Config{
		DisableSSL:  aws.Bool(true),
		Credentials: credentials.NewStaticCredentials("AKID", "SECRET", ""),
	}
	server := successRespServer([]byte(`{}`))
	cfg.Endpoint = aws.String(server.URL)

	svc := dynamodb.New(&cfg)
	svc.Handlers.Send.Clear()
	svc.Handlers.Send.PushBack(func(r *service.Request) {
		r.HTTPResponse = &http.Response{
			StatusCode: http.StatusOK,
			Status:     http.StatusText(http.StatusOK),
			Body:       noopBody,
		}
	})

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		av, err := dynamodbattribute.ConvertToMap(dbItem{Key: "MyKey", Data: "MyData"})
		if err != nil {
			b.Fatal("benchPutItem, expect no ConvertToMap errors", err)
		}
		params := &dynamodb.PutItemInput{
			Item:      av,
			TableName: aws.String("tablename"),
		}
		_, err = svc.PutItem(params)
		if err != nil {
			b.Error("benchPutItem, expect no request errors", err)
		}
	}
}
Пример #6
0
func benchPutItemParallel(p, c int, b *testing.B) {
	svc := dynamodb.New(&aws.Config{
		DisableSSL: aws.Bool(true),
	})

	av, err := dynamodbattribute.ConvertToMap(dbItem{Key: "MyKey", Data: "MyData"})
	if err != nil {
		b.Fatal("expect no ConvertToMap errors", err)
	}
	params := &dynamodb.PutItemInput{
		Item:      av,
		TableName: aws.String(testTableName),
	}
	b.N = c

	b.ResetTimer()
	b.SetParallelism(p)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			_, err = svc.PutItem(params)
			if err != nil {
				b.Error("expect no request errors", err)
			}
		}
	})
}
Пример #7
0
// output expects key value data to print to stdout
func (out stdout) output(data record) error {
	item, err := dynamodbattribute.ConvertToMap(data.Data)
	if err != nil {
		return err
	}
	fmt.Println(item)
	return nil
}
Пример #8
0
func main() {
	//testTableDelete()
	//chack table status

	//testTableCreate()
	//chack table status
	//if creating,wait for active

	svc := dynamodb.New(&aws.Config{Region: aws.String("ap-northeast-1")})
	/*
		scanParams := &dynamodb.ScanInput{
			TableName:aws.String("access_log_range"),
			AttributesToGet:[]*string{
				aws.String("id"),
				aws.String("time"),
				aws.String("body_bytes_sent"),
				aws.String("bytes_sent"),
				aws.String("forwardedfor"),
				aws.String("query_string"),
				aws.String("referer"),
				aws.String("remote_addr"),
				aws.String("request_length"),
				aws.String("request_method"),
				aws.String("request_time"),
				aws.String("request_uri"),
				aws.String("status"),
				aws.String("tag"),
				aws.String("useragent"),
			},
			//Limit: aws.Int64(1000000),
		}
	*/

	r := Record{
		Key:       "key127.0.0.1",
		RemoteID:  "abc-001",
		OtherData: map[string]int{"a": 1, "b": 2, "c": 3},
		Timestamp: time.Now().UTC().Unix(),
	}
	item, err := dynamodbattribute.ConvertToMap(r)
	log.Println(item)

	result, err := svc.PutItem(&dynamodb.PutItemInput{
		Item:      item,
		TableName: aws.String("result"),
	})

	fmt.Println(result, err)

	if err != nil {
		// Print the error, cast err to awserr.Error to get the Code and
		// Message from an error.
		fmt.Println(err.Error())
		return
	}
	fmt.Println(result)

}
Пример #9
0
// Register the service in the registry
func (c *Client) Register(name string, endpoint string) (*Service, error) {

	// Check whether the registry has been previously created. If not create before registration.
	if exists, err := c.Registry.Exists(); err != nil {
		return nil, err
	} else if !exists {
		if err := c.Registry.Create(); err != nil {
			return nil, err
		}
	}

	// Create Service
	service := &Service{Name: name, Endpoint: endpoint, stopHeartbeat: make(chan bool)}

	// Heartbeat function - updates expiry
	heartbeat := func() {
		// Update service Expiry based on TTL and current time
		service.Expiry = time.Now().Unix() + ServiceTTL

		// Update service entry in registry
		if av, err := dynamodbattribute.ConvertToMap(*service); err != nil {
			return
		} else {
			_, err := c.svc.PutItem(&dynamodb.PutItemInput{
				Item:      av,
				TableName: c.config.GetRegistryName(),
			})

			if err != nil {
				return
			}
		}
	}

	// Ensure call heartbeat at least once
	heartbeat()

	// Start goroutine to send heartbeat
	go func() {
		for {
			select {
			case <-service.stopHeartbeat:
				return
			default:
				// Pause for interval
				time.Sleep(HeartbeatInterval)

				// Call heartbeat function
				heartbeat()
			}
		}
	}()

	return service, nil
}
Пример #10
0
// tryToLock tries to create a new item in DynamoDB
// every `DynamoDBLockRetryInterval`. As long as the item
// cannot be created (because it already exists), it will
// be retried. If the operation fails due to an error, it
// is sent to the errors channel.
// When the lock could be acquired successfully, the success
// channel is closed.
func (l *DynamoDBLock) tryToLock(stop, success chan struct{}, errors chan error) {
	ticker := time.NewTicker(DynamoDBLockRetryInterval)

	record := DynamoDBRecord{
		Path:  recordPathForVaultKey(l.key),
		Key:   recordKeyForVaultKey(l.key),
		Value: []byte(l.value),
	}
	item, err := dynamodbattribute.ConvertToMap(record)
	if err != nil {
		errors <- err
		return
	}

	for {
		select {
		case <-stop:
			ticker.Stop()
		case <-ticker.C:
			_, err := l.backend.client.PutItem(&dynamodb.PutItemInput{
				TableName:           aws.String(l.backend.table),
				Item:                item,
				ConditionExpression: aws.String("attribute_not_exists(#p) or attribute_not_exists(#k)"),
				ExpressionAttributeNames: map[string]*string{
					"#p": aws.String("Path"),
					"#k": aws.String("Key"),
				},
			})
			if err != nil {
				if err, ok := err.(awserr.Error); ok && err.Code() != "ConditionalCheckFailedException" {
					errors <- err
				}
				if l.recovery {
					_, err := l.backend.client.DeleteItem(&dynamodb.DeleteItemInput{
						TableName: aws.String(l.backend.table),
						Key: map[string]*dynamodb.AttributeValue{
							"Path": {S: aws.String(record.Path)},
							"Key":  {S: aws.String(record.Key)},
						},
					})
					if err != nil {
						errors <- fmt.Errorf("could not delete lock record: %s", err)
					} else {
						l.recovery = false
					}
				}
			} else {
				ticker.Stop()
				close(success)
			}
		}
	}
}
Пример #11
0
func putResource(svc *dynamodb.DynamoDB, resource Resource, tableName string) error {
	item, err := dynamodbattribute.ConvertToMap(resource)
	if err != nil {
		return err
	}
	_, err = svc.PutItem(&dynamodb.PutItemInput{
		Item:      item,
		TableName: aws.String(tableName),
	})
	return err

}
Пример #12
0
func getDynamodbPutItemParams(b *testing.B) *dynamodb.PutItemInput {
	av, err := dynamodbattribute.ConvertToMap(struct {
		Key  string
		Data string
	}{Key: "MyKey", Data: "MyData"})
	if err != nil {
		b.Fatal("benchPutItem, expect no ConvertToMap errors", err)
	}
	return &dynamodb.PutItemInput{
		Item:      av,
		TableName: aws.String("tablename"),
	}
}
Пример #13
0
// output just writes to dynamodb
func (out dynamodbOut) output(data record) error {
	input := &dynamodb.PutItemInput{
		TableName: aws.String(out.table),
	}

	item, err := dynamodbattribute.ConvertToMap(data)
	if err != nil {
		return err
	}

	input.Item = item
	_, err = out.db.PutItem(input)
	return err
}
Пример #14
0
// Put is used to insert or update an entry
func (d *DynamoDBBackend) Put(entry *Entry) error {
	defer metrics.MeasureSince([]string{"dynamodb", "put"}, time.Now())

	record := DynamoDBRecord{
		Path:  recordPathForVaultKey(entry.Key),
		Key:   recordKeyForVaultKey(entry.Key),
		Value: entry.Value,
	}
	item, err := dynamodbattribute.ConvertToMap(record)
	if err != nil {
		return fmt.Errorf("could not convert prefix record to DynamoDB item: %s", err)
	}
	requests := []*dynamodb.WriteRequest{{
		PutRequest: &dynamodb.PutRequest{
			Item: item,
		},
	}}

	for _, prefix := range prefixes(entry.Key) {
		record = DynamoDBRecord{
			Path: recordPathForVaultKey(prefix),
			Key:  fmt.Sprintf("%s/", recordKeyForVaultKey(prefix)),
		}
		item, err := dynamodbattribute.ConvertToMap(record)
		if err != nil {
			return fmt.Errorf("could not convert prefix record to DynamoDB item: %s", err)
		}
		requests = append(requests, &dynamodb.WriteRequest{
			PutRequest: &dynamodb.PutRequest{
				Item: item,
			},
		})
	}

	return d.batchWriteRequests(requests)
}
Пример #15
0
// MarshalItem implements ItemMarshaler interface.
func (i TestItem) MarshalItem() (map[string]*dynamodb.AttributeValue, error) {
	if i.IsStartKey() {
		item := i.PrimaryKey()
		return item, nil
	}

	itemMapped, err := dynamodbattribute.ConvertToMap(i)
	if err != nil {
		return nil, err
	}
	if i.Password != "" {
		itemMapped["password"] = attributes.String(hashedPassword(i.Password))
	}

	return itemMapped, nil
}
Пример #16
0
// Query the registry for named service
func (c *Client) Discover(name string) (*Service, error) {

	// Make sure registry is active
	if active, _ := c.Registry.IsActive(); active == true {
		expressionAttributeValues := map[string]interface{}{
			":NameVal":   name,
			":ExpiryVal": time.Now().Unix(),
		}

		ean := map[string]*string{
			"#N": aws.String("Name"),
		}

		eav, err := dynamodbattribute.ConvertToMap(expressionAttributeValues)
		if err != nil {
			return nil, err
		}

		resp, err := c.svc.Query(&dynamodb.QueryInput{
			TableName:                 c.config.GetRegistryName(),
			KeyConditionExpression:    aws.String("#N = :NameVal"),
			FilterExpression:          aws.String("Expiry > :ExpiryVal"),
			ExpressionAttributeValues: eav,
			ExpressionAttributeNames:  ean,
		})

		if err != nil {
			return nil, err
		}

		if len(resp.Items) > 0 {
			// Randomly select one of the available endpoints (in effect load balancing between available endpoints)
			service := Service{}
			err = dynamodbattribute.ConvertFromMap(resp.Items[rand.Intn(len(resp.Items))], &service)
			if err != nil {
				return nil, err
			}
			return &service, nil
		} else {
			// No service found
			return nil, ErrServiceNotFound
		}
	} else {
		return nil, ErrRegistryNotActive
	}
}
func main() {

	svc := dynamodb.New(session.New(), &aws.Config{Endpoint: aws.String("http://localhost:8000"), Region: aws.String("us-east-1")})

	//var data map[string]interface{}

	file, e := ioutil.ReadFile("./mockdata/MOCK_DATA-gym.json")
	//fmt.Print(string(file))
	if e != nil {
		fmt.Printf("File error: %v\n", e)
		os.Exit(1)
	}

	var u []Gym
	json.Unmarshal(file, &u)

	for i := 0; i < len(u); i++ {
		fmt.Printf(" Name: %v\n", u[i].Name)

		item, err := dynamodbattribute.ConvertToMap(u[i])
		if err != nil {
			fmt.Println("Failed to convert", err)
			return
		}
		fmt.Printf("Item %v\n", item)

		//fmt.Printf("Data %v\n", data[user])
		params := &dynamodb.PutItemInput{
			Item:      item,
			TableName: aws.String("gym"),
		}
		resp, err := svc.PutItem(params)

		if err != nil {
			// Print the error, cast err to awserr.Error to get the Code and
			// Message from an error.
			fmt.Println(err.Error())
			return
		}
		// Pretty-print the response data.
		fmt.Println(resp)

	}
	//fmt.Printf("AL: Data %v", data)
}
func main() {

	svc := dynamodb.New(session.New(), &aws.Config{Endpoint: aws.String("http://localhost:8000"), Region: aws.String("us-east-1")})

	file, e := ioutil.ReadFile("./mockdata/MOCK_DATA-user-2.json")
	if e != nil {
		fmt.Printf("File error: %v\n", e)
		os.Exit(1)
	}

	var u []User
	json.Unmarshal(file, &u)

	for i := 0; i < len(u); i++ {
		fmt.Printf(" First Name: %v\n", u[i].FirstName)

		encPass, cryptErr := Crypt([]byte(u[i].Password))
		if cryptErr != nil {
			fmt.Println("Failed to hash password", cryptErr)
			return
		}
		u[i].EncPassword = string(encPass)
		u[i].Password = ""

		item, err := dynamodbattribute.ConvertToMap(u[i])
		if err != nil {
			fmt.Println("Failed to convert", err)
			return
		}
		fmt.Printf("Item %v\n", item)

		params := &dynamodb.PutItemInput{
			Item:      item,
			TableName: aws.String("user"),
		}
		resp, err := svc.PutItem(params)

		if err != nil {
			fmt.Println(err.Error())
			return
		}
		fmt.Println(resp)

	}
}
Пример #19
0
// PutItem puts an item on the table.
func (t *Table) PutItem(item interface{}, opts ...option.PutItemInput) error {
	req := &dynamodb.PutItemInput{
		TableName: t.Name,
	}

	itemMapped, err := dynamodbattribute.ConvertToMap(item)
	if err != nil {
		return err
	}

	req.Item = itemMapped

	for _, f := range opts {
		f(req)
	}

	_, err = t.DynamoDB.PutItem(req)
	return err
}
Пример #20
0
// ExclusiveStartKey sets an ExclusiveStartKey in dynamodb.QueryInput.
func ExclusiveStartKey(v interface{}) QueryInput {
	return func(req *dynamodb.QueryInput) error {
		var err error
		var esk map[string]*dynamodb.AttributeValue

		if key, ok := v.(map[string]*dynamodb.AttributeValue); ok {
			esk = key
		} else if marshaller, ok := v.(item.Marshaler); ok {
			esk, err = marshaller.MarshalItem()
		} else {
			esk, err = dynamodbattribute.ConvertToMap(v)
		}

		if err != nil {
			return err
		}

		req.ExclusiveStartKey = esk
		return nil
	}
}
func main() {
	svc := dynamodb.New(session.New(), &aws.Config{Region: aws.String("eu-central-1")})

	r := Record{
		Email:           "*****@*****.**",
		CheckEmailToken: "29jd9j2dm3932jdhewk4",
	}

	item, err := dynamodbattribute.ConvertToMap(r)
	if err != nil {
		panic(err)
	}
	result, err := svc.PutItem(&dynamodb.PutItemInput{
		TableName: aws.String("Users"),
		Item:      item,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}
func main() {

	svc := dynamodb.New(session.New(), &aws.Config{Endpoint: aws.String("http://localhost:8000"), Region: aws.String("us-east-1")})

	file, e := ioutil.ReadFile("./mockdata/MOCK_DATA-workout.json")
	if e != nil {
		fmt.Printf("File error: %v\n", e)
		os.Exit(1)
	}

	var u []Workout
	json.Unmarshal(file, &u)

	for i := 0; i < len(u); i++ {
		fmt.Printf("Name: %v\n", u[i].Name)

		item, err := dynamodbattribute.ConvertToMap(u[i])
		if err != nil {
			fmt.Println("Failed to convert", err)
			return
		}
		fmt.Printf("Item %v\n", item)

		params := &dynamodb.PutItemInput{
			Item:      item,
			TableName: aws.String("workout"),
		}
		resp, err := svc.PutItem(params)

		if err != nil {
			fmt.Println(err.Error())
			return
		}
		// Pretty-print the response data.
		fmt.Println(resp)

	}
}
Пример #23
0
// MarshalDynamoDB implements the dynamotree.Storable interface
func (a Account) MarshalDynamoDB() (map[string]*dynamodb.AttributeValue, error) {
	return dynamodbattribute.ConvertToMap(a)
}
Пример #24
0
func (a AccountT) MarshalDynamoDB() (map[string]*dynamodb.AttributeValue, error) {
	if a.MarshalFailPlease {
		return nil, fmt.Errorf("could not grob the frob")
	}
	return dynamodbattribute.ConvertToMap(a)
}
Пример #25
0
// MarshalDynamoDB implements the dynamotree.Storable interface
func (l Link) MarshalDynamoDB() (map[string]*dynamodb.AttributeValue, error) {
	return dynamodbattribute.ConvertToMap(l)
}
Пример #26
0
func realmain() error {
	flag.Parse()
	s := session.New(&aws.Config{Region: aws.String(*region)})
	ddb := dynamodb.New(s)

	tablename := aws.String(*table)

	describeTableOutput, err := ddb.DescribeTable(&dynamodb.DescribeTableInput{
		TableName: tablename,
	})
	if err != nil {
		return err
	}
	fmt.Printf("%#v\n", describeTableOutput)

	// if table not exists, create one.
	if describeTableOutput == nil {
		// create table should specify primary key.
		// on DynamoDB, not primary key fields are not necessary when creating table.
		createTableOutput, err := ddb.CreateTable(&dynamodb.CreateTableInput{
			AttributeDefinitions: []*dynamodb.AttributeDefinition{
				{
					AttributeName: aws.String("id"),
					AttributeType: aws.String("S"),
				},
			},
			KeySchema: []*dynamodb.KeySchemaElement{
				{
					AttributeName: aws.String("id"),
					KeyType:       aws.String("HASH"),
				},
			},
			ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
				ReadCapacityUnits:  aws.Int64(10),
				WriteCapacityUnits: aws.Int64(10),
			},
			TableName: tablename,
		})
		if err != nil {
			return err
		}
		fmt.Printf("%#v\n", createTableOutput)
	}

	item, err := dynamodbattribute.ConvertToMap(Item{"id1", "field1", 1})
	if err != nil {
		return err
	}
	fmt.Printf("%#v\n", item)

	// PutItem request overwrite existed item (same primary key) by default.
	putItemOutput, err := ddb.PutItem(&dynamodb.PutItemInput{
		Item:      item,
		TableName: tablename,
	})
	if err != nil {
		return err
	}
	// empty response even if successfly put
	fmt.Printf("%#v\n", putItemOutput)

	getItemOutput, err := ddb.GetItem(&dynamodb.GetItemInput{
		Key: map[string]*dynamodb.AttributeValue{
			"id": {
				S: aws.String("id1"),
			},
		},
		TableName: tablename,
	})
	if err != nil {
		return err
	}
	fmt.Printf("%#v\n", getItemOutput)

	var putreq []*dynamodb.WriteRequest
	for i := 0; i < 10; i++ {
		it, err := dynamodbattribute.ConvertToMap(Item{
			ID:       fmt.Sprintf("batchwrite_id%d", i+100),
			Field1:   fmt.Sprintf("batchwriteItem%d", i),
			IntField: i,
		})
		if err != nil {
			return err
		}
		putreq = append(putreq, &dynamodb.WriteRequest{
			PutRequest: &dynamodb.PutRequest{
				Item: it,
			},
		})
	}

	batchWriteItemOutput, err := ddb.BatchWriteItem(&dynamodb.BatchWriteItemInput{
		RequestItems: map[string][]*dynamodb.WriteRequest{
			*table: putreq,
		},
	})
	if err != nil {
		return err
	}
	fmt.Printf("%#v\n", batchWriteItemOutput)

	batchGetItemOutput, err := ddb.BatchGetItem(&dynamodb.BatchGetItemInput{
		RequestItems: map[string]*dynamodb.KeysAndAttributes{
			*table: {
				Keys: []map[string]*dynamodb.AttributeValue{
					{
						"id": {
							S: aws.String("batchwrite_id100"),
						},
					},
					{
						"id": {
							S: aws.String("batchwrite_id101"),
						},
					},
				},
			},
		},
	})
	if err != nil {
		return err
	}
	fmt.Printf("%#v\n", batchGetItemOutput)

	deleteTableOutput, err := ddb.DeleteTable(&dynamodb.DeleteTableInput{
		TableName: tablename,
	})
	if err != nil {
		return err
	}
	fmt.Printf("%#v", deleteTableOutput)

	return nil
}