Install the Node.js Client
Project repository: Pegasus NodeJS Client
Download and add the client dependency to package.json:
npm install pegasus-nodejs-client --save
Create/Close Client
create
let pegasusClient = require('pegasus-nodejs-client');
/**
* Create a client instance
* @param {Object} configs
* {Array} configs.metaServers required
* {String} configs.metaServers[i] required
* {Number} configs.operationTimeout(ms) optional
* {Object} configs.log optional
* @return {Client} client instance
* @throws {InvalidParamException}
*/
client = pegasusClient.create({
metaServers: ['127.0.0.1:34601', '127.0.0.1:34602', '127.0.0.1:34603'],
operationTimeout : 5000,
log : log,
});
metaServersis the list of meta server addresses and is required.operationTimeoutis the timeout for the current operation in milliseconds, default is1000ms.logis the logger instance.- We use log4js.
- The default logger configuration is in
log_config.js, as follows:let filename = "./logs/"+process.pid+"/pegasus-nodejs-client.log"; let logConfig = { appenders: { pegasus: { type: "file", filename: filename, maxLogSize: 104857600, backups: 10 } }, categories: { default: { appenders: ["pegasus"], level: "INFO" } } };The above configuration means logs of level
INFOand above are written to file, each file is capped at 100MB, and at most 10 backup files are kept. - If you don’t want to use the default configuration, redefine the
logConfigobject above and pass it as thelogobject when creating the client.
- If the parameters are invalid, an exception will be thrown and subsequent operations will stop.
close
// close client when you do not need to use it
client.close();
API
get
Read a single row.
/**
* Get value
* @param {String} tableName
* @param {Object} args
* {Buffer} args.hashKey required
* {Buffer} args.sortKey required
* {Number} args.timeout(ms) optional
* @param {Function} callback
* @throws{InvalidParamException} callback is not function
*/
client.get(
tableName,
args,
function(err, result){
// if get operation succeed, err will be null,
// result.hashKey is hashKey, result.sortKey is sortKey, result.value is value
// else err will be instance of PException, result will be null
}
);
- Required parameters for
getare table name,hashKey,sortKey, andcallback. hashKey,sortKey, andvalueareBufferobjects, consistent with Pegasus server semantics where key and value are bytes.timeoutis optional, defaulting to the timeout configured when creating the client.- On success,
callbackreceiveserr = nullandresult.valueis the retrieved value. - On failure,
callbackreceivesresult = null. - The client does not treat “not found” as an error. If key not found,
erris stillnullandresult.valueisBuffer('').
set
Write a single row.
/**
* Set Value
* @param {String} tableName
* @param {Object} args
* {Buffer} args.hashKey required
* {Buffer} args.sortKey required
* {Buffer} args.value required
* {Number} args.ttl(s) optional
* {Number} args.timeout(ms) optional
* @param {Function} callback
* @throws{InvalidParamException} callback is not function
*/
client.set(
tableName,
args,
function(err){
// if set operation succeed, err will be null
// else err will be instance of PException
}
);
- Required parameters for
setare table name,hashKey,sortKey,value, andcallback. ttlis the time-to-live in seconds. The defaultttlis0, meaning no expiration. For example,ttl = 86400means the data expires in 1 day, after which the value cannot be read.
del
Delete a single row.
/**
* Delete value
* @param {String} tableName
* @param {Object} args
* {Buffer} args.hashKey required
* {Buffer} args.sortKey required
* {Number} args.timeout(ms) optional
* @param {Function} callback
* @throws{InvalidParamException} callback is not function
*/
client.del(
tableName,
args,
function(err){
// if set operation succeed, err will be null
// else err will be instance of PException
}
);
- Required parameters for
delare table name,hashKey,sortKey, andcallback.
multiGet
Read multiple rows under the same hashKey.
/**
* Multi Get
* @param {String} tableName
* @param {Object} args
* {Buffer} args.hashKey required
* {Array} args.sortKeyArray required
* {Buffer} args.sortKeyArray[i] required
* {Number} args.timeout(ms) optional
* {Number} args.maxFetchCount optional
* {Number} args.maxFetchSize optional
* @param {Function} callback
* @throws{InvalidParamException} callback is not function
*/
client.multiGet(
tableName,
args,
function(err, result){
// if operation succeed, err will be null,
// result[i].hashKey is hashKey, result[i].sortKey is sortKey, result[i].value is value
// else err will be instance of PException, result will be null
}
);
- Required parameters for
multiGetare table name,hashKey, array ofsortKey, andcallback. - If
sortKeyArrayis an empty array, all sort keys under thehashKeywill be retrieved. maxFetchCountlimits the maximum number of entries returned, default100.maxFetchSizelimits the maximum bytes returned, default1000000bytes.
batchGet
Read a batch of entries.
/**
* Batch Get value
* @param {String} tableName
* @param {Array} argsArray
* {Buffer} argsArray[i].hashKey required
* {Buffer} argsArray[i].sortKey required
* {Number} argsArray[i].timeout(ms) optional
* @param {Function} callback
* @throws{InvalidParamException} callback is not function
*/
client.batchGet(
tableName,
argsArray,
function(err, result){
// err will be always be null, result is {'error': err, 'data': result} array
// if batchGet[i] operation succeed, result[i].error will be null
// result[i].data.hashKey is hashKey, result[i].data.sortKey is sortKey, result[i].data.value is value
// else result[i].error will be instance of PException, result[i].data will be null
}
);
- Required parameters for
batchGetare table name, arrays ofhashKeyandsortKey, andcallback. - Unlike
multiGet,batchGetsupports reading values from multiplehashKeys. batchGetwaits until allgetoperations in the batch have returned before responding.callback’serris alwaysnull.callback’sresultis an array, whereresult[i].errorindicates the error of the i-thgetoperation, andresult[i].dataindicates its result.
multiSet
Write multiple rows under the same hashKey.
/**
* Multi Set
* @param {String} tableName
* @param {Object} args
* {Buffer} args.hashKey required
* {Array} args.sortKeyValueArray required
* {'key' : sortKey, 'value' : value}
* {Number} args.timeout(ms) optional
* {Number} args.ttl(s) optional
* @param {Function} callback
* @throws{InvalidParamException} callback is not function
*/
client.multiSet(
tableName,
args,
function(err){
// if set operation succeed, err will be null
// else err will be instance of PException
}
);
- Required parameters for
multiSetare table name,hashKey, array ofsortKey-valueobjects, andcallback.
batchSet
Write a batch of entries.
/**
* Batch Set value
* @param {String} tableName
* @param {Array} argsArray
* {Buffer} argsArray[i].hashKey required
* {Buffer} argsArray[i].sortKey required
* {Buffer} argsArray[i].value required
* {Number} argsArray[i].ttl optional
* {Number} argsArray[i].timeout(ms) optional
* @param {Function} callback
* @throws{InvalidParamException} callback is not function
*/
client.batchSet(
tableName,
argsArray,
function(err, result){
// err will be always be null, result is {'error': err} array
// if batchSet[i] operation succeed, result[i].error will be null
// else result[i].error will be instance of PException
}
);
- Required parameters for
batchSetare table name, array ofhashKey-sortKey-valueobjects, andcallback. callback’serris alwaysnull.result[i].errorindicates the error status of the i-thsetoperation.