Skip to content

How to perform aggregation over data stored in NoSQL?

Devender Yadav edited this page Sep 14, 2016 · 1 revision

From Kundera 2.15.1 onwards, aggregation query can also be executed over the data stored in different NoSQL data stores

Why aggregations are needed?

Aggregations enable basic numerical operations to be directly performed over the field values. However, most of the NoSQL data stores do not provide this support. By using Kundera along with Elasticsearch as indexing data store you can perform these operations.

Supported Operations

  1. Max
  2. Min
  3. Sum
  4. Average

Supported Databases

  1. Cassandra
  2. HBase
  3. MongoDB
  4. CouchDB
  5. Oracle NoSQL
  6. Redis

How to execute aggregated query?

Following are the steps to start aggregations over data stored in NoSQL.

  • Specify the indexer in the persistence unit:
<persistence-unit name="esIndexerTest">
        <provider>com.impetus.kundera.KunderaPersistence</provider>
        <properties>
			<property name="kundera.nodes" value="localhost" />
			<property name="kundera.port" value="9160" />
			<property name="kundera.keyspace" value="KunderaExamples" />
			<property name="kundera.dialect" value="cassandra" />
			<property name="kundera.ddl.auto.prepare" value="create" />
			<property name="kundera.client.lookup.class"
				value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />
			<property name="kundera.indexer.class" value="com.impetus.client.es.index.ESIndexer"/>
		</properties>
	</persistence-unit>
  • Specify the fields in @IndexCollection on which aggregation need to be performed.
@IndexCollection(columns = { @com.impetus.kundera.index.Index(name = "price") })
public class Product
{
     @Column(name = "Price")
     private int price;
}
  • Now aggregations can be performed on the price field, below are the query examples.

Examples:

To find the minimum value of Price

String minQuery = "Select min(p.price) from Product p";

Query query = em.createQuery(minQuery);
Double output = query.getResultList().get(0);

//output is the minimum value of price.

To find the maximum value of Price

String maxQuery = "Select max(p.price) from Product p";

Query query = em.createQuery(maxQuery);
Double output = query.getResultList().get(0);

//output is the maximum value of price.

Similarly, to find average and sum below mentioned queries can be used.

String sumQuery = "Select sum(p.price) from Product p";

String avgQuery = "Select avg(p.price) from Product p";

For more details, please refer this test-case.

Client Specific Test cases

Clone this wiki locally