Upload and reference resources

Updated at:

Resource management lets you store and manage files, such as JARs, JSON, and Python files, required during code development. This document explains how to create and reference them.

Create resource

  1. On the Dataphin homepage, click Develop in the top menu bar.

  2. To open the Create Resource dialog box:

    Select a project, click Resource, and then click the New image icon. If your project is in Dev-Prod mode, you must also select an environment.

    image

  3. In the Create Resource dialog box, configure the parameters.

    Parameter

    Description

    Type

    Select the type of file to upload. By default, the system supports file (.xls, .xlsx, .doc, .docx, .txt, .csv), archive (.zip, .tgz, .tar.gz, .tar, .jar), jar, Python, and C++ files, as well as others.

    • file: A text file. Typically used for data that a task needs to read.

    • archive: A compressed archive file. Typically used for task dependencies.

    • Python: A Python file. Typically used as a dependency for Python tasks.

    • C++: A C++ source code file. Typically used to register a UDF for an Impala task.

    Parameter

    Enter a name for the resource. The naming rules are as follows:

    • The resource name must be unique within the project and cannot start with a number.

    • The name can contain only letters, numbers, underscores (_), hyphens (-), and periods (.).

    • The name must be between 3 and 200 characters long.

    Description

    Enter a brief description for the resource, up to 1,000 characters.

    File upload

    Based on the selected type, select Upload File.

    Compute type

    Select the compute type of the resource.

    • Computing engine: When you select this option, the system uploads the resource to the computing engine bound to the project, such as MaxCompute. This is typically used to create a UDF for the computing engine. When you upload a JAR file, the system runs the add jar xxx.jar command.

    • No specific engine: When you select this option, the resource is uploaded only to Dataphin. This is typically used to upload text-based resource files, such as file and archive types.

    Select directory

    Select a directory to store the resource. If the required directory does not exist, you can create one by specifying its name and location as shown in the figure.image

  4. Click Submit.

  5. In the Submission Notes dialog box, enter your notes, up to 128 characters.

  6. Click OK and Submit to create the resource.

    Note

    If the project uses Dev-Prod mode, you must release the resource to the production environment. For more information, see Manage Release Tasks.

Reference resource

Referencing methods

  • After uploading a resource, you can copy its reference statement for use in compute tasks, as shown in the figure.

    image

  • You can also reference a resource directly in a compute task by using the @resource_reference{"Resource Name"} statement.

  • For Shell and Python tasks, you can also click Properties in the right-side panel and select your resource from the Resource list under Resource Reference.

Reference statement

The @resource_reference{"Resource Name"} statement acts as a variable for the resource's file path. For example, consider the following Python code:

@resource_reference{"dataphin.xls"} # Reference the dataphin.xls resource

# Import the xlrd module.
import xlrd
wb = xlrd.open_workbook('dataphin.xls') # Open the Excel file
sh = wb.sheet_by_name('Sheet1') # Locate the worksheet by name

# Iterate through the Excel file and print all data.
for i in range(sh.nrows):
    print(sh.row_values(i))

The @resource_reference{"dataphin.xls"} statement is equivalent to the dataphin.xls = "/path/to/dataphin.xls" statement. In your code, you can then use the resource name, dataphin.xls, as a variable to access the resource.

Download location

The resource's download location in the task runtime environment depends on the referencing method.

Referencing method

Download location

@resource_reference{"Resource Name"} statement

The resource is downloaded to the {working directory}/resources/{session ID} directory. The prefix resource. is added to the filename, resulting in resource.{Resource Name}. In the code, the resource name is replaced with the full path to this file. This method is suitable for scenarios where you need to access the resource by its path.

Properties > Resource Reference

The resource is downloaded to the task's current working directory, and its original filename is preserved. You can then use the original filename in your code to read or import the resource. For example, you can import a referenced .py file in a Python task or run an installation command that requires filename verification in a Shell task.

Note

If you reference the same resource using both methods, the resource name in your code is replaced with the file path from the Resource Reference method.