Hive Read & Write

Using the HiveCatalog and Flink’s connector to Hive, Flink can read and write from Hive data as an alternative to Hive’s batch engine. Be sure to follow the instructions to include the correct dependencies in your application. And please also note that Hive connector only works with blink planner.

Reading From Hive

Assume Hive contains a single table in its default database, named people that contains several rows.

  1. hive> show databases;
  2. OK
  3. default
  4. Time taken: 0.841 seconds, Fetched: 1 row(s)
  5. hive> show tables;
  6. OK
  7. Time taken: 0.087 seconds
  8. hive> CREATE TABLE mytable(name string, value double);
  9. OK
  10. Time taken: 0.127 seconds
  11. hive> SELECT * FROM mytable;
  12. OK
  13. Tom 4.72
  14. John 8.0
  15. Tom 24.2
  16. Bob 3.14
  17. Bob 4.72
  18. Tom 34.9
  19. Mary 4.79
  20. Tiff 2.72
  21. Bill 4.33
  22. Mary 77.7
  23. Time taken: 0.097 seconds, Fetched: 10 row(s)

With the data ready your can connect to Hive connect to an existing Hive installation and begin querying.

  1. Flink SQL> show catalogs;
  2. myhive
  3. default_catalog
  4. # ------ Set the current catalog to be 'myhive' catalog if you haven't set it in the yaml file ------
  5. Flink SQL> use catalog myhive;
  6. # ------ See all registered database in catalog 'mytable' ------
  7. Flink SQL> show databases;
  8. default
  9. # ------ See the previously registered table 'mytable' ------
  10. Flink SQL> show tables;
  11. mytable
  12. # ------ The table schema that Flink sees is the same that we created in Hive, two columns - name as string and value as double ------
  13. Flink SQL> describe mytable;
  14. root
  15. |-- name: name
  16. |-- type: STRING
  17. |-- name: value
  18. |-- type: DOUBLE
  19. # ------ Select from hive table or hive view ------
  20. Flink SQL> SELECT * FROM mytable;
  21. name value
  22. __________ __________
  23. Tom 4.72
  24. John 8.0
  25. Tom 24.2
  26. Bob 3.14
  27. Bob 4.72
  28. Tom 34.9
  29. Mary 4.79
  30. Tiff 2.72
  31. Bill 4.33
  32. Mary 77.7

Querying Hive views

If you need to query Hive views, please note:

  1. You have to use the Hive catalog as your current catalog before you can query views in that catalog. It can be done by either tableEnv.useCatalog(...) in Table API or USE CATALOG ... in SQL Client.
  2. Hive and Flink SQL have different syntax, e.g. different reserved keywords and literals. Make sure the view’s query is compatible with Flink grammar.

Writing To Hive

Similarly, data can be written into hive using an INSERT clause.

Consider there is an example table named “mytable” with two columns: name and age, in string and int type.

  1. # ------ INSERT INTO will append to the table or partition, keeping the existing data intact ------
  2. Flink SQL> INSERT INTO mytable SELECT 'Tom', 25;
  3. # ------ INSERT OVERWRITE will overwrite any existing data in the table or partition ------
  4. Flink SQL> INSERT OVERWRITE mytable SELECT 'Tom', 25;

We support partitioned table too, Consider there is a partitioned table named myparttable with four columns: name, age, my_type and my_date, in types …… my_type and my_date are the partition keys.

  1. # ------ Insert with static partition ------
  2. Flink SQL> INSERT OVERWRITE myparttable PARTITION (my_type='type_1', my_date='2019-08-08') SELECT 'Tom', 25;
  3. # ------ Insert with dynamic partition ------
  4. Flink SQL> INSERT OVERWRITE myparttable SELECT 'Tom', 25, 'type_1', '2019-08-08';
  5. # ------ Insert with static(my_type) and dynamic(my_date) partition ------
  6. Flink SQL> INSERT OVERWRITE myparttable PARTITION (my_type='type_1') SELECT 'Tom', 25, '2019-08-08';

Formats

We have tested on the following of table storage formats: text, csv, SequenceFile, ORC, and Parquet.

Optimizations

Partition Pruning

Flink uses partition pruning as a performance optimization to limits the number of files and partitions that Flink reads when querying Hive tables. When your data is partitioned, Flink only reads a subset of the partitions in a Hive table when a query matches certain filter criteria.

Projection Pushdown

Flink leverages projection pushdown to minimize data transfer between Flink and Hive tables by omitting unnecessary fields from table scans.

It is especially beneficial when a table contains many columns.

Limit Pushdown

For queries with LIMIT clause, Flink will limit the number of output records wherever possible to minimize the amount of data transferred across network.

Vectorized Optimization upon Read

Optimization is used automatically when the following conditions are met:

  • Format: ORC or Parquet.
  • Columns without complex data type, like hive types: List, Map, Struct, Union.

This feature is turned on by default. If there is a problem, you can use this config option to close Vectorized Optimization:

  1. table.exec.hive.fallback-mapred-reader=true

Source Parallelism Inference

By default, Flink infers the hive source parallelism based on the number of splits, and the number of splits is based on the number of files and the number of blocks in the files.

Flink allows you to flexibly configure the policy of parallelism inference. You can configure the following parameters in TableConfig (note that these parameters affect all sources of the job):

KeyDefaultTypeDescription
table.exec.hive.infer-source-parallelism
trueBooleanIf is true, source parallelism is inferred according to splits number. If is false, parallelism of source are set by config.
table.exec.hive.infer-source-parallelism.max
1000IntegerSets max infer parallelism for source operator.

Roadmap

We are planning and actively working on supporting features like

  • ACID tables
  • bucketed tables
  • more formats

Please reach out to the community for more feature request https://flink.apache.org/community.html#mailing-lists