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 services with local services
- 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 Mule 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.
from users where division = 'Engineering' and company = 'MuleSoft'
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.
from users as u select new { name = u.firstName + ' ' + u.lastName }
You can also create new POJOs from existing objects:
from users as u select new(com.acme.UserSummary) { name = u.firstName + ' ' + u.lastName }
Join data from another source. This will call store.getPurchases and join the data into a new object for each user in the query.
from users as u join store.getPurchases(u.id) as storePurchases select new { name = u.name, purchases = storePurchases }
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.