我在我的应用程序中使用Cassandra Node js驱动程序,并且获取100k记录它需要5.1秒,但使用java驱动程序在2.7秒内也是如此。下面是我在java和node js中的模式和代码。
Cassandra表架构
CREATE TABLE transactions_data (
app_name text,
api_name text,
app_id text,
start_time timestamp,
duration int,
end_time timestamp,
node_id text,
request_body text,
request_parameter_name1 text,
request_parameter_name2 text,
request_parameter_name3 text,
request_parameter_name4 text,
request_parameter_name5 text,
request_parameter_value1 text,
request_parameter_value2 text,
request_parameter_value3 text,
request_parameter_value4 text,
request_parameter_value5 text,
response_body text,
response_parameter_name1 text,
response_parameter_name2 text,
response_parameter_name3 text,
response_parameter_name4 text,
response_parameter_name5 text,
response_parameter_value1 text,
response_parameter_value2 text,
response_parameter_value3 text,
response_parameter_value4 text,
response_parameter_value5 text,
responsestatus text,
responsestatuscode text,
transaction_id text,
PRIMARY KEY ((app_name, api_name, app_id), start_time)
);
Java代码
public class CassandraQueryPerformance {
private Cluster cluster;
private Session session;
private String query;
public CassandraQueryPerformance(String host,String query) throws
IOException {
this.query=query;
cluster = Cluster.builder().addContactPoints(host)
.withSocketOptions(new
SocketOptions().setConnectTimeoutMillis(2000000)).build();
session = cluster.connect();
}
public void performanceTest() throws IOException {
Statement statement = new SimpleStatement(query);
statement.setFetchSize(100000);
statement.setReadTimeoutMillis(650000).enableTracing();
ResultSet rs = session.execute(statement);
ArrayList<Row> list = new ArrayList<>();
for (com.datastax.driver.core.Row row : rs) {
list.add(row);
}
System.out.println("list count "+list.size());
}
public void close() {
cluster.close();
session.close();
}
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
CassandraQueryPerformance cqp = new
CassandraQueryPerformance(args[0],args[1]);
long onlyQueryTime = System.currentTimeMillis();
cqp.performanceTest();
System.out.println("total time without open close " +
(System.currentTimeMillis() - onlyQueryTime));
cqp.close();
System.out.println("total time " + (System.currentTimeMillis() -
startTime));
}
}
节点js代码
'use strict';
const Hapi = require('hapi');
const cassandra = require('cassandra-driver');
const options1 = {
contactPoints: ['abcserver:9042'],
keyspace: 'demo'
}
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 9999
});
// Add the route
server.route({
method: 'GET',
path:'/get-transaction',
handler: function (request, reply) {
let allResults = [];
//console.time('client_initialize');
var client = new cassandra.Client(options1);
// console.timeEnd('client_initialize');
var cqlQuery="SELECT start_time, end_time, duration FROM " +
"transactions_data WHERE app_name = 'app_name-100'"+
" AND api_name ='api_name-1'"+
" AND app_id='app_id_19999999' AND "+
" start_time >= '2017-03-20 13:40:29' AND "+
" start_time <= '2017-04-25 13:40:29' ";
client.connect(function(err, response) {
console.time('queryTime');
const options = { prepare : true , fetchSize : 100000};
let formattedRow;
client.eachRow(cqlQuery, {}, options, function (n, row) {
allResults.push(row);
}, function (err, result) {
if (result && result.nextPage) {
result.nextPage();
}
else{
console.timeEnd('queryTime');
console.log("Total no of records...",allResults.length);
reply('success');
}
reply('Hello '+request.query.limit);
});
});
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
以毫秒为单位捕获的结果
Number of records Node Java Difference
10k 846.232 494 352.232
20k 1115.307 624 491.307
30k 1603.353 897 706.353
40k 2124.656 1051 1073.656
50k 2626.624 1437 1189.624
60k 2940.313 1912 1028.313
70k 3478.797 1866 1612.797
80k 4293.986 2108 2185.986
90k 4677.516 2228 2449.516
100k 5175.231 2379 2796.231
答案 0 :(得分:1)
从NodeJS开始是单线程的,而java驱动程序不应该是。这可以使CPU密集型任务(例如解压缩或解析Cassandra记录)在NodeJS上运行缓慢。
在驱动程序实现级别上存在许多差异,首先是在客户端打开连接和完成记录构建所花费的时间。默认情况下也可能存在许多差异,例如使用或不使用压缩进行数据传输。
我已经使用python运行测试并且去了,并设法通过许多工作流程获得x10性能。 NodeJS和Java是非常不同的语言,默认情况下Java应该提供更好的性能。