This topic describes how to use MaxCompute to query geolocations of IP addresses. To query geolocations of IP addresses, you can download an IP address geolocation library, upload the library to MaxCompute, create a user-defined function (UDF), and then execute an SQL statement.
Prerequisites
- Activate MaxCompute.
- Activate DataWorks.
- Create a DataWorks on the workflow. This example uses DataWorks simple mode. For more information, see create a workflow.
Background information
You can send an HTTP request to call the API operation provided by the IP address
geolocation library of Taobao to query the geolocation of an IP address. The API operation
returns the geolocation information in a string, as shown in the following figure.

However, HTTP requests are not allowed in MaxCompute. You can query geolocations of
IP addresses in MaxCompute by using one of the following methods:
- Execute SQL statements to download IP address data from the IP address geolocation
library of Taobao to a local computer. Then, send HTTP requests to query the data.
Note This method is inefficient. In addition, the query frequency must be less than 10 queries per second (QPS). Otherwise, query requests are rejected by the IP address geolocation library of Taobao.
- Download the IP address geolocation library to a local computer. Then, query the geolocation
information from the local library.
Note This method is inefficient and is not suitable when the data needs to be uploaded and analyzed in a data warehouse service.
- Maintain an IP address geolocation library and upload it to MaxCompute regularly.
Then, query the geolocation information in MaxCompute.
Note This method is efficient. However, you must maintain the IP address geolocation library regularly.
Download an IP address geolocation library
Upload the IP address library
Create a UDF
- Go to the DataStudio page.
- Login DataWorks console.
- In the left-side navigation pane, click Workspaces.
- In the top navigation bar, select the region where the target workspace resides. Find the target workspace and click Data Analytics in the Actions column.
- Create a Python resource.
- Create a function.
- Click the
icon in the toolbar.
- Commit the function.
- Click the
icon in the toolbar to commit the function.
- In the Commit Node dialog box, enter your comments in the Change description field.
- Click OK.
- Click the