×
Community Blog Friday Blog - Week 32 - No-code APIs with DataService Studio

Friday Blog - Week 32 - No-code APIs with DataService Studio

Learn how to quickly and easily deploy data-driven HTTP APIs from within DataWorks, without writing any code!

By Jeremy Pedersen

Well friends, it's that time of the week again. I came here to chew gum and write blogs...and I'm all outta gum.

This week, we dive into a little-known but very handy service called DataService Studio. It's part of DataWorks, but you don't need to be a big data wizard (or even a programmer) to use it.

What does it do, you ask? DataService Studio deploys APIs. It takes a data source - such as a MySQL database - and deploys an HTTP API in front of it. It does this using API Gateway, Alibaba Cloud's API service.

This lets you deploy useful data-driven APIs without writing any code, which makes it a lot easier to push data to third party systems or applications.

Ok, let's try setting up our own API!

Creating A Data API

Setting Up A Data Source

Before we can set up and use DataService Studio itself, we need to create a data source.

In this blog, I'll assume the data source is an Alibaba Cloud RDS MySQL database, running MySQL 8.0. Please follow along with this blog post to learn how to set that up and load in some example data.

Creating A DataWorks Project

Because DataService Studio is a part of DataWorks, we must first set up a DataWorks Workspace.

Typically, you will want to do this as close to your data source as possible. In my case, my RDS database (data source) is in Singapore, so I will create my DataWorks Workspace in Singapore. Follow along with the screenshots below to set up your Workspace (note the Workspace and MaxCompute project names can be anything you like):

01_dw_setup

02_dw_setup

03_dw_setup

04_dw_setup

05_dw_setup

06_dw_setup

07_dw_setup

08_dw_setup

Connecting To The Data Source

We need to add our MySQL data source to the DataWorks project, in order for DataService Studio to work properly. If DataService Studio cannot query the source database, it won't be able to respond API requests as they come in.

We can set up a new data source using the DataWorks Data Integration Console:

09_data_source

10_data_source

11_data_source

12_data_source

13_data_source

14_data_source

15_data_source

16_data_source

17_data_source

18_data_source

19_data_source

Since we're simulating a self-built database (or a database not hosted on Alibaba Cloud), you should make sure that:

  1. You have applied for a public endpoint for your RDS database.
  2. You have set up your source database's whitelist to allow traffic from DataService Studio and from DataWorks itself. The IP address ranges used by DataService Studio are here while those used by other parts of DataWorks (like Data Integration) are here.

Setting Up An API Group

Great! With our DataWorks project up and running and our data source configured, we are well on our way to getting a Data API up and running.

However, there is one more step we need to take care of before diving back into the DataWorks console: we need to create an API Group inside the API Gateway console.

Although DataService Studio can create and publish APIs, it cannot automatically create the API Group that the APIs will be published into. We need to do that step manually from the API Gateway console. Again, make sure you create the API Group in the same region where your DataWorks project is deployed. In my case, that's Singapore:

20_api_gw

21_api_gw

22_api_gw

23_api_gw

24_api_gw

25_api_gw

26_api_gw

Publishing Our API

Finally, time for the good stuff: we can now use DataService Studio to publish some APIs! Follow the instructions below to deploy an API that reads from our MySQL database's users table. Whenever an HTTP GET request comes in with a valid user_id, we will return the name, age, sex, and location of the user, in JSON format. Follow the steps below to set up the API in DataService Studio:

27_dataservice

28_dataservice

29_dataservice

30_dataservice

31_dataservice

32_dataservice

33_dataservice

34_dataservice

35_dataservice

36_dataservice

37_dataservice

38_dataservice

Pretty easy huh? We didn't even have to write any code!

Calling The API

Now that our API is published, how do we use it? If we go back to the API Gateway console, we'll be able to find all the information we need to call the API using curl, Python, or a variety of other languages. Let's start by using Python.

The first thing we need to do is make a note of the URL for our API Group:

39_api_call

40_api_call

In my case, the API endpoint is:

cc043b8a86fe4bfb97f4145436fb37ce-ap-southeast-1.alicloudapi.com

Yours will be different. Next, we need to get a copy of the Python example code for our users API:

41_api_call

42_api_call

43_api_call

Before we can get this code working, we need to change the API endpoint (#1), the "AppCode" (#2), and the user_id (#3).

#1 and #3 are relatively easy. We already know our API endpoint address and we can choose any user ID between "0" and "999" for the test (let's go with "99"):

44_api_call

But what about the AppCode? The AppCode is a signing key that is required to successfully call the API. This limits API access to only people we authorize to use our API. It's possible to turn this feature off and make the API entirely public, but I don't recommend it.

Let's go find our AppCode:

45_api_call

46_api_call

We can now plug this into our Python code:

47_api_call

Next, let's try running the code. We should get a result like this one:

48_api_call

It works! Of course, it's a raw, unformatted JSON response. If we want better-looking results, we can try passing the JSON response to a parsing program like jq, using a command like this:

python call.py | jq

It looks a bit better but the data returned is of course the same:

49_api_call

Awesome! We managed to deploy our API without writing any code, and we were able to call the API using only a couple of lines of Python, which API Gateway handily provided to us.

Now, get out there and make some APIs!

I've Got A Question!

Great! Reach out to me at jierui.pjr@alibabacloud.com and I'll do my best to answer in a future Friday Q&A blog.

You can also follow the Alibaba Cloud Academy LinkedIn Page. We'll re-post these blogs there each Friday.

Not a LinkedIn person? We're also on Twitter and YouTube.

0 1 0
Share on

JDP

71 posts | 157 followers

You may also like

Comments

JDP

71 posts | 157 followers

Related Products