Access Hologres from MaxCompute
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.
To access Hologres by using MaxCompute SQL external tables, see Access Hologres by using MaxCompute SQL external tables.
To access Hologres by using Spark on MaxCompute, see the following topics:
Prerequisites
Your MaxCompute environment is ready.
You have activated MaxCompute and created a MaxCompute project for the Hologres external table.
For more information, see Activate MaxCompute and Create a MaxCompute project.
You have installed the MaxCompute client.
For more information, see Install and configure the MaxCompute client.
You have set up a development environment for Spark on MaxCompute.
This tutorial uses a Spark on MaxCompute development environment on Linux with the Spark 2.4.5 release package. For more information, see Set up a development environment for Spark on MaxCompute.
You have activated DataWorks.
For more information, see DataWorks Purchase guide.
You have activated Hologres and connected to HoloWeb.
For more information, see Activate Hologres and Connect to HoloWeb and run queries.
You have downloaded the PostgreSQL JDBC driver package.
This tutorial uses the
postgresql-42.2.16.jardriver package and stores the JAR file in the /home/postgreSQL directory on a Linux machine.
Access Hologres using MaxCompute SQL external tables
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.On the HoloWeb development page, run the following statements in the
mc_db_holodatabase to create a Hologres table namedmc_sql_holoand 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') ;In the Resource Access Management (RAM) console, create a RAM role named
AliyunOdpsHoloRoleand 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.
Add the
AliyunOdpsHoloRoleRAM 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.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' );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 | +----+----------+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');On the HoloWeb development page, query the data in the
mc_sql_holotable.SELECT * FROM mc_sql_holo;
Access Hologres using Spark on MaxCompute: local mode
On the HoloWeb development page, run the following statement in the
mc_db_holodatabase to create a Hologres table namedmc_jdbc_holo.For more information, see Create a Hologres table.
CREATE TABLE mc_jdbc_holo( id INTEGER, name TEXT );In your Linux environment, create a Python file named
holo_local.pyin 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
postgresqldriver.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.
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.pyCheck the Spark logs. If the printed schema matches the schema of the
mc_jdbc_holotable in Hologres, the connection is successful.
Access Hologres using Spark on MaxCompute: cluster mode
On the HoloWeb development page, run the following statement in the
mc_db_holodatabase to create a Hologres table namedmc_jdbc_holo.For more information, see Create a Hologres table.
CREATE TABLE mc_jdbc_holo( id INTEGER, name TEXT );In your Linux environment, create a Python file named
holo_yarncluster.pyin the/home/pythoncodedirectory.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.
Configure the
spark-defaults.conffile in the/home/spark2.4.5/spark-2.4.5-odps0.33.2/confdirectory 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 = hiveParameters:
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 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.
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.pyAfter 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/SuccessThe 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
Open the LogView URL. If the job status is success, choose 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_holotable 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
On the HoloWeb development page, run the following statement in the
mc_db_holodatabase to create a Hologres table namedmc_jdbc_holo.For more information, see Create a Hologres table.
CREATE TABLE mc_jdbc_holo( id INTEGER, name TEXT );Configure the
spark-defaults.conffile in the/home/spark2.4.5/spark-2.4.5-odps0.33.2/confdirectory 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 = hiveParameters:
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 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.
Log on to the DataWorks console.
In the left-side navigation pane, click Workspace list.
On the Workspace list page, find your target workspace and choose Shortcuts > Data Development in the Actions column.
Create a PostgreSQL JDBC resource and an ODPS Spark node.
In your target workflow, right-click and choose . In the New resource dialog box, upload the PostgreSQL JDBC JAR file and click Create.
In the workflow, right-click and choose . In the New resource dialog box, enter a name in the Resource name field and click Create. This tutorial uses
read_holo.pyas the Resource name.Write the following script in the
read_holo.pyfile 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.
In the workflow, right-click and choose . In the New node dialog box, enter a node name and click OK.
Configure the
spark_read_holonode.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.NoteThis parameter is required to enable network access from the MaxCompute secure sandbox to the Hologres instance. Otherwise, the MaxCompute cluster cannot access external services.
On the workflow canvas, right-click the .
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.
Open the LogView URL. If the job status is success, choose 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_holotable 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)