2016-04-05 6 views
1

指定されたパラメータで動的クエリを作成する方法はありますか?Cassandra 3 Java Driver動的クエリを作成する

public List getData(String title,Date dateFrom){ 
Statement statement = QueryBuilder.select() 
       .all().from("test_database", "books"); 
    if(dateFrom!=null){ 
     //add clause to statement to find books from 'dateFrom' 
    } 

} 

答えて

2

カサンドラで動的クエリを作成することは、コードの匂いのようなものです。 Cassandraは実際には「動的」クエリ用に設計されていないため、必要な特定のクエリパターンに基づいてテーブルを設計する必要があります。 Cassandraでは、WHOW句のルールに従っていることを確認する必要があるため、動的フィルタリングはすぐに乱雑になる可能性があります。したがって、ALLOW FILTERINGを使用する必要はありません。

//build your generic select 
     Select select = QueryBuilder.select().all().from("test"); 

     List<Clause> whereClauses = new ArrayList<>(); 

     /* 
     * Generate Clauses based on a value not being null. Be careful to 
     * add the clustering columns in the proper order first, then any index 
     */ 
     if(col1 != null) { 
      whereClauses.add(QueryBuilder.eq("col1Name", "col1Val")); 
     } 

     if(col2 != null) { 
      whereClauses.add(QueryBuilder.eq("col2Name", "col2Val")); 
     } 

     // Add the Clauses and execute or execute the basic select if there's no clauses 
     if(!whereClauses.isEmpty()) { 
      Select.Where selectWhere = select.where() 
      for(Clause clause : whereClauses) { 
       selectWhere.and(clause); 
      } 
      session.execute(selectWhere) 
     } else { 
      session.execute(select) 
     } 

は、とにかくここに、それはあなたのアプリケーションに適していた場合、あなたにこれを行う方法のアイデアを与える必要がありますいくつかの簡単なコードです

関連する問題