Access Hologres from MaxCompute

Updated at:

This topic describes how to access Hologres from MaxCompute.

Background information

Hologres is an interactive analytics product from Alibaba Cloud. It supports high-concurrency real-time data writes and queries, and provides high-performance acceleration for MaxCompute data analysis without requiring data migration. By performing federated analysis on real-time data in Hologres and offline data in MaxCompute, you can create an integrated data warehouse solution for both offline and real-time workloads. The combination of MaxCompute and Hologres meets the demands of various business scenarios, such as large-scale offline analysis, real-time operational analysis, and interactive queries.

Prerequisites

Access Hologres using MaxCompute SQL external tables

  1. In the Hologres console, select the target instance and create a Hologres database named mc_db_holo. For more information, see Create a Hologres database.

  2. On the HoloWeb development page, run the following statements in the mc_db_holo database to create a Hologres table named mc_sql_holo and insert data.

    For more information, see Create a Hologres table.

    CREATE TABLE mc_sql_holo(
            id INTEGER,
            name TEXT
    );
    INSERT INTO mc_sql_holo VALUES
            (1,'zhangsan'),
            (2,'lisi'),
            (3,'wangwu')
    ;
  3. In the Resource Access Management (RAM) console, create a RAM role named AliyunOdpsHoloRole and modify its trust policy.

    For more information, see Create a RAM role. In this tutorial, the trusted entity type for the RAM role is an Alibaba Cloud account.

  4. Add the AliyunOdpsHoloRole RAM role to your Hologres instance and grant the required permissions. For more information, see Add a RAM role to a Hologres instance and grant permissions.

  5. In the MaxCompute client, run the following statement to create an external table named mc_externaltable_holo. For more information about the parameters for creating an external table, see Hologres external tables.

    create external table if not exists mc_externaltable_holo
    (
        id int ,
        name string
    )
    stored by 'com.aliyun.odps.jdbc.JdbcStorageHandler'
    with serdeproperties (
      'odps.properties.rolearn'='acs:ram::13969******5947:role/aliyunodpsholorole')
    LOCATION 'jdbc:postgresql://hgprecn-cn-2r42******-cn-hangzhou-internal.hologres.aliyuncs.com:80/mc_db_holo?currentSchema=public&useSSL=false&table=mc_sql_holo/'
    TBLPROPERTIES (
      'mcfed.mapreduce.jdbc.driver.class'='org.postgresql.Driver',
      'odps.federation.jdbc.target.db.type'='holo',
      'odps.federation.jdbc.colmapping'='id:id,name:name'
    );
  6. After the external table is created, run the following commands in the MaxCompute client to retrieve data from it. For more information about the properties used in SET operations, see SET operations.

    SET odps.sql.split.hive.bridge=true;
    SET odps.sql.hive.compatible=true;
    SELECT * FROM mc_externaltable_holo limit 10;

    The following result is returned:

    +----+----------+
    | id | name     |
    +----+----------+
    | 1  | zhangsan |
    | 2  | lisi     |
    | 3  | wangwu   |
    +----+----------+
  7. In the MaxCompute client, run the following commands to write data to the external table.

    SET odps.sql.split.hive.bridge=true;
    SET odps.sql.hive.compatible=true;
    INSERT INTO mc_externaltable_holo VALUES (4,'alice');
  8. On the HoloWeb development page, query the data in the mc_sql_holo table.

    SELECT * FROM mc_sql_holo;

Access Hologres using Spark on MaxCompute: local mode

  1. On the HoloWeb development page, run the following statement in the mc_db_holo database to create a Hologres table named mc_jdbc_holo.

    For more information, see Create a Hologres table.

    CREATE TABLE mc_jdbc_holo(
            id INTEGER,
            name TEXT
    );
  2. In your Linux environment, create a Python file named holo_local.py in the /home/pythoncode directory.

    Use the following Python script:

    from pyspark.sql import SparkSession
    spark = SparkSession \
                .builder \
                .appName("Spark_local") \
                .config("spark.eventLog.enabled","false") \
                .getOrCreate()
    jdbcDF = spark.read.format("jdbc"). \
                options(
                url='jdbc:postgresql://hgprecn-cn-2r42******-cn-hangzhou.hologres.aliyuncs.com:80/mc_db_holo',
                dbtable='mc_jdbc_holo',
                user='LTAI****************',
                password='********************',
                driver='org.postgresql.Driver').load()
    jdbcDF.printSchema()

    Parameters:

    • url: The JDBC URL used by Spark to connect using the postgresql driver.

      • hgprecn-cn-2r42******-cn-hangzhou.hologres.aliyuncs.com:80: The public endpoint of the Hologres instance. To obtain the endpoint, see View instance details.

      • mc_db_holo: The name of the Hologres database. In this tutorial, the database is named mc_db_holo.

    • dbtable: The name of the source table in Hologres. In this tutorial, the table is named mc_jdbc_holo.

    • user: The AccessKey ID of your Alibaba Cloud account or RAM user. You can obtain the AccessKey ID from the AccessKey Management page.

    • password: The AccessKey Secret that corresponds to the AccessKey ID. You can obtain the AccessKey Secret from the AccessKey Management page.

    • driver: The PostgreSQL driver. Set this parameter to org.postgresql.Driver.

  3. From any directory in your Linux environment, run the spark-submit command to submit the local job.

    spark-submit -- master local -- driver-class-path /home/postgreSQL/postgresql-42.2.16.jar -- jars /home/postgreSQL/postgresql-42.2.16.jar /home/pythoncode/holo_local.py

    Check the Spark logs. If the printed schema matches the schema of the mc_jdbc_holo table in Hologres, the connection is successful.

Access Hologres using Spark on MaxCompute: cluster mode

  1. On the HoloWeb development page, run the following statement in the mc_db_holo database to create a Hologres table named mc_jdbc_holo.

    For more information, see Create a Hologres table.

    CREATE TABLE mc_jdbc_holo(
            id INTEGER,
            name TEXT
    );
  2. In your Linux environment, create a Python file named holo_yarncluster.py in the /home/pythoncode directory.

    Use the following Python script:

    from pyspark.sql import SparkSession
    spark = SparkSession \
                .builder \
                .appName("Spark_yarn") \
                .getOrCreate()
    jdbcDF = spark.read.format("jdbc"). \
                options(
                url='jdbc:postgresql://hgprecn-cn-2r42******-cn-hangzhou-internal.hologres.aliyuncs.com:80/mc_db_holo',
                dbtable='mc_jdbc_holo',
                user='LTAI****************',
                password='********************',
                driver='org.postgresql.Driver').load()
    jdbcDF.printSchema()

    Parameters:

    • url: The JDBC URL used by Spark to connect using the postgresql driver.

      • hgprecn-cn-2r42******-cn-hangzhou-internal.hologres.aliyuncs.com:80: The classic network endpoint of the Hologres instance. To obtain the endpoint, see View instance details.

      • mc_db_holo: The name of the Hologres database. In this tutorial, the database is named mc_db_holo.

    • dbtable: The name of the source table in Hologres. In this tutorial, the table is named mc_jdbc_holo.

    • user: The AccessKey ID of your Alibaba Cloud account or RAM user. You can obtain the AccessKey ID from the AccessKey Management page.

    • password: The AccessKey Secret that corresponds to the AccessKey ID. You can obtain the AccessKey Secret from the AccessKey Management page.

    • driver: The PostgreSQL driver. Set this parameter to org.postgresql.Driver.

  3. Configure the spark-defaults.conf file in the /home/spark2.4.5/spark-2.4.5-odps0.33.2/conf directory where you unzipped the Spark on MaxCompute client.

    # Configure the following parameters.
    spark.hadoop.odps.project.name = <MaxCompute_Project_Name>
    spark.hadoop.odps.end.point = <Endpoint>
    spark.hadoop.odps.runtime.end.point = <VPC_Endpoint>
    spark.hadoop.odps.access.id = <AccessKey_ID>
    spark.hadoop.odps.access.key = <AccessKey_Secret>
    spark.hadoop.odps.cupid.trusted.services.access.list = <Hologres_Classic_Network>
    # Retain the following default configurations.
    spark.master = yarn-cluster
    spark.driver.cores = 2
    spark.driver.memory = 4g
    spark.dynamicAllocation.shuffleTracking.enabled = true
    spark.dynamicAllocation.shuffleTracking.timeout = 20s
    spark.dynamicAllocation.enabled = true
    spark.dynamicAllocation.maxExecutors = 10
    spark.dynamicAllocation.initialExecutors = 2
    spark.executor.cores = 2
    spark.executor.memory = 8g
    spark.eventLog.enabled = true
    spark.eventLog.overwrite = true
    spark.eventLog.dir = odps://admin_task_project/cupidhistory/sparkhistory
    spark.sql.catalogImplementation = hive
    spark.sql.sources.default = hive

    Parameters:

    • MaxCompute_Project_Name: The name of the MaxCompute project to access.

      This is the name of the MaxCompute project, not the workspace. You can find the project name by logging on to the MaxCompute console, switching the region in the upper-left corner, and then choosing Manage Configurations > Projects in the left-side navigation pane.

    • AccessKey_ID: The AccessKey ID that has permissions to access the target MaxCompute project.

      You can obtain the AccessKey ID from the AccessKey Management page.

    • AccessKey_Secret: The AccessKey Secret that corresponds to the AccessKey ID.

    • Endpoint: The public endpoint for the MaxCompute project's region.

      For a list of public endpoints by region, see Public endpoints.

    • VPC_Endpoint: The VPC endpoint for the MaxCompute project's region.

      For a list of VPC endpoints by region, see VPC endpoints.

    • Hologres_Classic_Network: The classic network endpoint of the Hologres instance. This parameter is required to enable network access from the MaxCompute secure sandbox to the Hologres instance. Otherwise, the MaxCompute cluster cannot access external services.

  4. From any directory in your Linux environment, run the spark-submit command to submit the job.

    spark-submit -- master yarn-cluster -- driver-class-path /home/postgreSQL/postgresql-42.2.16.jar -- jars /home/postgreSQL/postgresql-42.2.16.jar /home/pythoncode/holo_yarncluster.py

    After submission, the LogView and Spark UI JobView URLs are printed. You can use these URLs for further diagnostics.

    • The LogView URL of the job:

      22/04/07 10:10:11 INFO Client: Submitting application application_1xxx_1600131562 to ResourceManager
      22/04/07 10:10:11 INFO YarnClientImpl: post: local resource scheme: tempresource, path: /2a7de2c1e7f051b656591322941157a.jar
      22/04/07 10:10:11 INFO YarnClientImpl: post: local resource scheme: tempresource, path: /6e483606b633aaf08f4a7e6bfbe98116.zip
      22/04/07 10:10:11 INFO YarnClientImpl: post: local resource scheme: tempresource, path: /22489af297da5c56d2ce533c9dc0d259.py
      22/04/07 10:10:11 INFO YarnClientImpl: post: local resource scheme: tempresource, path: /b2d6ef4c6f6dc428b9c5bd38aacfb7f1.zip
      22/04/07 10:10:11 INFO YarnClientImpl: post: local resource scheme: tempresource, path: /b6fc4b0a15046d1168b70e10b09ca334.zip
      22/04/07 10:10:11 INFO YarnClientImpl: post: local resource scheme: tempresource, path: /b5348e4c57f3d598043621739a41ed82.zip
      22/04/07 10:10:11 INFO SubmitJobUtil: submitting CupidTask with ALIYUN type, operator: startam
      22/04/07 10:10:11 INFO YarnClientImpl: transformAppCtxAndStartAM instance id 20220407021011976ggjr01ms2
      22/04/07 10:10:12 INFO YarnClientImplUtil: logview url: http://logview.odps.aliyun.com/logview/?h=http://service.cxxx.aliyun.com/api&p=xxx&i=2022xxx
      011976ggjr01ms2&token=dTFGWXZBSkEvNW84UVdVdUhVZm5V0YzcEVVPSxPRFBTX09CTzpwNF8yODAzMTY1MzI0NjY2ODI1MTIsMTY0OTU1NjYxMix7IlN0YXRlbWVudCI6W3siQWN0aW9uIjpbIm9kcHM6UijpbIm9kcHM6UmVhZCJdLCJFZmZlY3QiOiJBbGxvdyIsIlJlc291cmNlIjpbImFjczoiOiJhY3M6b2RwczoqOioiXX1dLCJWZXJzaW9uIjoiMSJ9
      iJBbGxvdyIsIlJlc291cmNlIjpbImFjczoiOiJhY3M6b2RwczoqOioiXX1dLCJWZXJzaW9uIjoiMSJ9xxx
      22/04/07 10:10:12 INFO CupidUtil: CupidTask status changed: Ready, expecting next status: Waiting/Running/Success
    • The Spark UI JobView URL:

      client token: N/A
      diagnostics: N/A
      ApplicationMaster host: 33.xxx.xxx.2
      ApplicationMaster RPC port: 8xxx
      queue: queue
      start time: 1649297412218
      final status: SUCCEEDED
      tracking URL: http://jobview.odps.aliyun.com/proxyview/jobview/?h=http://service.cn-hangzhou.maxcompute.aliyun.com/api&p=xxx&i=2022xxx1976ggjr01ms2&t=spark&id=application_1649xxxxxxxxx9_16001315628&metaname=2022xxxx-xxxxx1976ggjr01ms2&token=cXNmL3dvZFFlемY0aTF4MVpDTCe9WczY5K2k4PSxPRFBTX09CTzpwNF8yODAzMTY1MzI0NjY2ODI1MTlsMTY0OTU1NjY4OCx7IlN0YXRlbWVudCI6W3siQWN0aW9uIjpbIM9kcHM6KiJdLCJFZmZlY3QiOiJBbGxvdyIsIlJlc291cmNlIjpbImFjczpvZHBzOio6KjoqIl19XSwiVmVyc2lvbiI6IjEifQ==
           user: user
  5. Open the LogView URL. If the job status is success, choose Job Details > master-0 > StdOut to view the result of the jdbcDF.printSchema() method.

    In the StdOut log, check that the printed schema matches the schema of the mc_jdbc_holo table in Hologres. This confirms a successful connection. You can also open the Spark UI JobView URL to view and diagnose the job.

    root
     |-- id: integer (nullable = true)
     |-- name: string (nullable = true)

Access Hologres using Spark on MaxCompute: DataWorks submission mode

  1. On the HoloWeb development page, run the following statement in the mc_db_holo database to create a Hologres table named mc_jdbc_holo.

    For more information, see Create a Hologres table.

    CREATE TABLE mc_jdbc_holo(
            id INTEGER,
            name TEXT
    );
  2. Configure the spark-defaults.conf file in the /home/spark2.4.5/spark-2.4.5-odps0.33.2/conf directory where you unzipped the Spark on MaxCompute client.

    # Configure the following parameters.
    spark.hadoop.odps.project.name = <MaxCompute_Project_Name>
    spark.hadoop.odps.end.point = <Endpoint>
    spark.hadoop.odps.runtime.end.point = <VPC_Endpoint>
    spark.hadoop.odps.access.id = <AccessKey_ID>
    spark.hadoop.odps.access.key = <AccessKey_Secret>
    spark.hadoop.odps.cupid.trusted.services.access.list = <Hologres_Classic_Network>
    # Retain the following default configurations.
    spark.master = yarn-cluster
    spark.driver.cores = 2
    spark.driver.memory = 4g
    spark.dynamicAllocation.shuffleTracking.enabled = true
    spark.dynamicAllocation.shuffleTracking.timeout = 20s
    spark.dynamicAllocation.enabled = true
    spark.dynamicAllocation.maxExecutors = 10
    spark.dynamicAllocation.initialExecutors = 2
    spark.executor.cores = 2
    spark.executor.memory = 8g
    spark.eventLog.enabled = true
    spark.eventLog.overwrite = true
    spark.eventLog.dir = odps://admin_task_project/cupidhistory/sparkhistory
    spark.sql.catalogImplementation = hive
    spark.sql.sources.default = hive

    Parameters:

    • MaxCompute_Project_Name: The name of the MaxCompute project to access.

      This is the name of the MaxCompute project, not the workspace. You can find the project name by logging on to the MaxCompute console, switching the region in the upper-left corner, and then choosing Manage Configurations > Projects in the left-side navigation pane.

    • AccessKey_ID: The AccessKey ID that has permissions to access the target MaxCompute project.

      You can obtain the AccessKey ID from the AccessKey Management page.

    • AccessKey_Secret: The AccessKey Secret that corresponds to the AccessKey ID.

    • Endpoint: The public endpoint for the MaxCompute project's region.

      For a list of public endpoints by region, see Public endpoints.

    • VPC_Endpoint: The VPC endpoint for the MaxCompute project's region.

      For a list of VPC endpoints by region, see VPC endpoints.

    • Hologres_Classic_Network: The classic network endpoint of the Hologres instance. This parameter is required to enable network access from the MaxCompute secure sandbox to the Hologres instance. Otherwise, the MaxCompute cluster cannot access external services.

  3. Log on to the DataWorks console.

  4. In the left-side navigation pane, click Workspace list.

  5. On the Workspace list page, find your target workspace and choose Shortcuts > Data Development in the Actions column.

  6. Create a PostgreSQL JDBC resource and an ODPS Spark node.

    1. In your target workflow, right-click and choose Create Resource > MaxCompute > File. In the New resource dialog box, upload the PostgreSQL JDBC JAR file and click Create.

    2. In the workflow, right-click and choose Create Resource > MaxCompute > Python. In the New resource dialog box, enter a name in the Resource name field and click Create. This tutorial uses read_holo.py as the Resource name.

    3. Write the following script in the read_holo.py file and click the 保存 icon.

      from pyspark.sql import SparkSession
      spark = SparkSession \
                  .builder \
                  .appName("Spark") \
                  .getOrCreate()
      jdbcDF = spark.read.format("jdbc"). \
                  options(
                  url='jdbc:postgresql://hgprecn-cn-2r42******-cn-hangzhou-internal.hologres.aliyuncs.com:80/mc_db_holo',
                  dbtable='mc_jdbc_holo',
                  user='LTAI****************',
                  password='********************',
                  driver='org.postgresql.Driver').load()
      jdbcDF.printSchema()

      Parameters:

      • url: The JDBC URL used by Spark to connect using the postgresql driver.

        • hgprecn-cn-2r42******-cn-hangzhou-internal.hologres.aliyuncs.com:80: The classic network endpoint of the Hologres instance. To obtain the endpoint, see View instance details.

        • mc_db_holo: The name of the Hologres database. In this tutorial, the database is named mc_db_holo.

      • dbtable: The name of the source table in Hologres. In this tutorial, the table is named mc_jdbc_holo.

      • user: The AccessKey ID of your Alibaba Cloud account or RAM user. You can obtain the AccessKey ID from the AccessKey Management page.

      • password: The AccessKey Secret that corresponds to the AccessKey ID. You can obtain the AccessKey Secret from the AccessKey Management page.

      • driver: The PostgreSQL driver. Set this parameter to org.postgresql.Driver.

    4. In the workflow, right-click and choose Create Node > ODPS Spark. In the New node dialog box, enter a node name and click OK.

    5. Configure the spark_read_holo node.

      • Key: spark.hadoop.odps.cupid.trusted.services.access.list.

      • Value: hgprecn-cn-2r42******-cn-hangzhou-internal.hologres.aliyuncs.com:80. This is the classic network endpoint of the Hologres instance.

        Note

        This parameter is required to enable network access from the MaxCompute secure sandbox to the Hologres instance. Otherwise, the MaxCompute cluster cannot access external services.

  7. On the workflow canvas, right-click the spark_read_holo > Run.

    After the job runs, the operational logs are printed. The logs contain diagnostic information for the MaxCompute job, a LogView URL, and a Spark UI JobView URL.

  8. Open the LogView URL. If the job status is success, choose Job Details > master-0 > StdOut to view the result of the jdbcDF.printSchema() method.

    In the StdOut log, check that the printed schema matches the schema of the mc_jdbc_holo table in Hologres. This confirms a successful connection. You can also open the Spark UI JobView URL to view and diagnose the job.

    root
     |-- id: integer (nullable = true)
     |-- name: string (nullable = true)