Develop a Python UDF

更新时间:
复制 MD 格式

MaxCompute Studio allows you to develop, test, and publish Python user-defined functions (UDFs).

Prerequisites

Before you start, complete the following:

Develop a Python UDF

  1. In the Project pane, under the MaxCompute Studio directory, right-click scripts and select New > MaxCompute Python.
  2. In the Create new MaxCompute python class dialog box, enter a class name in the Name field, set Kind to Python UDF, and then click OK.
  3. Write the UDF code in the editor.
    from odps.udf import annotate
    @annotate("bigint,bigint->bigint")
    class Hello(object):
        def evaluate(self, arg0, arg1):
            if None in (arg0,arg1):
                return None
            return arg0+arg1

Test the UDF

MaxCompute Studio supports local testing. You can download sample data from a table and use it to run and debug the UDF locally.

  1. Right-click the Python UDF script and select RUN.
  2. On the Edit configuration page, configure the required parameters and click OK.
    • MaxCompute project: The MaxCompute project in which the UDF runs. If you have configured a project connection in Manage project connections, this field defaults to that project. You can also add other projects as prompted.
    • MaxCompute table: The source table for the UDF run. Select a table from the drop-down list in the selected MaxCompute project.
    • Table columns: The columns that the UDF uses.
    • Download Record limit: The maximum number of records to download. Default: 100.
    Note
    • If the data has already been downloaded, MaxCompute Studio does not download it again. To re-download the data, run the Tunnel command in the MaxCompute client.
    • By default, 100 records are downloaded. To test with more data, download the data by using the Tunnel command in the MaxCompute client or the table download feature in MaxCompute Studio.
    • After the download is complete, the sample data is available in the table's data file under the warehouse directory.
  3. The local run framework retrieves data from the specified columns in the data file and runs the UDF locally.
    Note Local runs use the pyou script from PyODPS. The command is pyou hello.Hello<data. After you install PyODPS, verify that the script exists by running the following command:
    • On Windows, run the ${python}/../Scripts/pyou command.
    • On macOS, run the ${python}/../pyou command.
  4. The following example shows the source code of a Python UDF. After you run the code, you can view the output in the console.
    from odps.udf import annotate
    @annotate("bigint,bigint->bigint")
    class Plus(object):
        def evaluate(self, arg0, arg1):
            if None in (arg0, arg1):
                return None
            return arg0 + arg1

Publish a Python UDF

After the Python UDF passes the tests, publish it to a production environment. For more information, see Upload and register a function.