MaxCompute is a closed computing environment — it cannot call external HTTP APIs to query IP geolocations in real time. This tutorial shows you how to import an offline IP database into MaxCompute and write a user-defined function (UDF) to look up IP geolocations directly in SQL.
Choose an approach
Three approaches exist for analyzing IP address geolocations in MaxCompute:
| Approach | How it works | Pros | Cons | Verdict |
|---|---|---|---|---|
| Export data for query | Export IP addresses to a local device, then call an external API in a loop | Simple | Rate-limited (e.g., 10 QPS); unsuitable for large datasets | Not recommended |
| Query a local database | Download the IP database and IP addresses locally, then match them with a local script | No rate limits | Data is separated from MaxCompute; cannot join with other MaxCompute tables | Not recommended |
| Import the IP database | Upload the IP database to a MaxCompute table, then query it with a UDF in SQL | Fully parallel; integrates with any MaxCompute table; scales to any data volume | Requires periodic manual updates to the IP database | Recommended |
The recommended approach is best suited for large-scale batch IP analysis that needs to join IP data with other MaxCompute business tables (such as order or log tables).
How it works
This tutorial covers three steps:
-
Prepare the IP database — create a MaxCompute table and upload an offline IP database file.
-
Develop a UDF — write a Python UDF that converts a dotted-decimal IP string to a decimal integer for range matching.
-
Query with SQL — call the UDF in a SQL
WHEREclause to look up the geolocation of any IP address.
Prerequisites
Before you begin, ensure that you have:
-
A DataWorks workspace in Basic mode (create a workspace)
-
A serverless resource group bound to the workspace (how to bind a serverless resource group)
-
A MaxCompute computing resource bound to the workspace with verified network connectivity (how to bind a MaxCompute computing resource)
Prepare the IP address database
Download the sample IP database: ipdata.csv.
The file uses UTF-8 encoding. The first four fields of each row define the IP range:
-
Fields 1–2: range start and end as decimal integers
-
Fields 3–4: range start and end in dotted-decimal notation (e.g.,
1.0.1.x)
Create a MaxCompute table
-
Go to the Workspaces page in the DataWorks console. In the top navigation bar, select a region. Find your workspace and choose Shortcuts > Data Studio in the Actions column.
-
On the Data Studio page, click the
icon in the navigation pane to open Workspace Directories. -
Click the
icon and select Create Node > MaxCompute > MaxCompute SQL. -
Enter a name and click OK.
-
In the node editor, enter the following SQL:
-- Drop the table if it already exists. DROP TABLE IF EXISTS ipresource; -- Create a table to store the IP address database. CREATE TABLE IF NOT EXISTS ipresource ( start_ip BIGINT, -- IP range start (decimal integer) end_ip BIGINT, -- IP range end (decimal integer) start_ip_arg STRING, -- IP range start (dotted-decimal notation, e.g., "1.0.1.x") end_ip_arg STRING, -- IP range end (dotted-decimal notation, e.g., "1.0.3.x") country STRING, -- Country name area STRING, -- Area name (usually empty or same as the country) city STRING, -- City name (may be a province for some rows) county STRING, -- District or county name isp STRING -- Internet Service Provider (ISP), e.g., China Telecom, China Unicom, China Mobile ); -
In the Select DataSource dropdown, select the MaxCompute computing resource you bound.
-
Click Running Duration and verify that the table is created.
Upload data to the table
-
Click the
icon in the upper-left corner. In the page that appears, click All Products > Data Integration > Upload and Download. -
Click the
icon in the left navigation pane. -
Click Upload Data and configure the following parameters:
If the
ipresourcetable does not appear in the Destination Table list, go to Data Map > My Data > Refresh Table Metadata and manually refresh the metadata forodps.<project_name>.ipresource.Parameter Value Specify Data to Be Uploaded > Select File Upload the ipdata.csvfileConfigure Destination Table > Compute Engine MaxCompute MaxCompute Project Name Select the target MaxCompute project Destination Table Select the ipresource table Resource Group Select the serverless resource group you prepared Preview Data of Uploaded File Click Mapping by Order to map the CSV columns to the ipresourcetable fields -
Click Upload Data.
Verify the upload
Return to the MaxCompute SQL node editor and run the following queries. If row counts and sample rows appear, the upload was successful.
-- Check the total number of rows.
SELECT COUNT(*) FROM ipresource;
-- Preview the first 10 rows.
SELECT * FROM ipresource LIMIT 10;Develop a UDF
IP ranges in the ipresource table are stored as decimal integers. To look up an IP address in SQL, you need a UDF that converts a dotted-decimal IP string — such as 192.168.1.1 — into a decimal integer using the formula:
integer = A × 2²⁴ + B × 2¹⁶ + C × 2⁸ + D
where A, B, C, and D are the four octets of the IP address. This lets you match an IP against a range with a simple >= and <= comparison.
Download the UDF source file: mc.py. The file implements the IPtoInt class, which accepts a dotted-decimal IP string and returns the corresponding decimal integer.
Create a Python resource
-
On the Data Studio page, click the Resource Management icon
. -
Click the
icon and select Create Resource > MaxCompute Python. -
In the dialog box, enter a name such as
mc.pyand click OK. -
Upload the
mc.pyfile to File Content and set Data Source to the computing resource you bound. -
In the toolbar, click Save and then Publish.
Create a MaxCompute function
-
Click the
icon and select Create Function > MaxCompute Function. -
In the dialog box, enter a name such as
ip2intand click OK. This name is the UDF name used in SQL and cannot be changed after creation. -
Configure the following parameters. For a full parameter reference, see MaxCompute resources and functions.
Parameter Value Function Type Keep the default: OTHER Data Source Select the computing resource bound to the workspace Class Name mc.IPtoIntResources Select the mc.pyresource from the previous step -
In the toolbar, click Save and then Publish.
Look up IP geolocations with SQL
Return to the MaxCompute SQL node editor and run the following query. Replace 1.0.2xx.4x with the IP address you want to look up.
-- Replace this with the actual IP address you want to query.
SELECT * FROM ipresource
WHERE ip2int('1.0.2xx.4x') >= start_ip
AND ip2int('1.0.2xx.4x') <= end_ip;
In the Select DataSource dropdown, select the MaxCompute computing resource you bound. Click Running Duration — the results show the geolocation of the IP address.