redis INFO命令返回类似redis_version:2.2.14\r\nredis_git_sha1:00000000\r\nredis_git_dirty:0\r\narch_bits:32
如何转换字符串以获取类似
的JSON对象 {
"redis_version":"x",
"key2":"value"
}
答案 0 :(得分:5)
我不知道你为什么要那样做,但这是一个简单的例子:
function parseInfo( info ) {
var lines = info.split( "\r\n" );
var obj = { };
for ( var i = 0, l = info.length; i < l; i++ ) {
var line = lines[ i ];
if ( line && line.split ) {
line = line.split( ":" );
if ( line.length > 1 ) {
var key = line.shift( );
obj[ key ] = line.join( ":" );
}
}
}
return obj;
}
答案 1 :(得分:3)
您可以使用node-redis-info:
npm install redis-info
用法:
> var parser = require('redis-info');
undefined
> var info = parser.parse(redis_info_str);
undefined
> info.fields.redis_version
2.6.1
> info.startWith('pubsub')
[ [ 'pubsub_channels', '2' ],
[ 'pubsub_patterns', '0' ] ]
> info.contains('memory')
[ [ 'used_memory', '15080416' ],
[ 'used_memory_human', '14.38M' ],
[ 'used_memory_rss', '21258240' ],
[ 'used_memory_peak', '18985904' ],
[ 'used_memory_peak_human', '18.11M' ] ]
答案 2 :(得分:0)
public List<Double> average(double[][] mat, int row_count){
List<Double> averageList = new ArrayList<>();
for(int i = 0; i < row_count; i++){
double averageOfStudent = (mat[i][0] + mat[i][1] + mat[i][2])/3;
averageList.add(averageOfStudent);
}
return averageList;
}
答案 3 :(得分:0)
简单:
#!/usr/bin/env python
import redis
import json
print(json.dumps(redis.Redis().info()))
对于 zabbix 模板数据库Redis:
#!/usr/bin/env python
import redis
import json
import sys
r = redis.Redis.from_url(sys.argv[1])
a = {}
sections = ["CPU","Clients","Cluster","Keyspace","Memory","Modules","Persistence","Replication","Server","Stats"]
for section in sections:
a[section]=r.info(section)
print(json.dumps(a))