All Products
Search
Document Center

E-MapReduce:Read from and write to Hologres

Last Updated:Jul 08, 2026

The Hologres Spark Connector enables EMR Serverless Spark to connect to Hologres by adding the required configurations. This topic describes how to read from and write to Hologres in a Serverless Spark environment.

Limitations

The Spark connector requires Hologres version 1.3 or later. You can check your instance version on the Instance Details page in the Hologres console. If your instance is older than version 1.3, upgrade your instance or join the Hologres DingTalk group (ID: 32314975) to request an upgrade.

Access methods

You can access Hologres in two ways. Choose the method that best fits your needs:

Access method

Description

Scenarios

References

Method 1: Task/session-level configuration

Configure the Hologres connection information (JDBC URL, username, password, and other parameters) separately in each task or session.

  • Ad hoc data access requirements

  • Different tasks need to access different Hologres instances

  • Fine-grained access control is required for each task

This topic

Method 2: Unified configuration through data catalogs (recommended)

Add a Hologres data catalog through the Data Catalogs feature of EMR Serverless Spark. After the catalog is added, all jobs and sessions in the workspace can access the authorized data by default.

Note

Only engine version esr-4.9.0 and later is supported.

  • Frequent access to Hologres data is required

  • Multiple tasks share the same Hologres access configuration

  • Simplify task configuration and improve development efficiency

Manage data catalogs

Note

If your workspace requires long-term, frequent access to Hologres data, we recommend Method 2 (data catalogs) to reduce repetitive configuration and improve development efficiency.

Procedure

Step 1: Get and upload the hologres-connector-spark JAR

Note

EMR Serverless Spark esr-4.8.0 and later versions include the Hologres connector built-in. This step is only required if you are using a version before esr-4.8.0.

  1. To read from and write to Hologres, Spark requires a connector JAR file. You can download it from the Maven Central Repository. This topic uses version 1.5.6: hologres-connector-spark-3.x-1.5.6-jar-with-dependencies.jar.

  2. Upload the downloaded hologres-connector-spark JAR file to OSS. For instructions, see Simple upload.

Step 2: Add a network connection

  1. Get the network information.

    Go to the Hologres page and navigate to the instance details of your target Hologres instance to find its VPC and vSwitch information.

  2. Add a network connection.

    Serverless Spark requires a network connection to the Hologres cluster to access the service. For more information about network connections, see Network connectivity between EMR Serverless Spark and other VPCs.

Step 3: Create a Hologres database and table

  1. Connect to the Hologres instance. For details, see Connect to an instance.

  2. On the SQL Editor tab, enter the following SQL statements in a new temporary query and run them.

    -- Create a database.
    CREATE DATABASE testdb;
    -- Create a table.
    CREATE TABLE "public"."test" (
        "id" text  NULL,
        "name" text  NULL);
    -- Insert data.
    INSERT INTO public.test VALUES ('1001','jack'),('1002','tony'),('1003','mike');
    -- Query data.
    SELECT * FROM public.test 

    image

Step 4: Read from and write to Hologres

Example 1: SQL session

This example shows how to read from and write to Hologres by using an SQL session.

  1. Create an SQL session. For details, see Manage SQL Sessions.

    When you create the session, select the network connection that you created in the previous step from the network connection list. In the Spark Configuration section, add the following parameters to load the hologres-connector-spark.

    # Add the hologres-connector JAR file (only required for versions before esr-4.8.0).
    spark.emr.serverless.user.defined.jars oss://<bucket>/hologres-connector-spark-3.x-<version>.jar
    
    # Configure the Hologres catalog.
    spark.sql.catalog.hologres_external_test_db com.alibaba.hologres.spark3.HoloTableCatalog
    spark.sql.catalog.hologres_external_test_db.username ***
    spark.sql.catalog.hologres_external_test_db.password ***
    spark.sql.catalog.hologres_external_test_db.jdbcurl jdbc:postgresql://hgpostcn-cn-***-vpc-st.hologres.aliyuncs.com:80/testdb

    The following table describes the parameters.

    Parameter

    Example

    Description

    spark.emr.serverless.user.defined.jars

    oss://<bucket>/hologres-connector-spark-3.x-<version>.jar

    Specifies the path to the user-defined JAR file.

    spark.sql.catalog.hologres_external_test_db

    com.alibaba.hologres.spark3.HoloTableCatalog

    In Spark 3.x, this parameter is used to configure a Hologres data source as an external catalog. This is a fixed value.

    spark.sql.catalog.hologres_external_test_db.username

    LTAI******

    The AccessKey ID of your Alibaba Cloud account. We recommend that you use secrets management to handle sensitive information. For details, see Manage sensitive information by using secrets management.

    spark.sql.catalog.hologres_external_test_db.password

    mXYV******

    The AccessKey Secret of your Alibaba Cloud account. We recommend that you use secrets management to handle sensitive information. For details, see Manage sensitive information by using secrets management.

    spark.sql.catalog.hologres_external_test_db.jdbcurl

    jdbc:postgresql://hgpostcn-cn-***-vpc-st.hologres.aliyuncs.com:80/testdb

    The JDBC connection URL of the Hologres instance.

    You can customize the hologres_external_test_db part in the parameter names.

  2. On the Data Development page, create a SparkSQL job and select the SQL session that you created from the upper-right corner.

    For more information, see SparkSQL development.

  3. Copy the following code into the new SparkSQL tab and click Run.

    -- Switch to the testdb database.
    USE hologres_external_test_db;
    -- Write data.
    INSERT INTO `public`.test VALUES ('1004','tom');
    -- Query data.
    SELECT * FROM `public`.test;

    image

Example 2: Streaming job

This PySpark example shows how to read data from Kafka and write it to Hologres as a streaming job.

Note

Ensure that the network connection between Kafka and Hologres is active. We recommend that you deploy Kafka and Hologres in the same VPC and vSwitch.

  1. In this code example, replace the Kafka information and Hologres table with your actual values.

    from pyspark.sql import SparkSession
    from pyspark.sql.functions import col
    
    # Configure your Kafka information.
    servers = "alikafka-serverless-cn-xxxxx-vpc.alikafka.aliyuncs.com:9092"  # Replace with your Kafka bootstrap servers.
    topic = "topic-name"  # Replace with your Kafka topic.
    
    # Create a SparkSession.
    spark = SparkSession.builder \
        .appName("test read kafka") \
        .getOrCreate()
    
    # Read the Kafka stream.
    df = spark \
        .readStream \
        .format("kafka") \
        .option("kafka.bootstrap.servers", servers) \
        .option("subscribe", topic) \
        .load()
    
    # Define a function to write to Hologres (called for each micro-batch).
    def write_to_hologres(batch_df, batch_id):
        print(f"Writing batch {batch_id} to Hologres...")
        batch_df.write \
            .format("hologres") \
            .mode("append") \
            .insertInto("hologres_external_test_db.public.test")  # Replace with your Hologres table.
    
    # Convert the key and value to strings and write the stream.
    query = df.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)") \
        .writeStream \
        .foreachBatch(write_to_hologres) \
        .outputMode("append") \
        .trigger(processingTime='30 seconds') \
        .start()
    
    # Wait for the streaming query to terminate (this blocks cell execution in a notebook).
    query.awaitTermination()
  2. Upload the file.

    1. On the Artifacts page, click Upload File.

    2. In the Upload File dialog box, click the upload area to select the Python file from the previous step, or drag the file into the upload area.

  3. Create and run the streaming job.

    1. On the Development page, click the image (Create) icon.

    2. In the dialog box that appears, enter a Name, select PySpark from the Application (Streaming) list, and then click OK.

    3. In the new development tab, configure the following parameters and leave the others at their default settings. Then, click Publish.

      Parameter

      Description

      Main Python Resources

      Select the Python file that you uploaded in the previous step.

      Engine Version

      Select a compatible Spark version. This example uses esr-4.6.0.

      Network Connection

      Select the network connection that you created in Step 2.

      Spark Configuration

      # Add the hologres-connector JAR file (only required for versions before esr-4.8.0).
      spark.emr.serverless.user.defined.jars              oss://<bucket>/test_script/hologres-connector-spark-3.x-1.5.6-jar-with-dependencies.jar
      # Configure the Hologres catalog.
      spark.sql.catalog.hologres_external_test_db com.alibaba.hologres.spark3.HoloTableCatalog
      spark.sql.catalog.hologres_external_test_db.username ***
      spark.sql.catalog.hologres_external_test_db.password ***
      spark.sql.catalog.hologres_external_test_db.jdbcurl jdbc:postgresql://hgpostcn-cn-***-vpc-st.hologres.aliyuncs.com:80/testdb

      For a detailed description of the parameters, see Example 1: SQL session.

    4. After you publish the job, click Go to O&M. On the page that opens, click Start.

  4. Verify the result.

    1. Send messages to Kafka.

      image

    2. Query the data by using Spark SQL.image

Example 3: Notebook session

  1. Create a Notebook session. For details, see Manage Notebook Sessions.

    When you create the session, select the network connection that you created in the previous step from the network connection list. In the Spark Configuration section, add the following parameter to load the hologres-connector-spark.

    # Add the hologres-connector JAR file (only required for versions before esr-4.8.0).
    spark.emr.serverless.user.defined.jars oss://<bucket>/hologres-connector-spark-3.x-<version>.jar
  2. On the Data Development page, create a Notebook job and select the Notebook session that you created from the upper-right corner.

  3. Copy the following code into the new Notebook tab and click image.

    import pandas as pd
    from pyspark.sql import SparkSession
    from pyspark.sql.types import StructType, StructField, StringType, IntegerType, LongType
    
    # 1. Prepare a Pandas DataFrame.
    pdf = pd.DataFrame({
        "id": ["1006"],
        "name": ["sl"]
    })
    
    # 2. Convert to a PySpark DataFrame.
    # (Optional: Explicitly define the schema to ensure correct data types)
    schema = StructType([
        StructField("id", StringType(), True),
        StructField("name", StringType(), True)
    ])
    
    df = spark.createDataFrame(pdf, schema=schema)
    
    # Write to Hologres.
    df.write \
      .format("hologres") \
      .option("username", "LTAI******") \
      .option("password", "mXYV******") \
      .option("jdbcurl", "jdbc:postgresql://hgpostcn-cn-***-vpc-st.hologres.aliyuncs.com:80/testdb") \
      .option("table", "test") \
      .mode("append") \
      .save()
    
    # Read data.
    readDf = spark.read\
      .format("hologres") \
      .option("username", "LTAI******") \
      .option("password", "mXYV******") \
      .option("jdbcurl", "jdbc:postgresql://hgpostcn-cn-***-vpc-st.hologres.aliyuncs.com:80/testdb") \
      .option("table", "test") \
      .load()
    
    readDf.select("id", "name").show(10)

    The following table describes the parameters.

    Parameter

    Example

    Description

    username

    LTAI******

    The AccessKey ID of your Alibaba Cloud account. We recommend that you use secrets management to handle sensitive information. For details, see Manage sensitive information by using secrets management.

    password

    mXYV******

    The AccessKey Secret of your Alibaba Cloud account. We recommend that you use secrets management to handle sensitive information. For details, see Manage sensitive information by using secrets management.

    jdbcurl

    jdbc:postgresql://hgpostcn-cn-***-vpc-st.hologres.aliyuncs.com:80/testdb

    The JDBC connection URL of the Hologres instance.

  4. Verify the result.image

Common Hologres catalog commands

In Serverless Spark, you use a Hologres catalog to connect a Hologres database to Spark SQL as an external catalog. Each catalog is strictly bound to a single Hologres database, and cross-database access is not supported (you cannot use the same catalog to access multiple Hologres databases). The logical structure inside a catalog is consistent with Hologres:

Spark concept

Hologres concept

Description

catalog

database

For example, hologres_external_test_db maps to the testdb database in Hologres.

namespace

schema

For example, public and test_schema. The default is public. You can use USE to switch the current default namespace.

table

table

You must explicitly specify namespace.table_name (for example, public.test) or run USE namespace before referencing the table name directly.

Using a Hologres catalog

A Hologres catalog in Spark maps to exactly one Hologres database and cannot be changed after it is created.

USE hologres_external_test_db;

Listing all namespaces

In Spark, a namespace corresponds to a schema in Hologres. The default schema is public. You can use the USE command to change the default schema for a session.

-- View all namespaces in the Hologres catalog, which correspond to all schemas in Hologres.
SHOW NAMESPACES;

Listing tables in a namespace

  • List all tables.

    SHOW TABLES;
  • List tables in a specific namespace.

    USE test_schema;
    SHOW TABLES;
    
    -- Or use:
    SHOW TABLES IN test_schema;

Related documents

For more information about using Spark to read from and write to Hologres, see Read data from and write data to Hologres by using Spark.