Expose Data to BI Tools Using OData
Introduction
Mendix apps encourage the application of a services-oriented architecture, with multiple smaller services providing APIs and user interfaces for a specific set of data and logic. Enterprises build up complete solutions by assembling these services.
One important aspect of services is that all access to data and logic is handled by the service operations. Direct access to databases used for storing the service data is discouraged because this would bypass the business rules and security handled by the service. This creates a challenge for generic reporting, data warehousing, and ETL tooling.
The OData standard is being adopted because it enables generic data access within a services-oriented architecture. OData is “an open protocol to allow the creation and consumption of queryable and interoperable RESTful APIs in a simple and standard way.". In other words, it enables tools to use any REST/OData service by providing metadata that describes the data being provided, and by standardizing the messages exchanged with the OData services.
A reporting tool like Excel or Tableau can discover what data and functionality is available in an OData service, and provide a generic way for users to build new queries for the data.
This how-to outlines how to do the following:
- Create a published OData service with resources
- Add the OData server to Tableau and Excel
- Combine resources in Tableau
- Create custom queries
Prerequisites
Before reviewing this guide, make sure you do the following:
- Create an app that includes data you want to expose. You can either have published OData services already, or follow the steps in the Creating a Published OData Service section below. For detailed steps to publishing an OData service, see the Creating an App and Publishing to the Catalog sections of Share Data Between Apps.
- Install Excel (this document uses Excel for Office 365).
- Download and install the public version of Tableau.
Creating a Published OData Service
A published OData service can be used by third-party applications to read data from a Mendix application. For detailed steps on publishing an OData service, see the Creating an App and Publishing to the Catalog sections of Share Data Between Apps. The basic steps are as follows:
-
Open Studio Pro and add a folder named OData Services to MyFirstModule.
-
Right-click the new folder and select Add other > Published OData service.
-
Enter a descriptive name for the service and click OK.
-
Under Entities, click Add:
-
In the Select Persistable Entity window, select the entity you want to expose and click Select.
Security in OData is managed by the App Security settings and the entity-level access rules. If you have already configured access rules in your app, you do not have to configure security separately for OData. -
On the Settings tab, choose As an associated id for Associations. Excel can handle the setting As a link, but Tableau does not support it.
-
Start the application.
The OData service is now ready to be consumed.
Working with Mendix Data in Excel for Office 365
- Open Excel.
- Open the DATA tab and select Get Data > From Other Sources > From OData Feed.
- Return to Studio Pro and double-click the published OData service. Copy the link in the Location field to the clipboard.
- Back in Excel, on the OData Feed dialog box, enter the copied link for the URL and click OK.
- Select the name of the published entity in the Navigator and click Load.
The data of the Mendix application is now available in Excel.
Working with Mendix Data in Tableau
In this section, we will outline the steps for adding more than one published entity so you can compare data from different data sources.
Use Case
Imagine you have an app that helps with asset management. You have a Published OData Service with persistable entities called SmartTask and Employee that expose information. The domain model looks like this:
To visualize data from your app in Tableau, follow these steps:
-
Open Tableau and select Connect > To a Server > OData:
-
Return to Studio Pro and double-click the published OData service. Copy the link in the Location field to the clipboard.
-
On the Server Connection dialog box, enter the copied OData service Location link for the Server address:
Include authentication credentials if you set them up.
-
Click Sign In to save the server connection. You should now see the data source details.
-
Click the name of the server connection and change it to a name related to your entity (in this case, SmartTasks) for readability.
-
Click Data > New Data Source and repeat steps 1–5 to add a server connection for other published OData services.
-
Open Sheet1. Under Data, click the first entity and drag a desired entity Attribute from Measures to Dimensions. In this case, click Engineers and drag ID from Measures to Dimensions:
-
Similarly, click and drag an attribute from the second entity from Measures to Dimensions. In this case, click SmartTasks and drag SmartTask_Engineer from Measures to Dimensions.
-
Go to Data > Edit Relationships… to define the relationship between the different data sources.
-
On the Relationships window, do the following:
- Select your first entity for the Primary data source. In this scenario, that is SmartTasks.
- Select your second entity for the Secondary data source. In this scenario, that is Engineers.
- Switch to Custom mapping.
- Remove any default mappings.
- Click Add… to configure a field mapping.
- Select your first entity for the Primary data source. In this scenario, that is SmartTasks.
-
In the Add/Edit Field Mapping window, select the attribute for the first entity for the Primary data source field and Entity1_Entity2 for the Secondary data source field, then click OK to save the field mapping. In this case, select ID for the Primary data source field and SmartTask_Engineer for the Secondary data source field:
-
In the Relationships window, click OK to save the relationships:
-
Select the second entity for the data source (in this case, Engineers) and drag the Name attribute from the Dimensions section to Rows:
-
Select your first published entity (in this case, SmartTasks) for the data source and do the following:
- Click the SmartTask_Engineer attribute to use it as the linking field.
- Drag Number of Records from the Measures section to Columns.
- Click the SmartTask_Engineer attribute to use it as the linking field.
You should now see a bar chart of the data.
Filtering Data With Query Parameters
By default, all data is retrieved by Tableau, but Studio Pro allows you to add filters to the query so only the desired data is being retrieved.
To filter data with query parameters, follow these steps:
-
Right-click a data source and select Edit Data Source….
-
Click the OData URL to change the connection settings.
-
Add
?$top=2
to the server URL (in order to only retrieve the first two values) and click Sign In: -
On the warning indicating that the data being used was refreshed, click OK.
-
Open Sheet1 and drag Name to Rows again. Now you should now see a bar chart with only the data of the two engineers:
-
You can combine filters by using the
&
character. Repeat steps 1–4, but now usehttp://localhost:8080/odata/Expenses/Expenses?$skip=1
as the server URL. You should now see a bar chart showing the data of 2 and 3.
These are some other query examples:
http://localhost:8080/odata/publishedservicename/v1/Engineers(7881299347898469)
http://localhost:8080/odata/publishedservicename/v1/Engineers/$count
http://localhost:8080/odata/publishedservicename/v1/Engineers?$filter=Name+eq+'Kim'
http://localhost:8080/odata/publishedservicename/v1/Engineers?$filter=Name+ne+'Kim'
http://localhost:8080/odata/publishedservicename/v1/SmartTasks?$filter=DueDate+gt+datetime'1995-01-01T00:00:00'
http://localhost:8080/odata/publishedservicename/v1/SmartTasks?$filter=Created+gt+datetime'2005-01-01T00:00:00'&$orderby=DueDate
Read More
- Consume a Complex Web Service
- Consume a Simple Web Service
- Export XML Documents
- Import Excel Documents
- Expose a Web Service