By default, MaxCompute blocks user-defined function (UDF) access to resources inside virtual private clouds (VPCs). To enable this, establish a network connection between MaxCompute and the target VPC, then pass the connection name as a session parameter when you run the UDF.
Prerequisites
Before you begin, make sure you have:
A UDF ready to deploy. See Java UDFs or Python 2 UDFs for how to write and register a UDF
The MaxCompute client installed and configured. See Install and configure the MaxCompute client
Supported regions
The VPC connection scheme is supported only in the following regions. UDFs can access VPC resources only in these regions.
China (Beijing)
China (Shanghai)
China (Zhangjiakou)
China (Hangzhou)
China (Shenzhen)
China (Hong Kong)
Singapore
Germany (Frankfurt)
US (Virginia)
If your VPC is in a region not listed above, UDF-based VPC access is not available.
How it works
Establish a network connection between MaxCompute and your VPC in the MaxCompute console.
Download and register the telnet UDF (
udf-3.jar) to test connectivity.Run a SQL statement with
set odps.session.networklink=<networklink_name>to bind the connection, then call the UDF.
The session parameter is valid only for the current session and must be committed together with the SQL statement.
Step 1: Establish a network connection
Log in to the MaxCompute console. In the upper-left corner, select the target region. In the left navigation pane, go to Tenants > Network Connection and create a network connection to your VPC.
For detailed steps, see Access over a VPC (dedicated connection).
Step 2: Call the UDF to access VPC resources
2.1 Install and start the MaxCompute client
2.2 Register the UDF
Download udf-3.jar.
Place the JAR file in the
bindirectory of your local MaxCompute client installation, then upload it as a resource:If the JAR file is in a different directory, include the full path in the command.
add jar udf-3.jar;Register the UDF:
create FUNCTION t_telnet as 'com.ali.odps.udf.Telnet' USING 'udf-3.jar';
2.3 Run a SQL statement to call the UDF
Run the following SQL to verify connectivity. Replace testLink with the name of the network connection you created in Step 1, and replace the placeholders with the actual IP address, port number, and timeout value of your VPC resource.
set odps.sql.type.system.odps2=true;
-- Specify the network connection name. This setting applies to the current session only.
set odps.session.networklink=testLink;
-- Call the UDF to test connectivity to the VPC resource.
select t_telnet("172.16.xxx.xxx", <Port number>, <Timeout period>);Expected result: If the connection is established, the UDF returns True. If an error is returned, check whether the network connection information is correctly configured.
Troubleshooting
The UDF returns an error instead of `True`
Check that the network connection name in odps.session.networklink exactly matches the name configured in the MaxCompute console under Tenants > Network Connection.
The SET command has no effect
The set odps.session.networklink command must be submitted in the same session commit as the SQL statement that calls the UDF.
The connection times out
Verify that the IP address and port of the target resource are correct and that the resource is reachable from within the VPC. Also confirm that the network connection is correctly configured in the MaxCompute console.
What's next
To learn how network connections between MaxCompute and VPCs work, see Network connection process.
To write your own UDF logic for accessing VPC resources, see Java UDFs or Python 2 UDFs.