When you create a Delta table using Spark in E-MapReduce (EMR), its metadata is automatically synchronized to the Hive Metastore. You can then query the Delta table as you would any other table, or create a Hive external table to query existing Delta data. This topic describes how to perform these queries from Hive, Presto, and Trino.
Background information
-
You can perform Data Definition Language (DDL) and Data Manipulation Language (DML) operations on a Delta table only in Spark. You can use the Hive, Presto, and Trino engines to query data in a Delta table. For more information, see Query a table created in Spark.
-
E-MapReduce (EMR) also allows you to query existing Delta data by creating a Hive external table. For more information, see Create a Hive external table.
Limitations
This topic applies to EMR 3.37.0 and later, EMR 5.3.0 and later, and EMR 4.10.0.
Query a table created in Spark
- Open the Spark CLI.
- Log on to the master node of your EMR cluster in SSH mode. For more information, see Log on to a cluster.
- Run the following command to open the Spark CLI:
spark-sql
- Create a table in Spark and query data in the table.
- Run the following command to create a Delta Lake table named delta_table in Spark:
create table delta_table (id int) using delta location "/tmp/delta_table"; - Run the following command to insert data into the table:
insert into delta_table values 0,1,2,3,4; - Run the following command to query data in the table:
select * from delta_table;The information that contains the following content is returned:2 3 4 0 1 Time taken: 1.847 seconds, Fetched 5 row(s)
- Run the following command to create a Delta Lake table named delta_table in Spark:
-
Query the Delta table data.
Query data from Hive
Important-
For the EMR 3.x series (EMR 3.37.0 and later), you must add a parameter. You can add the parameter in the EMR console or set it when you run a command. To add the parameter in the EMR console, go to the Configure page of the Hive service. On the hive-site.xml tab, add a configuration item based on your execution engine: If you use Hive on MapReduce, set the parameter name to hive.input.format and the value to io.delta.hive.HiveInputFormat. If you use Hive on Tez, set the parameter name to hive.tez.input.format and the value to io.delta.hive.HiveInputFormat. For more information about how to add component parameters, see Add component parameters.
-
If you use ADD COLUMN to add a column to a Delta table, add the new column before any partition columns to prevent data query exceptions in Hive. For more information, see ALTER TABLE.
-
Run the following command to open the Hive CLI.
hive -
Run the following command to view the Delta table data in Hive.
select * from delta_table;The following output is returned.
2 3 4 0 1 Time taken: 2.937 seconds, Fetched: 5 row(s)NoteThe output is consistent with the data inserted in Spark, indicating that Hive has successfully accessed the data in the Delta table.
Query data from Presto
-
Run the following command to open the Presto CLI.
presto --server master-1-1:8889 --catalog delta --schema default --user hadoopThe command includes the following parameters:
-
--server master-1-1:8889: The address and port of the Presto server. -
--catalog delta: The name of the data source. -
--schema default: The database to use. This example uses the default database. You can change this parameter as needed. -
--user hadoop: The authentication username.
-
-
Run the following command to view the Delta table data in Presto.
select * from delta_table;The following output is returned.
id ---- 2 3 4 0 1 (5 rows)NoteThe output is consistent with the data inserted in Spark, indicating that Presto has successfully accessed the data in the Delta table.
Query data from Trino
-
Run the following command to open the Trino CLI.
trino --server master-1-1:9090 --catalog delta --schema default --user hadoopThe command includes the following parameters:
-
--server master-1-1:9090: The address and port of the Trino server. -
--catalog delta: The name of the data source. -
--schema default: The database to use. This example uses the default database. You can change this parameter as needed. -
--user hadoop: The authentication username.
-
-
Run the following command to view the Delta table data in Trino.
select * from delta_table;The following output is returned.
id ---- 2 3 4 0 1 (5 rows)NoteThe output is consistent with the data inserted in Spark, indicating that Trino has successfully accessed the data in the Delta table.
-
Create a Hive external table
E-MapReduce (EMR) also supports querying existing Delta data by creating a Hive external table. For example:
CREATE EXTERNAL TABLE delta_tbl (name string, age int, city string)
ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
WITH SERDEPROPERTIES("path" = "/path/to/delta")
STORED AS INPUTFORMAT 'io.delta.hive.DeltaInputFormat'
OUTPUTFORMAT 'io.delta.hive.DeltaOutputFormat'
LOCATION '/path/to/delta'
TBLPROPERTIES("spark.sql.sources.provider" = "delta");
If your cluster runs an EMR 3.x version (EMR 3.37.0 or later), you can also create an external table using a Hive StorageHandler. For example:
CREATE EXTERNAL TABLE delta_tbl123 (name string, age int, city string)
STORED BY 'io.delta.hive.DeltaStorageHandler'
WITH SERDEPROPERTIES("path" = "/path/to/delta")
LOCATION '/path/to/delta'
TBLPROPERTIES("spark.sql.sources.provider" = "delta");
In the CREATE TABLE statement, setting path in SERDEPROPERTIES and spark.sql.sources.provider in TBLPROPERTIES ensures compatibility with subsequent Spark SQL queries. After you create a Delta external table using this method, you can query it from Hive, Presto, and Trino in the same way as a Delta table created in Spark. For more information, see Step 3.