9721

Cassandra 3 Java Driver build dynamic queries

Question:

Is there a way to build dynamic queries by given parameters?

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' } }

Answer1:

Creating dynamic queries in Cassandra is kind of a code smell. Cassandra isn't really designed for "dynamic" queries, you should be designing tables based on specific query patterns that you need. Dynamic queries can quickly become messy since in Cassandra you'll have to make sure you're following the rules of the WHERE clause so you don't have to use ALLOW FILTERING.

Anyway here's some quick code that should give you an idea of how to do this if it's appropriate for your application:

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

Recommend

  • Ext.getCmp(id) or Ext.ComponentQuery.query(attribute)
  • Show item of the day
  • ('Unable to complete the operation against any hosts', {})
  • LINQ Query to find all tags?
  • Cassandra: Adding new column to the table
  • How do I store unsigned integers in Cassandra?
  • Java Caching frameworks for maintaining huge data
  • command line of process by name
  • How many Vertica Databases can run on a Host in the same time?
  • SQL Server re-calculate or not?
  • Does the MySQL IN clause execute the subquery multiple times?
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • What is the use of a session store?
  • NHibernate manually control fetching
  • Which open source license has no forking [closed]
  • Yii2: Config params vs. const/define
  • Get data from AJAX - How to
  • Ajax Loaded meta Tags
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Adding a button at the bottom of a table view
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Why winpcap requires both .lib and .dll to run?
  • KeystoneJS: Relationships in Admin UI not updating
  • log4net write single file for each call to log.info
  • Acquiring multiple attributes from .xml file in c#
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal