Search This Blog

Showing posts with label Innovation. Show all posts
Showing posts with label Innovation. Show all posts

Wednesday, May 11, 2022

How Netflix onboards new content - Video Processing at scaleπŸŽ₯

Everyday, #Netflix handles billions of requests regarding movies, trailers and other video content. Delivering at such a large scale needs an #engineering marvel. This #video will talk about how Netflix is able to onboard new video content onto their platform. We go from video chunking to collating 4 second shots into scenes.

Amazon S3 is used to store the video chunks. Netflix also provides open connect servers to internet service providers, which acts like a cache of movies. Most requests to Netflix can be served by this cache, and the remaining are sent over the network. This reduces the bandwidth and time required for Netflix to operate at scale. Synergy at it's finest.




Video Formats and Resolutions

  • Different formats
    • High quality
    • Medium quality
    • Low quality
  • Different resolutions
    • 1080p
    • 720p
    • 480p
  • Storage combination comes F x R -> V
  • What netflix does is like broken down a video into smaller parts so that it can be deal with effectively per processor
  • One resolution, one format, one chunk - that's one task
Chunk Processing
  • What they are doing intelligently is breaking the chunks not based on timestamps but based on scenes to have seamless user experience.
  • Based os scenes so you can make instead of 3 min thing, you can make it more fine grained 4 sec each, it's called a shot and you can collate shot, put them all together to create a scene.
  • Prediction algorithm is pretty smart to understand if user is not watching with engaged mode and clicking fwd to see the movie, instead of giving the whole content, it gives only data to the user has asked for because they are probably clicking on different points in that buffer you get.
  • On the other hand, if user if watching in engaged mode, so instead of sending just the part user has asked for, it redictively proactively fetches the future parts, gets onto your computer and shows it you.
Storage
  • Amazon S3 is what Netflix uses to store that video content.
    • This is where people store their static data meaning that you don't change that data.
    • It's extremely cheap compared to a database.
OpenConnect for video caching
  • Netflix servers are usually in the U.S which means they are geographically concentrated and in a place like India which is really far, it's going to take lot of time to send the signal and receive it especially if it's video because there is lot of data which is going to be coming in and it's going to be slow.
  • What Netflix did intelligently to extend the concept of caching and apply it to ISP's. When request comes to ISP for the movie, it looks for local cache say cache for Indian movies.
  • Cache has been called as OpenConnect
  • Lots of bandwidth saved
  • Lots of time saved
  • Much better user experience
  • 90% of the Netflix traffic is taken care by these ISP boxes that they provide.
  • W.r.t new content overnight job can be run to copy the new contents when there is less load on server for the requests.

Thursday, October 17, 2019

Framework Evaluation & Selection

Framework Evaluation Criteria

  1. Support automation integration testing for UI (real/headless browser/device), API,  performance in cross platforms.

    • For the integration test, we don’t test the front-end or back-end individually. We need to verify the data with the integration between system, front-end & back-end. So the framework needs to be able to support all testing type and especially crossing browser, device & system.
  2. Support CI integration with parallel execution.

    • The testing framework needs to be able to integrate with our current CI/CD. Parallel execution will help reduce the build time, so we will be able to deploy quickly and have faster turnaround times for bugs and features.
  3. Supports the concept of executable documentation for BDD (behavior-driven development)& DDT (data-driven testing) in modularization, maintainable and understandable test suites.

    • BDD will help the test case & test suite easy to maintaining and understanding. It also helps communication between business and development is extremely focused as a result of common language.
    • One of the most important concepts for effective test automation is modularization, it will help to create a sequence of test case only one and reuse as often as required in test script without rewriting the test all the time.

BDD Testing Framework Selection


GaugeCucumber
LanguageMarkdownGherkin
IDE & plugin supportYesYes
Easy to integrate with CI/CDYesYes
Easy to use, quick to learnNoNo
Reusable, easy to maintainYesYes
Parallel executionBuilt-in3rd party plugin
Customize reportingYesYes
PriceOpen source & free Open source & free 

Winner - Gauge:

  • An open source lightweight cross-platform test automation tool with the ability to author test cases in the business language and have built-in parallel execution feature.
  • Support BDD (Behavior-Driven Development) & CI(Continuous Integration) & report customization.

API Testing Framework Selection


REST AssuredPostman
Support BDDYes3rd third party
Support DDTYesLimit
Easy to integrate with CI/CDYesYes
Easy to use, quick to learnNoYes
Reusable, easy to maintainYesNo
Customize reportingCan be used with any customized/open source reporting tool.No
PriceOpen source & free 8-21$ per user/month for professional collaboration  & advanced features
  

Winner - REST Assured:

  • An open source Java-based Domain-Specific Language (DSL) that allows writing powerful, readable, and maintainable automated tests for RESTful APIs. 
  • Support testing and validating REST services in BDD (Behavior-Driven Development) / Gherkin format.

API Performance Testing Framework  Selection


Apache JMeterLoadrunner
Support DDTYesYes
Easy to integrate with CI/CDYesYes
Easy to use, quick to learnYesNo
Cross-platformYesWindows, Linux
Reusable, easy to maintainYesNo
Customize reportingYesYes
PriceOpen source & free Free for first 50 virtual User

Winner - Apache JMeter : 

  • Open source performance test runner & management framework may be used to test performance both on static and dynamic resources.
  • Support load test functional behavior and measure performance. It can be used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types.

Test Runner & Test Suite Management Framework Selection


TestNGJUnit
Support DDTYesYes
Easy to integrate with CI/CDYesYes
Easy to use, quick to learnNoNo
Reusable, easy to maintainYesYes
Parallel executionYesYes
PriceOpen source & free Open source & free 
Annotation supportYesLimit
Suite TestYesYes
Ignore TestYesYes
Exception TestYesYes
TimeoutYesYes
Parameterized TestYesYes
Dependency TestYesNo
Support executing before & after all tests in the suiteYesNo
Support executing  before & after a test runsYesNo
Support executing  before the first & last test method
is invoked that belongs to any of these groups is invoked
YesNo

Winner - TestNG :

  • Open source test runner framework which helps to run your tests in arbitrarily big thread pools with various policies available and flexible test configuration.
  • Support DDT(Data-driven testing) & Test suite management.

Build Tool & Dependency ManagementFramework Selection


Apache MavenGrandle
Easy to integrate with CI/CDYesYes
Easy to use, quick to learnYesNo
Build Script LanguageXMLGroovy
Reusable, easy to maintainYesYes
Dependency ManagementYesYes
Dependency ScopesBuilt-inCustom
IDE & plugin supportManyA little
PriceOpen source & free Open source & free 

Winner - Apache Maven 

  • The leading open source dependency management and build tool. It standardizes the software build process by articulating a project’s constitution.
  • Software project management and comprehension tool in the concept of a project object model (POM), Maven can manage a project's build, reporting, and documentation from a central piece of information.

Sunday, September 15, 2019

Mocking a RESTful Microservice in MounteBank

What is MounteBank?

Mountebank is the first open source tool to provide cross-platform, multi-protocol test doubles over the wire. Simply point your application under test to mountebank instead of the real dependency, and test like you would with traditional stubs and mocks.
Basically it provides a server that can be configured using a DSL to simulate requests and responses over variety of protocols (http, https, tcp and smtp).

How it works?

MounteBank

Installation

There are various ways to install MounteBank on different platforms. We recommend to use via npm.
  1. Install npm
  2. Install MounteBank globally
$ npm mountebanck -g

Start MounteBank

$ mb –allowCORS –allowInjection –mock
By default it starts on the port 2525.
Now let’s learn some basic terminologies used by MounteBank DSL.
  • Response - Defines status code, headers and body
  • Predicate - Conditions to check request to match some criteria based on which response will be returned
  • Stub - Also called Imposter is a Collection of predicates and responses for simulating an API
The DSL is very rich and we cannot cover everything here. For more details visit the API contract.
Now let us mock Get All Posts and Get a Specific Post API.
Use this JSON and send it to MounteBank Server already running on your local machine.

MounteBank Server
Create an Impostor for Get All Posts

It should create an impostor successfully and return same JSON as you sent in request.
Now go and hit http://localhost:9999/posts in REST Client or Browser and you should receive JSON having two posts data.

Response from GET /posts
Response from GET /posts

Response from POST /posts
Response from POST /posts

Now it’s your turn. Create impostors for rest of the RESTful APIs and get hands on it.

Thursday, April 7, 2016

Why Spring framework and What exactly is Spring for?

One of the hardest part in learning Spring is to understand why do you need spring and what is it exactly. Once this is clear, rest all becomes easy. Spring is generally described as ” A light weight framework for building Java applications”. This means any type of Java application – standalone java, JEE applications , Web applications etc. The light weight means you have to make only a few change or none at all to your application code to get the benefits of Spring. This is very important. You don’t need to extend your classes from any particular Spring framework component to get the framework features. This statement is applicable to Spring core.
Spring provides IOC  ( Inversion of Control) and DI ( Dependency Injection ) Capabilities. Minimizing the Dependencies is one of most important factors in creating maintainable, extensible software and Spring solves this beautifully. Let us explore IOC and Dependency Injection in the next section.

Inversion of Control ( IOC )

IoC is a technique that externalizes the creation and management of component dependencies. Inversion of Control is best understood through the term the “Hollywood Principle,” which basically means “Don’t call me, I’ll call you.” 
Consider the case where  Class Person depends on an instance of Class Address.  In traditional programming practice, Class Person either instantiates an instance of Address or Class Person uses a factory class to get the instance of Address. With the IOC practice, an instance of Address is given to Person at run time by the framework.  Thus the dependency is injected by the framework at run time and hence it is also called Dependency Injection. Effectively Inversion of Control is a means to achieve Dependency Injection. Many people use IOC and Dependency Injection interchangeably. In fact Spring acts like a container providing your application classes with all dependencies it needs.
Let us explore the beauty of Dependency Injection with an example
 package com.javahash.spring;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

class Person {

Address address;

public Person() {

}

@Autowired

public void setAddress(Address a){

this.address=a;

}

public Address getAddress(){

return address;

}

}

In the above Code, we simply tell spring that Person requires an instance of Address and that dependency should be satisfied using the setter method annotated with @Autowire.
@Autowired will instruct Spring framework to look for a bean that implements the required interface and inject it automatically via the setter.
The spring framework will  instantiate a valid instance of Address and wire it to the Person class. This is the beauty of Dependency Injection / IOC. There is no plumbing code put by the developer to satisfy the dependency. In my opinion IOC/ DI is the most important reason for the popularity and usefulness of Spring framework.  Spring framework provides better means of  non intrusive, plug-gable programming model.
There is more to Spring – it also provides declarative programming with AOP ( Aspect Oriented Programming). This is a better way to implement cross cutting concerns without the needs to use plumbing code all over the core business classes.
AOP enables you to think about concerns or aspects in your system. Typical concerns are transaction management, logging etc. AOP enables you to capture the cross-cutting code in modules such as interceptors that can be applied declaratively wherever the concern they express applies.Spring includes a proxy-based AOP framework.
But is that all that Spring offers ?. The answer is a BIG NO. Spring provides a lot of components that help with data access / integration, Web programming and other common programming requirements.
spring-overview
Spring also has a number of projects than utilizes the spring core and makes programming easier and cleaner.  Few of the Spring projects are
  • Spring Batch
  • Spring AMQP
  • Spring Boot
  • Spring Data Commons
  • Spring Data GemFire
  • Spring Data JDBC Extensions
  • Spring Data JDBC Extensions
  • Spring Data JPA
  • Spring Data MongoDB
  • Spring Data Neo4J
  • Spring Data Redis
  • Spring Data REST
  • Spring Data Solr
  • Spring Flex
  • Spring for Android
  • Spring for Apache Hadoop
  • Spring Framework
  • Spring HATEOAS
  • Spring Integration
  • Spring LDAP
  • Spring Mobile
  • Spring Roo
  • Spring Security
  • Spring Security OAuth
  • Spring Shell
  • Spring Social
  • Spring Social Facebook
  • Spring Social Twitter
  • Spring Web Flow
  • Spring Web Services


Basically Spring is a framework for  which is a pattern that allows to build very decoupled systems. I'll try to explain you the simplest I can (this isn't a short answer).

The problem

For example, suppose you need to list the users of the system and thus declare an interface called UserLister:
public interface UserLister {
    List getUsers();
}
And maybe an implementation accessing a database to get all the users:
public class UserListerDB implements UserLister {
    public List getUsers() {
        // DB access code here
    }
}
In your view you'll need to access an instance (just an example, remember):
public class SomeView {
    private UserLister userLister;

    public void render() {
        List users = userLister.getUsers();
        view.render(users);
    }
}
Note that the code above doesn't have initialized the variable userLister. What should we do? If I explicitly instantiate the object like this:
UserLister userLister = new UserListerDB();
...I'd couple the view with my implementation of the class that access the DB. What if I want to switch from the DB implementation to another that gets the user list from a comma-separated file (remember, it's an example)? In that case I would go to my code again and change the last line by:
UserLister userLister = new UserListerCommaSeparatedFile();
This has no problem with a small program like this but... What happens in a program that has hundreds of views and a similar number of business classes. The maintenance becomes a nightmare!

Spring (Dependency Injection) approach

What Spring does is to wire the classes up by using a XML file, this way all the objects are instantiated and initialized by Spring and injected in the right places (Servlets, Web Frameworks, Business classes, DAOs, etc, etc, etc...).
Going back to the example in Spring we just need to have a setter for the userLister field and have an XML like this:



    

This way when the view is created it magically will have a UserLister ready to work.
List users = userLister.getUsers();  // This will actually work
                                           // without adding any line of code
It is great! Isn't it?
  • What if you want to use another implementation of your UserLister interface? Just change the XML
  • What if don't have a UserLister implementation ready? Program a temporal mock implementation of UserLister and ease the development of the view
  • What if I don't want to use Spring anymore? Just don't use it! Your application isn't coupled to it. Inversion of Control states: "The application controls the framework, not the framework controls the application".
There are some other options for Dependency Injection around there, what in my opinion has made Spring so famous besides its simplicity, elegance and stability is that the guys of SpringSource have programmed many many POJOs that help to integrate Spring with many other common frameworks without being intrusive in your application. Also Spring has several good subprojects like Spring MVC, Spring WebFlow, Spring Security and again a loooong list of etceteras.

Wednesday, April 6, 2016

Hibernate Criteria Queries Tutorial and Examples

Hibernate provides three different ways to retrieve data from database. The Criteria Query API lets you build nested, structured query expressions in Java, providing a compile-time syntax checking that is not possible with a query language like HQL or SQL. The Criteria API also includes query by example (QBE) functionality. This lets you supply example objects that contain the properties you would like to retrieve instead of having to step-by-step spell out the components of the query. It also includes projection and aggregation methods, including count(). Let’s explore it’s different features in detail.y

Basic Usage Example

The Criteria API allows you to build up a criteria query object programmatically; the org.hibernate.Criteria interface defines the available methods for one of these objects. The Hibernate Session interface contains severalcreateCriteria() methods. Pass the persistent object’s class or its entity name to the createCriteria() method, and Hibernate will create a Criteria object that returns instances of the persistence object’s class when your application executes a criteria query.
The simplest example of a criteria query is one with no optional parameters or restrictions—the criteria query will simply return every object that corresponds to the class.
Criteria crit = session.createCriteria(Product.class);
List results = crit.list();
Moving on from this simple example, we will add constraints to our criteria queries so we can whittle down the result set.

Using Restrictions with Criteria

The Criteria API makes it easy to use restrictions in your queries to selectively retrieve objects; for instance, your application could retrieve only products with a price over $30. You may add these restrictions to a Criteria object with the add()method. The add() method takes an org.hibernate.criterion.Criterion object that represents an individual restriction. You can have more than one restriction for a criteria query.
i) Restrictions.eq() Example
To retrieve objects that have a property value that “equals” your restriction, use the eq() method on Restrictions, as follows:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.eq("description","Mouse"));
List results = crit.list()
Above query will search all products having description as “Mouse”.
ii) Restrictions.ne() Example
To retrieve objects that have a property value “not equal to” your restriction, use the ne() method on Restrictions, as follows:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.ne("description","Mouse"));
List results = crit.list()
Above query will search all products having description anything but not “Mouse”.
You cannot use the not-equal restriction to retrieve records with a NULL value in the database for that property (in SQL, and therefore in Hibernate, NULL represents the absence of data, and so cannot be compared with data). If you need to retrieve objects with NULL properties, you will have to use the isNull() restriction.
iii) Restrictions.like() and Restrictions.ilike() Example
Instead of searching for exact matches, we can retrieve all objects that have a property matching part of a given pattern. To do this, we need to create an SQL LIKE clause, with either the like() or the ilike() method. The ilike() method is case-insensitive.
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.like("name","Mou%",MatchMode.ANYWHERE));
List results = crit.list();
Above example uses an org.hibernate.criterion.MatchMode object to specify how to match the specified value to the stored data. The MatchMode object (a type-safe enumeration) has four different matches:
ANYWHERE: Anyplace in the string
END: The end of the string
EXACT: An exact match
START: The beginning of the string
iv) Restrictions.isNull() and Restrictions.isNotNull() Example
The isNull() and isNotNull() restrictions allow you to do a search for objects that have (or do not have) null property values.
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.isNull("name"));
List results = crit.list();
v) Restrictions.gt(), Restrictions.ge(), Restrictions.lt() and Restrictions.le() Examples
Several of the restrictions are useful for doing math comparisons. The greater-than comparison is gt(), the greater-than-or-equal-to comparison is ge(), the less-than comparison is lt(), and the less-than-or-equal-to comparison is le(). We can do a quick retrieval of all products with prices over $25 like this, relying on Java’s type promotions to handle the conversion toDouble:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.gt("price", 25.0));
List results = crit.list();
vi) Combining Two or More Criteria Examples
Moving on, we can start to do more complicated queries with the Criteria API. For example, we can combine AND and OR restrictions in logical expressions. When we add more than one constraint to a criteria query, it is interpreted as an AND, like so:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.lt("price",10.0));
crit.add(Restrictions.ilike("description","mouse", MatchMode.ANYWHERE));
List results = crit.list();
If we want to have two restrictions that return objects that satisfy either or both of the restrictions, we need to use the or()method on the Restrictions class, as follows:
Criteria crit = session.createCriteria(Product.class);
Criterion priceLessThan = Restrictions.lt("price", 10.0);
Criterion mouse = Restrictions.ilike("description", "mouse", MatchMode.ANYWHERE);
LogicalExpression orExp = Restrictions.or(priceLessThan, mouse);
crit.add(orExp);
List results=crit.list();
The orExp logical expression that we have created here will be treated like any other criterion. We can therefore add another restriction to the criteria:
Criteria crit = session.createCriteria(Product.class);
Criterion price = Restrictions.gt("price",new Double(25.0));
Criterion name = Restrictions.like("name","Mou%");
LogicalExpression orExp = Restrictions.or(price,name);
crit.add(orExp);
crit.add(Restrictions.ilike("description","blocks%"));
List results = crit.list();
vii) Using Disjunction Objects with Criteria
If we wanted to create an OR expression with more than two different criteria (for example, “price > 25.0 OR name like Mou% OR description not like blocks%”), we would use an org.hibernate.criterion.Disjunction object to represent a disjunction.
You can obtain this object from the disjunction() factory method on the Restrictions class. The disjunction is more convenient than building a tree of OR expressions in code. To represent an AND expression with more than two criteria, you can use the conjunction() method, although you can easily just add those to the Criteria object. The conjunction can be more convenient than building a tree of AND expressions in code. Here is an example that uses the disjunction:
Criteria crit = session.createCriteria(Product.class);
Criterion priceLessThan = Restrictions.lt("price", 10.0);
Criterion mouse = Restrictions.ilike("description", "mouse", MatchMode.ANYWHERE);
Criterion browser = Restrictions.ilike("description", "browser", MatchMode.ANYWHERE);
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(priceLessThan);
disjunction.add(mouse);
disjunction.add(browser);
crit.add(disjunction);
List results = crit.list();
viii) Restrictions.sqlRestriction() Example
sqlRestriction() restriction allows you to directly specify SQL in the Criteria API. It’s useful if you need to use SQL clauses that Hibernate does not support through the Criteria API.
Your application’s code does not need to know the name of the table your class uses. Use {alias} to signify the class’s table, as follows:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.sqlRestriction("{alias}.description like 'Mou%'"));
List results = crit.list();

Paging Through the ResultSet

One common application pattern that criteria can address is pagination through the result set of a database query. There are two methods on the Criteria interface for paging, just as there are for Query: setFirstResult() and setMaxResults(). The setFirstResult() method takes an integer that represents the first row in your result set, starting with row 0. You can tell Hibernate to retrieve a fixed number of objects with the setMaxResults() method. Using both of these together, we can construct a paging component in our web or Swing application.
Criteria crit = session.createCriteria(Product.class);
crit.setFirstResult(1);
crit.setMaxResults(20);
List results = crit.list();
As you can see, this makes paging through the result set easy. You can increase the first result you return (for example, from 1, to 21, to 41, etc.) to page through the result set.

Obtaining a Unique Result

Sometimes you know you are going to return only zero or one object from a given query. This could be because you are calculating an aggregate or because your restrictions naturally lead to a unique result. If you want obtain a single Object reference instead of a List, the uniqueResult() method on the Criteria object returns an object or null. If there is more than one result, the uniqueResult() method throws a HibernateException.
The following short example demonstrates having a result set that would have included more than one result, except that it was limited with the setMaxResults() method:
Criteria crit = session.createCriteria(Product.class);
Criterion price = Restrictions.gt("price",new Double(25.0));
crit.setMaxResults(1);
Product product = (Product) crit.uniqueResult();
Again, please note that you need to make sure that your query returns only one or zero results if you use theuniqueResult() method. Otherwise, Hibernate will throw a NonUniqueResultException exception.

Obtaining Distinct Results

If you would like to work with distinct results from a criteria query, Hibernate provides a result transformer for distinct entities, org.hibernate.transform.DistinctRootEntityResultTransformer, which ensures that no duplicates will be in your query’s result set. Rather than using SELECT DISTINCT with SQL, the distinct result transformer compares each of your results using their default hashCode() methods, and only adds those results with unique hash codes to your result set. This may or may not be the result you would expect from an otherwise equivalent SQL DISTINCT query, so be careful with this.
Criteria crit = session.createCriteria(Product.class);
Criterion price = Restrictions.gt("price",new Double(25.0));
crit.setResultTransformer( DistinctRootEntityResultTransformer.INSTANCE )
List results = crit.list();
An additional performance note: the comparison is done in Hibernate’s Java code, not at the database, so non-unique results will still be transported across the network.

Sorting the Query’s Results

Sorting the query’s results works much the same way with criteria as it would with HQL or SQL. The Criteria API provides theorg.hibernate.criterion.Order class to sort your result set in either ascending or descending order, according to one of your object’s properties.
This example demonstrates how you would use the Order class:
Criteria crit = session.createCriteria(Product.class);
crit.add(Restrictions.gt("price",10.0));
crit.addOrder(Order.desc("price"));
List results = crit.list();
You may add more than one Order object to the Criteria object. Hibernate will pass them through to the underlying SQL query. Your results will be sorted by the first order, then any identical matches within the first sort will be sorted by the second order, and so on. Beneath the covers, Hibernate passes this on to an SQL ORDER BY clause after substituting the proper database column name for the property.

Performing Associations (Joins)

The association works when going from either one-to-many or from many-to-one. First, we will demonstrate how to use one-to-many associations to obtain suppliers who sell products with a price over $25. Notice that we create a new Criteria object for the products property, add restrictions to the products’ criteria we just created, and then obtain the results from the supplier Criteria object:
Criteria crit = session.createCriteria(Supplier.class);
Criteria prdCrit = crit.createCriteria("products");
prdCrit.add(Restrictions.gt("price",25.0));
List results = crit.list();
Going the other way, we obtain all the products from the supplier MegaInc using many-to-one associations:
Criteria crit = session.createCriteria(Product.class);
Criteria suppCrit = crit.createCriteria("supplier");
suppCrit.add(Restrictions.eq("name","Hardware Are We"));
List results = crit.list();

Adding Projections and Aggregates

Instead of working with objects from the result set, you can treat the results from the result set as a set of rows and columns, also known as a projection of the data. This is similar to how you would use data from a SELECT query with JDBC.
To use projections, start by getting the org.hibernate.criterion.Projection object you need from theorg.hibernate.criterion.Projections factory class. The Projections class is similar to the Restrictions class in that it provides several static factory methods for obtaining Projection instances. After you get a Projection object, add it to your Criteria object with the setProjection() method. When the Criteria object executes, the list contains object references that you can cast to the appropriate type.
Example 1 : Single Aggregate ( Getting Row Count )
Criteria crit = session.createCriteria(Product.class);
crit.setProjection(Projections.rowCount());
List results = crit.list();
Other aggregate functions available through the Projections factory class include the following:
  1. avg(String propertyName): Gives the average of a property’s value
  2. count(String propertyName): Counts the number of times a property occurs
  3. countDistinct(String propertyName): Counts the number of unique values the property contains
  4. max(String propertyName): Calculates the maximum value of the property values
  5. min(String propertyName): Calculates the minimum value of the property values
  6. sum(String propertyName): Calculates the sum total of the property values
Example 2 : Multiple Aggregates
We can apply more than one projection to a given Criteria object. To add multiple projections, get a projection list from theprojectionList() method on the Projections class. The org.hibernate.criterion.ProjectionList object has anadd() method that takes a Projection object. You can pass the projections list to the setProjection() method on theCriteria object because ProjectionList implements the Projection interface.
Criteria crit = session.createCriteria(Product.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.max("price"));
projList.add(Projections.min("price"));
projList.add(Projections.avg("price"));
projList.add(Projections.countDistinct("description"));
crit.setProjection(projList);
List results = crit.list();
Example 3 : Getting Selected Columns
Another use of projections is to retrieve individual properties, rather than entities. For instance, we can retrieve just the name and description from our product table, instead of loading the entire object representation into memory.
Criteria crit = session.createCriteria(Product.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("name"));
projList.add(Projections.property("description"));
crit.setProjection(projList);
crit.addOrder(Order.asc("price"));
List results = crit.list();

Query By Example (QBE)

In QBE, instead of programmatically building a Criteria object with Criterion objects and logical expressions, you can partially populate an instance of the object. You use this instance as a template and have Hibernate build the criteria for you based upon its values. This keeps your code clean and makes your project easier to test.
For instance, if we have a user database, we can construct an instance of a user object, set the property values for type and creation date, and then use the Criteria API to run a QBE query. Hibernate will return a result set containing all user objects that match the property values that were set. Behind the scenes, Hibernate inspects the Example object and constructs an SQL fragment that corresponds to the properties on the Example object.
The following basic example searches for suppliers that match the name on the example Supplier object:
Criteria crit = session.createCriteria(Supplier.class);
Supplier supplier = new Supplier();
supplier.setName("MegaInc");
crit.add(Example.create(supplier));
List results = crit.list();

Summary

Using the Criteria API is an excellent way to get started developing with HQL. The developers of Hibernate have provided a clean API for adding restrictions to queries with Java objects. Although HQL isn’t too difficult to learn, some developers prefer the Criteria Query API, as it offers compile-time syntax checking—although column names and other schema-dependent information cannot be checked until run time.
Happy Learning !!

My Profile

My photo
can be reached at 09916017317