All Products
Search
Document Center

AnalyticDB:Access Elasticsearch data sources

Last Updated:Jul 10, 2026

This topic shows you how to use the Spark engine in AnalyticDB for MySQL to read data from an Alibaba Cloud Elasticsearch data source over an elastic network interface (ENI).

Prerequisites

Preparations

  1. In the Elasticsearch console, go to the Basic Information page and obtain the vSwitch ID.

  2. In the Elastic Compute Service (ECS) console, go to the Security Group page and obtain the security group ID of the Alibaba Cloud Elasticsearch cluster. If no security group is added, see Create a security group.

Connect to Alibaba Cloud Elasticsearch with Scala

  1. Download the JAR package that matches the version of your Alibaba Cloud Elasticsearch cluster. For the download link, see Elasticsearch Spark. This example uses Elasticsearch-spark-30_2.12-7.17.9.jar.

  2. Add the required dependencies to the dependencies section of your pom.xml file.

    <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-spark-30 -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch-spark-30_2.12</artifactId>
        <version>7.17.9</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.12</artifactId>
        <version>3.2.0</version>
        <scope>provided</scope>
    </dependency>
    Important

    Ensure the Elasticsearch-spark-30_2.12 version in the pom.xml file matches your Alibaba Cloud Elasticsearch cluster, and the Spark-core_2.12 version matches the Spark version of AnalyticDB for MySQL.

  3. Write, compile, and package the following sample code. For this example, name the output JAR file spark-example.jar.

    package org.example
    
    import org.apache.spark.sql.{SaveMode, SparkSession}
    
    object SparkEs {
      def main(args: Array[String]): Unit = {
        val spark = SparkSession.builder().getOrCreate();
    
        // Create a DataFrame.
        val columns = Seq("language","users_count")
        val data = Seq(("Java", "20000"), ("Python", "100000"), ("Scala", "3000"))
        val writeDF = spark.createDataFrame(data).toDF(columns:_*)
    
        // Write data.
        writeDF.write.format("es").mode(SaveMode.Overwrite)
        // The private endpoint of the Alibaba Cloud Elasticsearch cluster.
        .option("es.nodes", "es-cn-nwy34drji0003****.elasticsearch.aliyuncs.com")
        // The port number of the private endpoint.
        .option("es.port", "9200")
        // The username for the Alibaba Cloud Elasticsearch cluster. The username must be elastic.
        .option("es.net.http.auth.user", "elastic")
        // The password for the Alibaba Cloud Elasticsearch cluster.
        .option("es.net.http.auth.pass", "password")
        // This must be set to true when connecting to an Alibaba Cloud Elasticsearch cluster. 
        .option("es.nodes.wan.only", "true")
        // This must be set to false when connecting to an Alibaba Cloud Elasticsearch cluster.
        .option("es.nodes.discovery", "false")
        // The resource to write to, in the format /.
        .save("spark/_doc")
    
        // Read data.
        spark.read.format("es")
        // The private endpoint of the Alibaba Cloud Elasticsearch cluster.
        .option("es.nodes", "es-cn-nwy34drji0003****.elasticsearch.aliyuncs.com")
        // The port number of the private endpoint.
        .option("es.port", "9200")
        // The username for the Alibaba Cloud Elasticsearch cluster. The username must be elastic.
        .option("es.net.http.auth.user", "elastic")
        // The password for the Alibaba Cloud Elasticsearch cluster.
        .option("es.net.http.auth.pass", "password")
        // This must be set to true when connecting to an Alibaba Cloud Elasticsearch cluster. 
        .option("es.nodes.wan.only", "true")
        // This must be set to false when connecting to an Alibaba Cloud Elasticsearch cluster.
        .option("es.nodes.discovery", "false")
        // The data source to read from, in the format of <index>/<type>.
        .load("spark/_doc").show
      }
    }
  4. Upload the JAR file downloaded in Step 1 and the sample program spark-example.jar to an OSS bucket. For more information, see Upload objects.

  5. Log on to the AnalyticDB for MySQL console. In the upper-left corner of the console, select a region. In the left-side navigation pane, click Clusters. Find the cluster that you want to manage and click the cluster ID.

  6. In the left-side navigation pane, click Job Development > Spark JAR Development.

  7. Above the editor, select a job resource group and a spark application type. This topic uses the Batch type as an example.

  8. In the editor, enter the following job configuration.

    {
    
        "name": "ES-SPARK-EXAMPLE",
        "className": "org.example.SparkEs",
        "conf": {
            "spark.driver.resourceSpec": "small",
            "spark.executor.instances": 1,
            "spark.executor.resourceSpec": "small",
            "spark.adb.eni.enabled": "true",
            "spark.adb.eni.vswitchId": "vsw-bp17jqw3lrrobn6y4****",
            "spark.adb.eni.securityGroupId": "sg-bp163uxgt4zandx1****"
        },
        "file": "oss://testBucketName/spark-example.jar",
        "jars": "oss://testBucketName/Elasticsearch-spark-30_2.12-7.17.9.jar"
    }

    The following table describes the parameters.

    Parameter

    Description

    name

    The name of the Spark job.

    className

    The entry class for the Java or Scala program. This parameter is not required for Python jobs.

    conf

    The configuration settings for the Spark application, similar to open source Spark. These settings are provided as key:value pairs in the conf object. For information about configuration parameters that differ from open source Spark or are specific to AnalyticDB for MySQL, see Spark application configuration parameters.

    spark.adb.eni.enabled

    Specifies whether to enable ENI access. This setting is required to access Elasticsearch data sources from Spark in Enterprise, Basic, or Data Lakehouse Edition.

    spark.adb.eni.vswitchId

    The vSwitch ID of the Alibaba Cloud Elasticsearch cluster. For more information, see Preparations.

    spark.adb.eni.securityGroupId

    The security group ID of the Alibaba Cloud Elasticsearch cluster. For more information, see Preparations.

    file

    The OSS path of the sample program spark-example.jar.

    jars

    The OSS path to the JAR packages required by the Spark job.

  9. Click Run Now.

Connect to Alibaba Cloud Elasticsearch with PySpark

  1. Download the JAR package that matches the version of your Alibaba Cloud Elasticsearch cluster. For the download link, see Elasticsearch Spark. This example uses Elasticsearch-spark-30_2.12-7.17.9.jar.

  2. Add the required dependencies to the dependencies section of your pom.xml file.

    <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-spark-30 -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch-spark-30_2.12</artifactId>
        <version>7.17.9</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.12</artifactId>
        <version>3.2.0</version>
        <scope>provided</scope>
    </dependency>
    Important

    Ensure the Elasticsearch-spark-30_2.12 version in the pom.xml file matches your Alibaba Cloud Elasticsearch cluster, and the Spark-core_2.12 version matches the Spark version of AnalyticDB for MySQL.

  3. Write the following sample code and save it as es-spark-example.py.

    from pyspark.sql import SparkSession
    
    if __name__ == '__main__':
        spark = SparkSession \
            .builder \
            .getOrCreate()
    
        # Create a DataFrame.
        dept = [("Finance", 10),
                ("Marketing", 20),
                ("Sales", 30),
                ("IT", 40)
                ]
        deptColumns = ["dept_name", "dept_id"]
        deptDF = spark.createDataFrame(data=dept, schema=deptColumns)
        deptDF.printSchema()
        deptDF.show(truncate=False)
    
        # Write data.
        deptDF.write.format('es').mode("overwrite") \
            # The private endpoint of the Alibaba Cloud Elasticsearch cluster.
            .option('es.nodes', 'es-cn-nwy34drji0003****.elasticsearch.aliyuncs.com') \
            # The port number of the private endpoint.
            .option('es.port', '9200') \
            # The username for the Alibaba Cloud Elasticsearch cluster. The username must be elastic.
            .option('es.net.http.auth.user', 'elastic') \
            # The password for the Alibaba Cloud Elasticsearch cluster.
            .option('es.net.http.auth.pass', 'password') \
            # This must be set to true when connecting to an Alibaba Cloud Elasticsearch cluster.
            .option("es.nodes.wan.only", "true") \
            # This must be set to false when connecting to an Alibaba Cloud Elasticsearch cluster.
            .option("es.nodes.discovery", "false") \
            # The resource to write to, in the format /.
            .save("spark/_doc")
    
        # Read data.
        df = spark.read.format("es") \
            # The private endpoint of the Alibaba Cloud Elasticsearch cluster.
            .option('es.nodes', 'es-cn-nwy34drji0003****.elasticsearch.aliyuncs.com') \
            # The port number of the private endpoint.
            .option('es.port', '9200') \
            # The username for the Alibaba Cloud Elasticsearch cluster. The username must be elastic.
            .option('es.net.http.auth.user', 'elastic') \
            # The password for the Alibaba Cloud Elasticsearch cluster.
            .option('es.net.http.auth.pass', 'password') \
            # This must be set to true when connecting to an Alibaba Cloud Elasticsearch cluster. 
            .option("es.nodes.wan.only", "true") \
            # This must be set to false when connecting to an Alibaba Cloud Elasticsearch cluster.
            .option("es.nodes.discovery", "false") \
            # The data source to read from, in the format of <index>/<type>.
            .load("spark/_doc").show
                            
  4. Upload the JAR file downloaded in Step 1 and the es-spark-example.py program to an OSS bucket. For more information, see Upload objects.

  5. Log on to the AnalyticDB for MySQL console. In the upper-left corner of the console, select a region. In the left-side navigation pane, click Clusters. Find the cluster that you want to manage and click the cluster ID.

  6. In the left-side navigation pane, click Job Development > Spark JAR Development.

  7. Above the editor, select a job resource group and a spark application type. This topic uses the Batch type as an example.

  8. In the editor, enter the following job configuration.

    {
        "name": "ES-SPARK-EXAMPLE",
        "conf": {
            "spark.driver.resourceSpec": "small",
            "spark.executor.instances": 1,
            "spark.executor.resourceSpec": "small",
            "spark.adb.eni.enabled": "true",
            "spark.adb.eni.vswitchId": "vsw-bp17jqw3lrrobn6y4****",
            "spark.adb.eni.securityGroupId": "sg-bp163uxgt4zandx1****"
        },
        "file": "oss://testBucketName/es-spark-example.py",
        "jars": "oss://testBucketName/Elasticsearch-spark-30_2.12-7.17.9.jar"
    }

    The following table describes the parameters.

    Parameter

    Description

    name

    The name of the Spark job.

    conf

    The configuration settings for the Spark application, similar to open source Spark. These settings are provided as key:value pairs in the conf object. For information about configuration parameters that differ from open source Spark or are specific to AnalyticDB for MySQL, see Spark application configuration parameters.

    spark.adb.eni.enabled

    Specifies whether to enable ENI access. This setting is required to access Elasticsearch data sources from Spark in Enterprise, Basic, or Data Lakehouse Edition.

    spark.adb.eni.vswitchId

    The vSwitch ID of the Alibaba Cloud Elasticsearch cluster. For more information, see Preparations.

    spark.adb.eni.securityGroupId

    The security group ID of the Alibaba Cloud Elasticsearch cluster. For more information, see Preparations.

    file

    The OSS path of the es-spark-example.py program.

    jars

    The OSS path to the JAR packages required by the Spark job.

  9. Click Run Now.