How to bind values to bound statement in a generic way using datastax java driver?

孤街醉人 提交于 2019-12-25 08:38:05

问题


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.

I have created below two methods to execute my cql query.

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.

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.

First Method:-

  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);
      }
    });
  }

Second Method:-

  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();
  }

Do I have to use BoundStatement here at all? I guess I have to because that's how I can set consistency level?


回答1:


How about that:

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);
}


来源:https://stackoverflow.com/questions/41757029/how-to-bind-values-to-bound-statement-in-a-generic-way-using-datastax-java-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!