项目地址:Pegasus NodeJS Client
下载并将客户端依赖添加到package.json中:npm install pegasus-nodejs-client --save
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, });
log_config.js
文件中,配置如下: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" } } };
上述配置表示,会将错误级别等于及高于INFO级别的日志存储在文件中,每个日志文件最大100M,最多保留10个日志文件
// close client when you do not need to use it client.close();
读单行数据
/** * 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 } );
Buffer('')
写单行数据
/** * 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 } );
删除单行数据
/** * 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 } );
读同一个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 } );
读取一批数据
/** * 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 } );
写同一个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 } );
写入一批数据
/** * 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 } );