Use DataService Studio Of DataWorks To Publish APIs
1. Experiment
1.1 Knowledge points
This experiment uses DataService Studio of Alibaba Cloud DataWorks. It demonstrates how to publish APIs by using DataService Studio of DataWorks. The DataService Studio of DataWorks aims at building a centralized data service bus to help enterprises manage all internal and external APIs. You can quickly create APIs in DataService Studio based on data tables or register existing APIs for centralized management and publishing.
1.2 Experiment process
- Prepare an RDS environment.
- Create a DataWorks environment.
- Use DataService Studio to create APIs.
1.3 Cloud resources required
- ECS
- RDS
- Dataworks
- Api Gateway
1.4 Prerequisites
- If you’re using your own Alibaba Cloud account instead of the account provided by this lab to operate the experiment, please note that you’ll need to choose the same Ubuntu 16.04 operating system for your ECS in order to run the experiment smoothly.
- Before starting the experiment, please confirm that the previous experiment has been closed normally and exited.
2. Start the experiment environment
Click Start Lab in the upper right corner of the page to start the experiment.
.
After the experiment environment is successfully started, the system has deployed resources required by this experiment in the background, including the ECS instance, RDS instance, Server Load Balancer instance, and OSS bucket. An account consisting of the username and password for logging on to the Web console of Alibaba Cloud is also provided.
After the experiment environment is started and related resources are properly deployed, the experiment starts a countdown. You have two hours to perform experimental operations. After the countdown ends, the experiment stops, and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Next, use the username and password provided by the system to log on to the Web console of Alibaba Cloud and view related resources:
Go to the logon page of Alibaba Cloud console.
Fill in the RAM user name and click Next.
Fill in the RAM user password and click Log on.
After you successfully log on to the console, the following page is displayed.
3. Prepare the RDS environment
Choose Products and Services > ApsaraDB for RDS to log on to the ApsaraDB for RDS console.
You can see that an RDS instance is being created. Wait a few minutes until the status changes to Running.
Click Manage to go to the details page.
Click Configure Whitelist to configure a whitelist.
Click Modify.
Edit the whitelist, as shown in the following figure. Then, click OK.
3.2 Create an administrator account
Click Create Account, as shown in the following figure.
Configure the account and password, and then click OK, as shown in the following figure.
The account is created.
3.3 Apply for a public endpoint
Click Apply for Public Endpoint, as shown in the following figure.
Click OK.
The public endpoint is created.
3.4 Import data
Click Elastic Compute Service, as shown in the following figure.
We can see one running ECS instance in the US West 1 region. Click it to go to the ECS console as shown in the following figure.
Copy this ECS instance’s Internet IP address and remotely log on to this ECS (Ubuntu system) instance. For more information about remote logon, see [How to Remotely Log in to Alibaba Cloud’s ECS Server] (https://labex.io/questions/150).
The default account name and password of the ECS instance:
Account name: root
Password: nkYHG890..
After you log on to the ECS instance, run the following command to install the MySQL client:
apt update && apt -y install git mysql-client
Run the following command to clone a Github repository of a MySQL project:
git clone https://github.com/datacharmer/test_db.git
Run the following command to import data to your RDS database. Note: Replace YOUR-RDS-PRVIATE-ADDRESS with the private IP address of your RDS instance.
cd test_db
mysql -ulabex -pAliyun-test -hYOUR-RDS-PRVIATE-ADDRESS < employees.sql
As shown in the following figure, you can obtain the internal endpoint of the RDS instance.
The data is imported.
4. Create a DataWorks environment
4.1 Create a DataWorks workspace
In the Alibaba Cloud console, choose DataWorks.
On the Workspace List page, choose US West 1 and click Create Workspace.
Set the project name, select Standard Mode for Mode, and click Next.
Select the MaxCompute engine and click Next.
Set the Instance Display Name, and click Create Workspace.
The creation is complete.
After a while, the status will be displayed as Normal, and the creation is successful.
4.2 Add a MySQL data source
Go back to the DataWorks console and click Data Integration.
Click Data Sources.
Click Add data source.
Click MySQL.
Select Connection string mode. Note: Replace YOUR-RDS-PUBLIC-ADDRESS with the public endpoint of your RDS instance. After the test connection is successful, click Complete.
Enter the value of JDBC URL in the following format.
jdbc:mysql://YOUR-RDS-PUBLIC-ADDRESS:3306/employees
The value of YOUR-RDS-PUBLIC-ADDRESS is the public endpoint you applied for in Section 3.3.
The data source is added.
5. Use DataService Studio to create APIs
5.1 Create an API group
Refer to the figure below and go to ApiGateway.
Click Create Group.
Go back to the DataWorks console and click DataService Studio.
Create an Business process, as shown in the following figure.
Create success.
5.2 Create an API in wizard mode
Create an API in the selected API group, as shown in the following figure.
Set API Name, API Path, and Description, and then click OK, as shown in the following figure.
The API is created.
Edit the API content, as shown in the following figure.
Select the “salaries” table in the database, set the “emp_no” field as the request field, and then set the other three fields as the response fields.
Click the Save icon in the upper-left corner.
After you save the configuration, click Test in the upper-right corner.
Set the request parameter “emp_no” to 111035, and click Test.
The first API is created. After you publish the API, you can directly use it or use an application to call it.
5.3 Create an API in script mode
It is easy and intuitive to edit an API created in wizard mode, but you cannot perform multi-table queries.
If you need to perform multi-table queries, you must create APIs in script mode.
Choose Script Mode, as shown in the following figure.
Set API Name, API Path, and Description, and then click OK, as shown in the following figure.
As shown in the following figure, select a data source, copy and paste the following SQL statements to the Edit query SQL text box, and then click the Save icon in the upper-left corner.
select d.emp_no, e.first_name, e.last_name, d.from_date, d.to_date from dept_manager d, employees e where d.dept_no = (select dept_no from departments where dept_name = '${dept_name}') and e.emp_no = d.emp_no
Click Test.
Set the request parameter “dept_name” to Marketing, and click Test.
5.4 Publish APIs
Publish the API “query1”, as shown in the following figure.
Publish the API “query2”, as shown in the following figure.
Go to the API Gateway console of Alibaba Cloud.
Select the US (Silicon Valley) region.
The two APIs are published, as shown in the following figure.
An application with the same name as the DataWorks project is automatically created.
5.5 Call APIs
Use a call to API query2 an example.
Click Manage.
Click Custom Sample Code. You can see different sample code for calling the current API in different languages.
Copy the Python demo code.
Go back to the command line interface (CLI) of the ECS instance. Run vim call.py
to create a python script file, paste the copied content to the file, and then modify the three parameters in the code, as shown in the following figure.
The value of parameter 1 is the address of the current API group.
The value of parameter 2 is the AppCode of the API.
Run the following command to execute the script file.
python call.py
Finally, format the returned json string.
Reminder:
Log out your Alibaba RAM user account before you click stop to leave this lab. Otherwise you’ll encounter some issues when opening a new lab session in the same browser:
6. Experiment summary
This experiment demonstrates how to publish APIs by using DataService Studio of DataWorks. DataService Studio is connected to API Gateway and supports one-click publishing of API services to API Gateway. DataService Studio works together with API Gateway to provide a secure, stable, low-cost, and easy-to-use data sharing service. DataService Studio adopts the serverless architecture. You only need to focus on the logic of API queries instead of the infrastructures, such as the operation environment. DataService Studio provides computing resources and supports auto scaling without any operation and maintenance (O&M) costs.