如何使用Datastax Java驱动程序以通用方式将值绑定到bound语句? [英] How to bind values to bound statement in a generic way using datastax java driver?
问题描述
我正在使用Datastax Java驱动程序来读写Cassandra中的数据.我正在使用datastax Java驱动程序3.1.0,而我的cassandra群集版本是2.0.10.
I am working with Datastax Java driver to read and write data into Cassandra. I am using datastax java driver 3.1.0 and my cassandra cluster version is 2.0.10.
我创建了以下两种方法来执行cql查询.
I have created below two methods to execute my cql query.
当我不需要在cql查询中设置任何值时,我正在调用第一个方法,因此它适用于cql字符串,如下所示:
I am calling first method when I don't need to set any value in my cql query so it works for cql string like below:
select * from testkeyspace.testtable where row_id=1 // cql-1
select * from testkeyspace.meta where row_id=1 // cql-2
select * from testkeyspace.proc where row_id=1 // cql-3
现在,只要有这样的查询,我就需要调用第二种方法:
Now I need to call second method whenever I have query like this:
select * from testkeyspace.testtabletwo where row_id=1 and active=? // cql-4
select * from testkeyspace.storage where topic=? and parts=? // cql-5, in this part is Integer and topic is String.
所以我的问题是如何使第二种方法更通用,以便如果我需要使用BoundStatement在cql查询中设置n个值,它应该可以工作?现在,我不确定用传递给该方法的值调用第二个方法时应该对cql-4
和cql-5
做什么?我知道我必须使用BoundStatement设置这些值,但是如果我需要设置两个或三个或四个值,如何使该方法通用,这样我就不需要硬编码了?有些可以是整数,有些可以是字符串.
So my question is how can I make my second method more generic so that if I need to set n number of values in my cql query using BoundStatement, it should work? Right now I am not sure what I am supposed to do for cql-4
and cql-5
while calling my second method with values passed to that method? I know I have to set those values using BoundStatement but if I need to set two or three or four value, how can I make that method generic so that I don't need to hardcode anything? Some can be Integer and some can be String.
第一种方法:-
public ResultSet executeQuery(final String cql) {
return executeWithSession(new SessionCallable<ResultSet>() {
@Override
public ResultSet executeWithSession(Session session) {
BoundStatement bs = getStatement(cql);
bs.setConsistencyLevel(consistencyLevel);
return session.execute(bs);
}
});
}
第二种方法:-
public ResultSet executeQuery(final String cql, final Object... values) {
return executeWithSession(new SessionCallable<ResultSet>() {
@Override
public ResultSet executeWithSession(Session session) {
BoundStatement bs = getStatement(cql);
bs.setConsistencyLevel(consistencyLevel);
// how to set these **values** into my cql query using BoundStatement in a generic way
// so that I don't need to hardcode anything for cql-4 and cql-5
return session.execute(cql, values);
}
});
}
// cache the prepared statement
private BoundStatement getStatement(final String cql) {
Session session = getSession();
PreparedStatement ps = cache.get(cql);
// no statement cached, create one and cache it now.
if (ps == null) {
ps = session.prepare(cql);
PreparedStatement old = cache.putIfAbsent(cql, ps);
if (old != null)
ps = old;
}
return ps.bind();
}
我必须在这里使用BoundStatement
吗?我猜我必须这样做,因为那是我可以设置一致性级别的方法?
Do I have to use BoundStatement
here at all? I guess I have to because that's how I can set consistency level?
推荐答案
那呢:
public ResultSet executeQuery(final String cql, final Object... values) {
return executeWithSession(new SessionCallable<ResultSet>() {
@Override
public ResultSet executeWithSession(Session session) {
BoundStatement bs = getStatement(cql, values);
bs.setConsistencyLevel(consistencyLevel);
return session.execute(cql);
}
});
}
// cache the prepared statement
private BoundStatement getStatement(final String cql, Object... values) {
Session session = getSession();
PreparedStatement ps = cache.get(cql);
// no statement cached, create one and cache it now.
if (ps == null) {
ps = session.prepare(cql);
PreparedStatement old = cache.putIfAbsent(cql, ps);
if (old != null)
ps = old;
}
return ps.bind(values);
}
这篇关于如何使用Datastax Java驱动程序以通用方式将值绑定到bound语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!