Skip to content

Instantly share code, notes, and snippets.

@jhecking
Last active August 24, 2016 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhecking/b98783bea7564d610ea291b5ac47808c to your computer and use it in GitHub Desktop.
Save jhecking/b98783bea7564d610ea291b5ac47808c to your computer and use it in GitHub Desktop.
Aerospike Go client example for Aggregate Query
package main
import (
"fmt"
. "github.com/aerospike/aerospike-client-go"
)
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
func setup(client *Client, ns string, set string, module string) {
SetLuaPath("./")
file := module + ".lua"
t, err := client.RegisterUDFFromFile(nil, file, file, LUA)
panicOnError(err)
<- t.OnComplete()
key1, _ := NewKey(ns, set, 1)
client.PutBins(nil, key1, NewBin("filename", "file1"), NewBin("version", 3))
key2, _ := NewKey(ns, set, 2)
client.PutBins(nil, key2, NewBin("filename", "file2"), NewBin("version", 7))
key3, _ := NewKey(ns, set, 3)
client.PutBins(nil, key3, NewBin("filename", "file3"), NewBin("version", 2))
}
func main() {
client, err := NewClient("192.168.33.10", 3000)
panicOnError(err)
ns := "test"
set := "udf-go"
module := "udfFilter"
setup(client, ns, set, module)
stmt := NewStatement(ns, set)
recordset, err := client.QueryAggregate(nil, stmt, module, "maxVersion")
panicOnError(err)
for rec := range recordset.Results() {
res := rec.Record.Bins["SUCCESS"].(map[interface{}]interface{})
fmt.Printf("filename with max. version: %s (ver. %d)\n", res["filename"], res["version"])
}
}
function maxVersion(stream, bin)
-- The stream function cannot return record objects directly,
-- so we have to map to a Map data type first.
local function toArray(rec)
local result = map()
result['filename'] = rec['filename']
result['version'] = rec['version']
return result
end
local function findMax(a, b)
if a.version > b.version then
return a
else
return b
end
end
return stream : map(toArray) : reduce(findMax)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment