Using the Gateway to build composites (Chaining API calls)ΒΆ

In this tutorial you will learn how to build composites or chains of API calls. A Gateway API allows you to invoke a query or an aggregation pipeline. When you use the gateway, the results are normally returned as an HTTP response. The Gateway can also insert the resulting documents into another collection instead and that means that you can then use that resulting collection as the input to a second API call. This tutorial build on example described in Joins in that after calling the API to run the join, it runs an aggregation pipeline on this collection to produce another result. This tutorial will use curl to invoke the APIs.

Recall that the join from Joins produces documents of the form:

{
 "_id": {
  "$oid": "52b213b38594d8a2be17c780"
  },
  "borrower": "FEDERAL DEMOCRATIC REPUBLIC OF ETHIOPIA",
  "closingdate": "2018-07-07T00:00:00Z",
  "countrycode": "ET",
  "countryname": "Federal Democratic Republic of Ethiopia",
  "countryshortname": "Ethiopia",
  "impagency": "MINISTRY OF EDUCATION",
  "totalamt": 1.3E8,
  "gdp data": {
       "_id": {
      "$oid": "531927d742b13294d2d21cd2"
      },
      "1994": 124.55028984006057,
      "1995": 133.3778456526254,
      "1996": 144.22964451211635,
      "1997": 146.75139689788517,
      "1998": 129.41342202476415,
      "1999": 121.99507364700685,
      "2000": 122.65703782705778,
      "2001": 119.00835379315724,
      "2002": 110.27641437726285,
      "2003": 117.69763222093768,
      "2004": 134.39487997738743,
      "2005": 159.96867809189058,
      "2006": 191.7685451102841,
      "2007": 236.0982162942025,
      "2008": 309.95228313748186,
      "2009": 332.33055631420217,
      "2010": 302.098004655256,
      "2011": 335.0028859670898,
      "2012": 453.56899273923506,
      "Country Code": "ETH",
      "Country Name": "Ethiopia"
     }
 }

and that it was created using an API call of the form:

https://localhost:8443/Gateway?col=projects&name=projects_join&type=find&prettyPrint=1&username=<username>&pwd=<password>&db=tweets&sdb=lmrm&host=localhost

Since we now want the results to be placed in a Gateway collection (as intermediate results) and since we want to invoke it using curl, we will instead run the API using:

$ curl --ssl -k https://localhost:8443/Gateway -d name=projects_join -d col=projects -d host=localhost -d db=tweets -d sdb=lmrm
-d username=<username> -d pwd=<password> -d type=find -d output=collection.interim1

{"output": {"inserted objects: 500}}

At this point you will have a collection called interim1 and each document in it will have the embedded structure.

Now we build an aggregation pipeline on this data structure. In this case let’s just build a simple pipeline that groups the amount spent by country but also carries through the gdp data. The pipeline shown below is very simple but this tutorial is about chaining so the step does not matter much.

_images/chaining1.jpg

Now instead of running one curl command we run two in a script or on the command line one after the other (agg_countries is the name we saved the pipeline with) remembering that we want the data as a CSV we want the data output as a CSV:

$ curl --ssl -k https://localhost:8443/Gateway -d name=projects_join -d col=projects -d host=localhost -d db=tweets -d sdb=lmrm
   -d username=<username> -d pwd=<password> -d type=find -d output=collection.interim1
   {"output": {"inserted objects: 500}}
$ curl --ssl -k https://localhost:8443/Gateway -d name=agg_countries -d col=interim1 -d host=localhost -d db=tweets -d sdb=lmrm
   -d username=<username> -d pwd=<password> -d type=agg -d output=csv
_id.__gdp_data_2005_,_id.countryshortname,count,_sum_totalamt
2146.179053106227,Guatemala,2,2.0E8
4621.364127327985,Costa Rica,1,2.0E8
1020.7903052771514,Bolivia,2,7.4E7
2821.7877499398933,Bosnia and Herzegovina,1,3.41E7
2622.949225390487,Tonga,1,1800000.0
7858.762169973125,Mexico,2,5.0E7
3582.243859957466,Namibia,1,0.0
null,Europe and Central Asia,1,0.0
804.0711094233134,Papua New Guinea,5,7300000.0
546.7768501855516,Uzbekistan,5,3.15E8
625.8537548422248,Zambia,4,1.55E8
273.7470892260562,Rwanda,3,1.6E8
7614.522057975195,Chile,1,0.0
831.1602651732469,Moldova,5,8.0E7
...

If you want the interim collection to be deleted after the second API call, use the deleteSource=true flag.

Table Of Contents

Previous topic

An Aggregation Pipeline Primer for those Familiar with SQL

Next topic

Using the Gateway to Build Dashboards and Applications

Copyright © 2013-2016 jSonar, Inc
MongoDB is a registered trademark of MongoDB Inc. Excel is a trademark of Microsoft Inc. JSON Studio is a registered trademark of jSonar Inc. All trademarks and service marks are the property of their respective owners.