我做了一个小测试,比较了 Phoenix 批量插入和使用 hbase 的多个 put。而且 Phoenix 比直接看跌要慢得多(6.157 秒对 10,000 条记录的 0.083 秒)。
这是我的代码:
SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
final JdbcTemplate template = new JdbcTemplate();
template.setDataSource(dataSource);
dataSource.setUrl(PhoenixZK);
dataSource.setDriverClassName("org.apache.phoenix.jdbc.PhoenixDriver");
final PlatformTransactionManager txnManager = new DataSourceTransactionManager(dataSource);
TransactionTemplate txnTemplate = new TransactionTemplate(txnManager);
txnTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
template.execute("DROP TABLE IF EXISTS stats.prod_metrics");
template.execute("CREATE TABLE stats.prod_metrics ( host char(50) not null, created_date TIMESTAMP not null,\n" +
" txn_count bigint CONSTRAINT pk PRIMARY KEY (host, created_date) ) SALT_BUCKETS=36, COMPRESSION='SNAPPY', REPLICATION_SCOPE=1");
}
});
long startTime = System.currentTimeMillis();
final Random random = new Random();
txnTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
template.batchUpdate("UPSERT INTO stats.prod_metrics VALUES (?,?,?)", new BatchPreparedStatementSetter(){
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, "localhost-" + random.nextInt());
ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
ps.setInt(3, random.nextInt());
}
@Override
public int getBatchSize() {
return numRec;
}
});
}
});
System.out.println("elapse = " + (double)(System.currentTimeMillis() - startTime)/1000.0);
这是来自 phoonix 的日志记录:
16:03:42.544 DEBUG MutationState Sending 10000 mutations for STATS.PROD_METRICS with 20000 key values of total size 1950000 bytes
16:03:47.784 DEBUG MutationState Total time for batch call of 10000 mutations into STATS.PROD_METRICS: 5235 ms
为什么凤凰比直插花的时间长得多?我是不是做错了什么?
谢谢,肖恩