Skip to content

Instantly share code, notes, and snippets.

@jhecking
Created April 14, 2016 18:16
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/57c74ff89353b7772f9869f8025652c5 to your computer and use it in GitHub Desktop.
Save jhecking/57c74ff89353b7772f9869f8025652c5 to your computer and use it in GitHub Desktop.
Aerospike Node.js v1 Aggregation Query Example
var aerospike = require('aerospike')
var config = {log: {level: 3} }
var ns = 'test'
var set = 'demo'
aerospike.client(config).connect(function (error, client) {
client.put({ns: ns, set: set, key: 'k1'}, {i: 1}, function (error) {
if (error && error.code !== 0) throw new Error(error.message)
client.udfRegister('udf.lua', function (error) {
if (error && error.code !== 0) throw new Error(error.message)
client.udfRegisterWait('udf.lua', 50, function (error) {
if (error && error.code !== 0) throw new Error(error.message)
var statement = {
aggregationUDF: {module: 'udf', funcname: 'count'}
}
var query = client.query('test', 'demo', statement)
var stream = query.execute() // returns a stream object
stream.on('data', function (result) {
console.info('the result is: %d', result)
})
stream.on('error', function (error) {
console.error('error: %s (%d)', error.messge, error.code)
})
stream.on('end', function () {
console.log('bye bye!')
client.close()
})
})
})
})
})
{
"dependencies": {
"aerospike": "^1.0"
}
}
function count(stream)
local function mapper(rec)
return 1
end
local function reducer(v1, v2)
return v1 + v2
end
return stream : map(mapper) : reduce(reducer)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment