Skip to content

Instantly share code, notes, and snippets.

@phstc phstc/query.go
Last active Aug 29, 2015

Embed
What would you like to do?
package main
import (
"fmt"
"github.com/crowdmob/goamz/aws"
"github.com/crowdmob/goamz/dynamodb"
"log"
"os"
"time"
)
func main() {
start := time.Now()
fmt.Printf("starting\n")
os.Setenv("AWS_ACCESS_KEY_ID", "...")
os.Setenv("AWS_SECRET_ACCESS_KEY", "...")
auth, err := aws.EnvAuth()
if err != nil {
log.Fatal(err.Error())
}
ddbs := dynamodb.New(auth, aws.USEast)
primary := dynamodb.NewStringAttribute("account_id", "")
key := dynamodb.PrimaryKey{primary, nil}
table := ddbs.NewTable("moby_events", key)
q := dynamodb.NewQuery(table)
// q.AddKey(&dynamodb.Key{HashKey: "test"})
acs := []dynamodb.AttributeComparison{
*dynamodb.NewStringAttributeComparison("account_id", "EQ", "CUfKqynamDnZV9+d"),
}
q.AddKeyConditions(acs)
result, err := dynamodb.RunQuery(q, table)
if err != nil {
log.Fatal(err.Error())
}
for i, n := range result {
fmt.Printf("%2d: %s\n", i, n["created_at"].Value)
}
// fmt.Printf("%s\n", q.String())
elapsed := time.Since(start)
fmt.Printf("Completed in %dms", elapsed.Nanoseconds()/1000)
}
require 'aws-sdk'
started_at = Time.now
client = Aws::DynamoDB::Client.new(
region: 'us-east-1',
access_key_id: '...',
secret_access_key: '...'
)
resp = client.query(
table_name: 'moby_events',
key_conditions: {
'account_id' => {
attribute_value_list: ['CUfKqynamDnZV9+d'],
comparison_operator: 'EQ',
},
}
)
resp.items.each_with_index do |item, index|
puts "#{index}: #{item['created_at']}"
end
puts "Completed in: #{(Time.now - started_at) * 1000} ms"

Result

golang completed in 1164.213ms

ruby completed in 2431.504ms

all execution

golang:

  • real 0m1.228s
  • user 0m0.217s
  • sys 0m0.029s

ruby:

  • real 0m3.292s
  • user 0m1.614s
  • sys 0m0.135s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.