簡體   English   中英

如何使用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。

我創建了以下兩種方法來執行cql查詢。

我不需要在cql查詢中設置任何值時會調用第一個方法,因此它適用於cql字符串,如下所示:

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

現在,只要有這樣的查詢,就需要調用第二個方法:

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-4cql-5做什么? 我知道我必須使用BoundStatement設置這些值,但是如果我需要設置兩個或三個或四個值,我該如何使該方法通用,這樣我就不需要進行任何硬編碼了? 有些可以是整數,有些可以是字符串。

第一種方法:

  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嗎? 我猜我必須這樣做,因為那是我可以設置一致性級別的方法?

那個怎么樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM