Introducing Mule Query Language


Working with web APIs, local APIs and different data formats and structures is too damn hard. You have to write painful verbose code to:

  • Query Web APIs and work with the data
  • Enrich and join data from external with local
  • Compose RESTful services from existing services
  • Version services and data formats
  • Merge data from different sources into a common data format
  • Sort through sets of data

We’d like to introduce an easier way that will hopefully change the way that you write applications: Mule Query Language. It’s a LINQ-inspired language that allows you to filter, join and transform data from messages, Cloud Connectors, Spring beans – or any other object – in very concise query statements.

We think that the best way to illustrate this is a few examples. While you look, imagine how many more lines of code this would take if you didn’t have MQL.

The Basics

MQL statements allow you to query, transform, and join data. Let’s take a look at a couple basic examples.

Filter objects from a Collection of user objects.

Transform an existing object into a Map/POJO. This expression creates a new Map with a name property from the firstName and lastName properties on the user object.

You can also create new POJOs from existing objects:

Join data from another source. This will call store.getPurchases and join the data into a new object for each user in the query.

Now, let’s take a look at how we can put this to use!

Query the Web

Combine the above with Cloud connectors, and you can query the web to do things like merge data from different services into a common format. This example shows two queries which merge the data from Twitter and Yammer into a common format.

Here twitter and yammer are Cloud Connectors inside of Mule. We can create new objects using the “select” statement and set properties on these new objects by referring to the yammer and twitter objects. We can then return this merged data inside of Mule in the JSON format.

Join Data Across the Web

Join information from Cloud Connectors, like Salesforce, with your local data sources:

This example shows how you can combine data from your local user data base with the company name and phone number from Salesforce.

Compose new RESTful services

Create an instant RESTful JSON service from a query of your Spring bean or Cloud Connector. This will run your query and return the result as a set of JSON objects which you can expose to customers or to your frontend:

Transform Data for Your Frontend

Here we’re retrieving a List of users from the UserManager.getUsers() method and building an object for our frontend which has additional data attached to it.

Transform your Flows

You can easily perform transformations inside your Mule flows which might otherwise require custom code by just using the select part of the query. As more and more people move to JSON based services, they commonly need a replacement for XSLT, and this can perform many of the transformations that you need to do in a much more simple fashion.

Version your RESTful or SOAP web services

Building on the above, you can easily do transformations to version your services. Here’s an example which converts a User object with an inline address to a User object that has an Address as it’s own object.

This is the perfect complement to JAX-RS or JAX-WS services inside your applications.

Query Your POJOs

Run queries inside your Java code:

Hopefully you’ll find a million other uses for it too.

To get started, check out our documentation or head over to our download page. And be sure to drop us a line letting us know how you would like to use it and your feedback.


We'd love to hear your opinion on this post

9 Responses to “Introducing Mule Query Language”

  1. Dan,

    Very interesting project!

    I am trying to query the POJOs as shown in your example. The query result collection does not contain User, instead it returns Query$1 and fails in ClassCastException when iterating the result collection. Here is the code:

    List users = new ArrayList();
    users.add(new User(“Dan”, “Diephouse”, “MuleSoft”, “Engineering”));
    users.add(new User(“Joe”, “Sales”, “MuleSoft”, “Sales”));
    Map context = new HashMap();
    context.put(“users”, users);
    Collection result = Query.execute(“from users where division = ‘Engineering'”, context);
    for(User user : result) {

    Exception in thread “main” java.lang.ClassCastException: com.mulesoft.mql.Query$1 cannot be cast to mqldemo.User
    at mqldemo.Test.main(

    Any clues what could be wrong here?


  2. Hi Arul, Very sorry for that issue that you ran into. It was a release screw up (that’s why it’s 0.9 not 1.0 :-)). I’ve pushed a new 0.9.1 release which has the Query engine fixed and modified our tests to be more strict about the results.

  3. Awesome. It works now! Thanks Dan for fixing this so quickly.

  4. Extremely interesting.

  5. MQL order by clause not actually ordering the result-set.
    i am playing with this to see , if we can use in our application.

    Any links to documentation on query sytax would be more helpful.

    List users = new ArrayList();
    users.add(new User(“Dan”, “”,2,13000 , address));
    address = new Address(“International pkway”,”Atlatna”,”GA”,”USA”);
    users.add(new User(“Joe”, “”, 1,14000, address));
    users.add(new User(“John”, “”, 1,16000, address));
    users.add(new User(“Scott”, “”, 1,15000, address));
    users.add(new User(“Andy”, “”, 1,7000, address));

    Query query = new QueryBuilder()
    // .max(3)
    .where(eq(property(“companyId”), 1))
    .set(“name”, “name”)
    .set(“income”, “income”)
    .set(“email”, “email”)).build();

    Collection result1 = query.execute(users);

  6. Got it . Looked at the mql source code. We have a bug ( typo ) in OrderByComparator class.

    Object r1 = MVEL.executeExpression(expression, o1.get(queryBuilder.getAs()), o1); Object r2 = MVEL.executeExpression(expression, o1.get(queryBuilder.getAs()), o1);

    should be Object r2 = MVEL.executeExpression(expression, o1.get(queryBuilder.getAs()), o2);

  7. Another error – when the result set is not hashmap.

    Exception in thread “main” java.lang.ClassCastException: com.mql.test.User cannot be cast to java.util.Map
    at java.util.Arrays.mergeSort(
    at java.util.Arrays.sort(
    at java.util.Collections.sort(
    at com.mulesoft.mql.Query.order(
    at com.mulesoft.mql.Query.execute(

    List persons = getPersons();

    Query query = new QueryBuilder()
    // .where(and(eq(property(“division”), “Sales”),
    // eq(property(“firstName”), “Joe”)))

    Collection result = query.execute(persons);

  8. Got it , Need couple of fixes in OrderByComparator.

  9. Hi Dan,

    MQL looks great. I was wondering it is possible to use it in MuleStudio (CE 3.3), or only with Maven.