Performing Operations on a Hudi Table Using Spark SQL
Scenarios
Hudi is integrated with Spark in an MRS cluster to provide advanced data processing capabilities. This integration allows you to create Hudi tables using Spark SQL and perform efficient data operations, including insert, update, delete, and query. Hudi tables are ideal for scenarios requiring frequent updates and queries, such as real-time data analytics and data warehouse updates. They ensure data consistency and efficiency, support incremental data processing, and significantly improve processing performance and flexibility.
Specifically, common Spark SQL operations on Hudi tables include:
- Create Hudi tables: You can use Spark SQL data definition language (DDL) to define table structures, storage formats, and index types.
- Insert data: Hudi supports batch insert and append operations. Its write optimization technology ensures efficient data writes and reduces data redundancy.
- Update and delete data: Hudi supports record-level update and deletion based on primary keys, a feature often unavailable in traditional big data frameworks. You can execute these operations using Spark SQL to ensure data integrity and real-time consistency.
- Query data: Hudi tables support efficient query operations, including point queries, range queries, and aggregation queries. With the optimized indexing mechanism, Hudi can quickly locate and retrieve data, significantly improving query performance.
Prerequisites
If Kerberos authentication is enabled for the cluster, create a human-machine user on FusionInsight Manager, assign it to the hadoop and hive user groups, and set hadoop as the primary group. For details, see Creating an MRS Cluster User.
Notes and Constraints
This section applies only to MRS 3.5.0-LTS or later.
Procedure
- Download and install the Hudi client. For details, see Installing a Client.
Currently, Hudi is integrated in Spark. You only need to download the Spark client on FusionInsight Manager. For example, the client installation directory is /opt/client.
- Log in to the node where the client is installed as user root and run the following command: Navigate to the client installation directory.
cd /opt/hadoopclientLoad environment variables.
source bigdata_env
source Hudi/component_env
Perform the security authentication.
kinit Created service user
- You need to change the password of the created user, and then run the kinit command to log in to the system again.
- In normal mode (Kerberos authentication disabled), you do not need to run the kinit command.
- If multiple services are installed, run the component_env command of the source Spark and then the component_env command of the source Hudi after you run the source bigdata_env command.
- Run the spark-sql --master yarn command to start the Spark SQL.
- Run the following SQL statements to perform operations on the Hudi table:
- Create a Hudi table.
create table if not exists hudi_table2 (id int,name string,price double) using hudi options (type = 'cow',primaryKey = 'id',preCombineField = 'price');
- Insert data.
insert into hudi_table2 select 1,1,1; insert into hudi_table2 select 2,1,1;
- Update data.
update hudi_table2 set name=3 where id=1;
- Delete data.
delete from hudi_table2 where id=2;
- Query data.
select * from hudi_table2;
- Create a Hudi table.
Helpful Links
For more Hudi SQL syntax, see Hudi SQL Syntax Reference.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot