By default, MaxCompute does not allow user-defined functions (UDFs) to directly access a Virtual Private Cloud (VPC). To access resources in a VPC from a UDF, you must first establish a network connection between MaxCompute and the target VPC. This topic shows how to access VPC resources from a UDF.
Prerequisites
Ensure you meet the following conditions:
-
You have written the UDF code.
For more information about how to write and register UDFs, see Java UDFs or Python UDF.
-
You have installed the MaxCompute client.
For more information about how to install and configure the MaxCompute client, see Install and configure the MaxCompute client.
Background information
To access VPC resources from a UDF, you must create a network connection between MaxCompute and the target VPC. For more information, see Create a network connection. After creating the network connection, add the set odps.session.networklink=<networklink_name>; property before the SQL statement that calls the UDF and submit them together. networklink_name is the name of the network connection that you created.
Limits
Due to region-specific limitations, you can access VPC resources from a UDF only in the following regions: China (Beijing), China (Shanghai), China (Zhangjiakou), China (Hangzhou), China (Shenzhen), China (Hong Kong), Singapore, Germany (Frankfurt), and US (Virginia).
Step 1: Create a network connection
Log on to the MaxCompute console. In the upper-left corner, select a region. In the left-side navigation pane, choose Tenants > Network Connection to create a network connection. For detailed instructions, see Create a network connection.
Step 2: Access VPC resources via UDF
-
Register the function.
-
Download udf-3.jar.
-
Place the downloaded JAR file in the
bindirectory of your local MaxCompute client installation. Then, run the following command to upload the JAR file as a resource.NoteIf the JAR file is in a different directory, you must include the full path in the command.
add jar udf-3.jar; -
Run the following command to register the function:
create FUNCTION t_telnet as 'com.ali.odps.udf.Telnet' USING 'udf-3.jar';
-
-
Run an SQL statement to call the UDF.
set odps.sql.type.system.odps2=true; -- Specify the name of the network connection configured for the VPC. This setting is valid only for the current session. set odps.session.networklink=testLink; -- Run the SQL statement to call the UDF and access the VPC resource. select t_telnet("172.16.xxx.xxx",<port>,<timeout>);If the command returns
True, the network connection is established and you can call the UDF. If an error occurs, verify your network connection configuration.