Use the Query Service API to manage query templates and query schedules
Context
In this exercise you will execute API calls to manage query templates and query schedules using a Postman collection. You will define query templates, execute regular queries and CTAS queries. A CTAS query (create table as select query) stores its resultset in an explicit dataset. While regular queries are stored in a implicit (or system generated) dataset, that is typically exported in parquet file format.
The Query Service API lets you manage non-interactive queries against the Adobe Experience Platform data-lake.
Non-interactive means that a request to execute a query will not result in an immediate response. The query will be processed and its resultset will be stored in an implicit or explicit (CTAS: create table as select) dataset.
How many product views do we have on a daily basis?
SQL
selectdate_format( timestamp , 'yyyy-MM-dd') ASDay,count(*) AS productViewsfrom demo_system_event_dataset_for_website_global_v1_1where--aepTenantId--.demoEnvironment.brandName IN ('Luma Telco', 'Citi Signal')and eventType ='commerce.productViews'group byDaylimit10;
4.7.3 Queries
Open Postman on your computer. As part of Module 3, you created a Postman environment and imported a Postman collection. Follow the instructions in Exercise 3.3.3 in case you haven't done that yet.
As part of the Postman collection you imported, you'll see a folder 3. Query Service. If you don't see this folder, please redownload the Postman collection and reimport that collection in Postman as instructed in Exercise 3.3.3.
At this moment, only the folder 1. Queries contains requests. Other requests will be added at a layer stage.
Open that folder and get to know the Query Service API calls to execute, monitor and download the query resultset.
A POST call to [/query/queries] with the following payload will trigger the execution of our query;
4.7.3.1 Create Query
Click on the request named 1.1 QS - Create Query and go to Headers. You'll then see this:
Let's focus on this header field:
Key
Value
x-sandbox-name
--module7sandbox--
You need to specify the name of the Adobe Experience Platform sandbox you're using. The header field x-sandbox-name should be --module7sandbox--.
Go the the Body section of this request. In the Body of this request, you'll see the following:
{"name" : "ldap - QS API demo - Citi Signal - Product Views Per Day","description": "ldap - QS API demo - Citi Signal - Product Views Per Day","dbName": "module7:all", "sql": "select date_format( timestamp , 'yyyy-MM-dd') AS Day, count(*) AS productViews from demo_system_event_dataset_for_website_global_v1_1 where _experienceplatform.demoEnvironment.brandName IN ('Luma Telco', 'Citi Signal') and eventType = 'commerce.productViews' group by Day limit 10"
}
Attention: please update the variable name in the below request by replacing ldap with your specific ldap.
After adding your specific ldap, the Body should look similar to this:
{"name":"vangeluw - QS API demo - Citi Signal - Product Views Per Day","description":"vangeluw - QS API demo - Citi Signal - Product Views Per Day","dbName":"module7:all", "sql": "select date_format( timestamp , 'yyyy-MM-dd') AS Day, count(*) AS productViews from demo_system_event_dataset_for_website_global_v1_1 where _experienceplatform.demoEnvironment.brandName IN ('Luma Telco', 'Citi Signal') and eventType = 'commerce.productViews' group by Day limit 10"
}
The key dbName in the above JSON body refers to the sandbox that is used in your Adobe Experience Platform instance. If you're using the PROD sandbox, the dbName should be prod:all, if you use another sandbox like for instance module7, the dbName should be equal to module7:all.
Next, click the blue Send button to create the segment and view the results of that.
When successful the POST request will return the following response:
{"isInsertInto":false,"request": {"dbName":"module7:all", "sql": "select date_format( timestamp , 'yyyy-MM-dd') AS Day, count(*) AS productViews from demo_system_event_dataset_for_website_global_v1_1 where _experienceplatform.demoEnvironment.brandName IN ('Luma Telco', 'Citi Signal') and eventType = 'commerce.productViews' group by Day limit 10",
"name":"vangeluw - QS API demo - Citi Signal - Product Views Per Day","description":"vangeluw - QS API demo - Citi Signal - Product Views Per Day" },"clientId":"5a143b5ae4aa4631a1f3b09cd051333f","state":"SUBMITTED","rowCount":0,"errors": [],"isCTAS":false,"version":1,"id":"8f0d7f25-f7aa-493b-9792-290f884a7e5b","elapsedTime":0,"updated":"2021-01-20T13:23:13.951Z","client":"API","userId":"A3392DB95FFF08EE0A495E87@techacct.adobe.com","created":"2021-01-20T13:23:13.951Z","_links": {"self": {"href":"https://platform-va7.adobe.io/data/foundation/query/queries/8f0d7f25-f7aa-493b-9792-290f884a7e5b","method":"GET" },"soft_delete": {"href":"https://platform-va7.adobe.io/data/foundation/query/queries/8f0d7f25-f7aa-493b-9792-290f884a7e5b","method":"PATCH","body":"{ \"op\": \"soft_delete\"}" },"cancel": {"href":"https://platform-va7.adobe.io/data/foundation/query/queries/8f0d7f25-f7aa-493b-9792-290f884a7e5b","method":"PATCH","body":"{ \"op\": \"cancel\"}" } }}
The current state of the query is SUBMITTED, once executed its state will become SUCCESS.
You can also lookup submitted queries via Adobe Experience Platform UI, open Adobe Experience Platform, navigate to Queries, to Log and select your query:
4.7.3.2 Get Queries
Click on the request named 1.2 QS - Get Queries and go to Headers. You'll then see this:
Let's focus on this header field:
Key
Value
x-sandbox-name
--module7sandbox--
You need to specify the name of the Adobe Experience Platform sandbox you're using. The header field x-sandbox-name should be --module7sandbox--.
Go to Params. You'll then see this:
The orderby parameter allows you to specify a sort order based on the created property. Notice the '-' sign in front of created, which means that the order in which the list of queries is returned will be using their created date in descending order. Your query should be on top of the list.
Next, click the blue Send button to create the segment and view the results of that.
When successful the request will return a response similar to the below one. The state of the response may be SUBMITTED, IN_PROGRESS or SUCCESS. It may take several minutes before the query has a SUCCESS state. You can repeat sending this request several times, until you see the SUCCESS state.
{"queries": [ {"isInsertInto":false,"request": {"dbName":"module7:all", "sql": "select date_format( timestamp , 'yyyy-MM-dd') AS Day, count(*) AS productViews from demo_system_event_dataset_for_website_global_v1_1 where _experienceplatform.demoEnvironment.brandName IN ('Luma Telco', 'Citi Signal') and eventType = 'commerce.productViews' group by Day limit 10",
"name":"vangeluw - QS API demo - Citi Signal - Product Views Per Day","description":"vangeluw - QS API demo - Citi Signal - Product Views Per Day" },"clientId":"5a143b5ae4aa4631a1f3b09cd051333f","state":"SUCCESS","rowCount":1,"errors": [],"isCTAS":false,"version":1,"id":"8f0d7f25-f7aa-493b-9792-290f884a7e5b","elapsedTime":217481,"updated":"2021-01-20T13:26:51.432Z","client":"API","userId":"A3392DB95FFF08EE0A495E87@techacct.adobe.com","created":"2021-01-20T13:23:13.951Z","_links": {"self": { "href": "https://platform-va7.adobe.io/data/foundation/query/queries/8f0d7f25-f7aa-493b-9792-290f884a7e5b",
"method":"GET" },"soft_delete": { "href": "https://platform-va7.adobe.io/data/foundation/query/queries/8f0d7f25-f7aa-493b-9792-290f884a7e5b",
"method":"PATCH","body":"{ \"op\": \"soft_delete\"}" },"referenced_datasets": [ {"id":"60080ace62c49a19490c5870", "href": "https://platform-va7.adobe.io/data/foundation/catalog/dataSets/60080ace62c49a19490c5870"
} ] } } ] },"version": 1}
When the state is SUCCESS, please continue with the next request.
4.7.3.3 Get Query Status
Click on the request named 1.3 QS - Get Query Status and go to Headers. You'll then see this:
Let's focus on this header field:
Key
Value
x-sandbox-name
--module7sandbox--
You need to specify the name of the Adobe Experience Platform sandbox you're using. The header field x-sandbox-name should be --module7sandbox--.
Next, click the blue Send button to create the segment and view the results of that.
When successful the request will return a response similar to the below one.
{"isInsertInto":false,"request": {"dbName":"module7:all", "sql": "select date_format( timestamp , 'yyyy-MM-dd') AS Day, count(*) AS productViews from demo_system_event_dataset_for_website_global_v1_1 where _experienceplatform.demoEnvironment.brandName IN ('Luma Telco', 'Citi Signal') and eventType = 'commerce.productViews' group by Day limit 10",
"name":"vangeluw - QS API demo - Citi Signal - Product Views Per Day","description":"vangeluw - QS API demo - Citi Signal - Product Views Per Day" },"clientId":"5a143b5ae4aa4631a1f3b09cd051333f","state":"SUCCESS","rowCount":1,"errors": [],"isCTAS":false,"version":1,"id":"8f0d7f25-f7aa-493b-9792-290f884a7e5b","elapsedTime":217481,"updated":"2021-01-20T13:26:51.432Z","client":"API","userId":"A3392DB95FFF08EE0A495E87@techacct.adobe.com","created":"2021-01-20T13:23:13.951Z","_links": {"self": {"href":"https://platform-va7.adobe.io/data/foundation/query/queries/8f0d7f25-f7aa-493b-9792-290f884a7e5b","method":"GET" },"soft_delete": {"href":"https://platform-va7.adobe.io/data/foundation/query/queries/8f0d7f25-f7aa-493b-9792-290f884a7e5b","method":"PATCH","body":"{ \"op\": \"soft_delete\"}" },"referenced_datasets": [ {"id":"60080ace62c49a19490c5870","href":"https://platform-va7.adobe.io/data/foundation/catalog/dataSets/60080ace62c49a19490c5870" } ] }}
When a query reaches the state of SUCCESS, the response will also indicate the number of rows retrieved by the query via the rowCount property. In our example 10 rows are returned by the query. Let's see in the next section how we can retrieve the 10 rows.
4.7.3.4 Retrieve Query Result
The SUCCESS response above includes a referenced_datasets property, which points to the implicit dataset which stores the query result. To get access to the result, we use its href or id property.
Click on the request named 1.4 QS - Get Query Result and go to Headers. You'll then see this:
Let's focus on this header field:
Key
Value
x-sandbox-name
--module7sandbox--
[!NOTE]
You need to specify the name of the Adobe Experience Platform sandbox you're using. The header field x-sandbox-name should be --module7sandbox--.
Next, click the blue Send button to create the segment and view the results of that.
The response of this request will point to the dataset files: