All Products
Search
Document Center

AnalyticDB:Access OSS

Last Updated:Mar 28, 2026

AnalyticDB for MySQL Spark lets you access Object Storage Service (OSS) data within the same Alibaba Cloud account or across Alibaba Cloud accounts. This topic walks you through both scenarios using a PySpark example that reads a text file from OSS.

Prerequisites

Before you begin, ensure that you have:

Choose your scenario

ScenarioRequired permissionKey configuration
Access OSS within the same Alibaba Cloud accountAliyunADBSparkProcessingDataRole on your accountNo extra parameters needed
Access OSS across Alibaba Cloud accountsAuthorization completed for the other accountAdd spark.adb.roleArn to the conf block

Step 1: Prepare data

Upload both the input file and the PySpark script to your OSS bucket before submitting a job.

  1. Create a text file named readme.txt with the following content, then upload it to your OSS bucket:

    AnalyticDB for MySQL
    Database service

    For upload instructions, see Upload objects.

  2. Create a Python file named example.py that reads the first line of readme.txt, then upload it to the same OSS bucket:

    import sys
    from pyspark.sql import SparkSession
    
    # Initialize the Spark application
    spark = SparkSession.builder.appName('OSS Example').getOrCreate()
    
    # Read the text file — the path is passed in via args
    textFile = spark.sparkContext.textFile(sys.argv[1])
    
    # Print the total line count and the first line
    print("File total lines: " + str(textFile.count()))
    print("First line is: " + textFile.first())
Important

Spark application main files (JAR packages or Python scripts) must be stored in OSS before you submit a job.

Step 2: Submit a Spark job

  1. Log on to the AnalyticDB for MySQL console. In the upper-left corner, select a region. In the left-side navigation pane, click Clusters. On the Data Lakehouse Edition tab, find your cluster and click its ID.

  2. In the left-side navigation pane, choose Job Development > Spark JAR Development.

  3. In the upper part of the editor, select a job resource group and a Spark application type. In this example, the Batch type is selected.

  4. Paste the job configuration JSON for your scenario and click Run Now.

    Access OSS data from the same account

    {
      "args": ["oss://testBucketName/data/readme.txt"],
      "name": "spark-oss-test",
      "file": "oss://testBucketName/data/example.py",
      "conf": {
        "spark.driver.resourceSpec": "small",
        "spark.executor.resourceSpec": "small",
        "spark.executor.instances": 1
      }
    }

    Access OSS data from a different account

    Add spark.adb.roleArn to the conf block. This parameter tells Spark which RAM role to assume when accessing the other account's OSS bucket.

    {
      "args": ["oss://testBucketName/data/readme.txt"],
      "name": "CrossAccount",
      "file": "oss://testBucketName/data/example.py",
      "conf": {
        "spark.adb.roleArn": "acs:ram::testAccountID:role/<testUserName>",
        "spark.driver.resourceSpec": "small",
        "spark.executor.resourceSpec": "small",
        "spark.executor.instances": 1
      }
    }

Parameters

ParameterDescription
argsArguments passed to the Spark application. Separate multiple values with commas (,). In this example, the OSS path of the input file is passed to textFile.
nameName of the Spark application.
filePath of the main file — a JAR package containing the entry class, or a Python script as the entry point. Must be an OSS path.
spark.adb.roleArnRAM role for cross-account OSS access. Format: acs:ram::<testAccountID>:role/<testUserName>, where <testAccountID> is the ID of the Alibaba Cloud account that owns the OSS bucket, and <testUserName> is the RAM role name created during cross-account authorization. See Perform authorization. Separate multiple roles with commas (,). Not required for same-account access.
confSpark configuration in key:value format, comma-separated. Accepts standard Apache Spark parameters and AnalyticDB for MySQL-specific parameters. See Spark application configuration parameters.

Verify the result

After the job runs, click Log in the Actions column on the Applications tab of the Spark JAR Development page to view the output.

For more details on the log viewer, see Spark editor.

Related topics