All Products
Search
Document Center

DataWorks:Find IP geolocation by using MaxCompute UDFs

Last Updated:Mar 26, 2026

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:

  1. Prepare the IP database — create a MaxCompute table and upload an offline IP database file.

  2. Develop a UDF — write a Python UDF that converts a dotted-decimal IP string to a decimal integer for range matching.

  3. Query with SQL — call the UDF in a SQL WHERE clause to look up the geolocation of any IP address.

Prerequisites

Before you begin, ensure that you have:

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

  1. 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.

  2. On the Data Studio page, click the image icon in the navigation pane to open Workspace Directories.

  3. Click the image icon and select Create Node > MaxCompute > MaxCompute SQL.

  4. Enter a name and click OK.

  5. 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
    );
  6. In the Select DataSource dropdown, select the MaxCompute computing resource you bound.

  7. Click Running Duration and verify that the table is created.

Upload data to the table

  1. Click the image icon in the upper-left corner. In the page that appears, click All Products > Data Integration > Upload and Download.

  2. Click the image icon in the left navigation pane.

  3. Click Upload Data and configure the following parameters:

    If the ipresource table does not appear in the Destination Table list, go to Data Map > My Data > Refresh Table Metadata and manually refresh the metadata for odps.<project_name>.ipresource.
    Parameter Value
    Specify Data to Be Uploaded > Select File Upload the ipdata.csv file
    Configure 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 ipresource table fields
  4. 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

  1. On the Data Studio page, click the Resource Management icon image.

  2. Click the image icon and select Create Resource > MaxCompute Python.

  3. In the dialog box, enter a name such as mc.py and click OK.

  4. Upload the mc.py file to File Content and set Data Source to the computing resource you bound.

  5. In the toolbar, click Save and then Publish.

Create a MaxCompute function

  1. Click the image icon and select Create Function > MaxCompute Function.

  2. In the dialog box, enter a name such as ip2int and click OK. This name is the UDF name used in SQL and cannot be changed after creation.

  3. 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.IPtoInt
    Resources Select the mc.py resource from the previous step
  4. 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.

What's next