Last active
January 3, 2016 04:49
-
-
Save robert-zaremba/8411925 to your computer and use it in GitHub Desktop.
Test gorethinkdb driver for a sum of values in RethinkDB table.
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 ( | |
r "github.com/dancannon/gorethink" | |
"log" | |
) | |
type valT struct { | |
V float64 | |
} | |
func main() { | |
dbs, err := r.Connect(map[string]interface{}{ | |
"address": "localhost:28015", | |
"database": "test", | |
}) | |
if err != nil { | |
log.Fatalln(err.Error()) | |
} | |
rows, err := r.Db("test").Table("values").Run(dbs) | |
if err != nil { | |
log.Fatalln(err.Error()) | |
} | |
var total float64 = 0.0 | |
for rows.Next() { | |
var row valT | |
if err := rows.Scan(&row); err != nil { | |
log.Fatalln(err) | |
} | |
total += row.V | |
} | |
println(total) | |
} |
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
import sys | |
import rethinkdb as r | |
r.connect( "localhost", 28015).repl() | |
table = r.db('test').table('values') | |
if len(sys.argv) > 1: | |
print "Inserting records into 'values' table" | |
import random | |
random.seed() | |
tables = r.db('test').table_list().run() | |
print(tables) | |
if 'values' not in tables: | |
print 'creating "values" table' | |
r.db('test').table_create('values').run() | |
table.delete().run() | |
vals = [{"v": random.randint(0, 1000)} for i in xrange(17733)] | |
table.insert(vals, durability="soft").run() | |
total = 0 | |
for r in table.run(): | |
total += r['v'] | |
print total | |
# JS Data explorer reduction function: | |
# r.db('test').table('values').map(r.row('v')).reduce(function(acc, val) { return acc.add(val)}, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment