Created
April 14, 2016 18:16
-
-
Save jhecking/57c74ff89353b7772f9869f8025652c5 to your computer and use it in GitHub Desktop.
Aerospike Node.js v1 Aggregation Query Example
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
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() | |
}) | |
}) | |
}) | |
}) | |
}) |
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
{ | |
"dependencies": { | |
"aerospike": "^1.0" | |
} | |
} |
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
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