Upsert To Partitioned

The Tag Management will maintain the manifests and data files of the snapshot. A typical usage is creating tags daily, then you can maintain the historical data of each day for batch reading.

When using primary key tables, a non-partitioned approach is often used to maintain updates, in order to mirror and synchronize tables from upstream database tables. This allows users to query the latest data. The tradition of Hive data warehouses is not like this. Offline data warehouses require an immutable view every day to ensure the idempotence of calculations. So we created a Tag mechanism to output these views.

However, the traditional use of Hive data warehouses is more accustomed to using partitions to specify the query’s Tag, and is more accustomed to using Hive computing engines.

So, we introduce 'metastore.tag-to-partition' and 'metastore.tag-to-partition.preview' to mapping a non-partitioned primary key table to the partition table in Hive metastore, and mapping the partition field to the name of the Tag to be fully compatible with Hive.

Example for Tag to Partition

Step 1: Create table and tag in Flink SQL

Flink

  1. CREATE CATALOG my_hive WITH (
  2. 'type' = 'paimon',
  3. 'metastore' = 'hive',
  4. -- 'uri' = 'thrift://<hive-metastore-host-name>:<port>', default use 'hive.metastore.uris' in HiveConf
  5. -- 'hive-conf-dir' = '...', this is recommended in the kerberos environment
  6. -- 'hadoop-conf-dir' = '...', this is recommended in the kerberos environment
  7. -- 'warehouse' = 'hdfs:///path/to/table/store/warehouse', default use 'hive.metastore.warehouse.dir' in HiveConf
  8. );
  9. USE CATALOG my_hive;
  10. CREATE TABLE mydb.t (
  11. pk INT,
  12. col1 STRING,
  13. col2 STRING
  14. ) WITH (
  15. 'bucket' = '-1',
  16. 'metastore.tag-to-partition' = 'dt'
  17. );
  18. INSERT INTO t VALUES (1, '10', '100'), (2, '20', '200');
  19. -- create tag '2023-10-16' for snapshot 1
  20. CALL sys.create_tag('mydb.t', '2023-10-16', 1);

Step 2: Query table in Hive with Partition Pruning

Hive

  1. SHOW PARTITIONS t;
  2. /*
  3. OK
  4. dt=2023-10-16
  5. */
  6. SELECT * FROM t WHERE dt='2023-10-16';
  7. /*
  8. OK
  9. 1 10 100 2023-10-16
  10. 2 20 200 2023-10-16
  11. */

Example for Tag Preview

The above example can only query tags that have already been created, but Paimon is a real-time data lake, and you also need to query the latest data. Therefore, Paimon provides a preview feature:

Step 1: Create table and tag in Flink SQL

Flink

  1. CREATE CATALOG my_hive WITH (
  2. 'type' = 'paimon',
  3. 'metastore' = 'hive',
  4. -- 'uri' = 'thrift://<hive-metastore-host-name>:<port>', default use 'hive.metastore.uris' in HiveConf
  5. -- 'hive-conf-dir' = '...', this is recommended in the kerberos environment
  6. -- 'hadoop-conf-dir' = '...', this is recommended in the kerberos environment
  7. -- 'warehouse' = 'hdfs:///path/to/table/store/warehouse', default use 'hive.metastore.warehouse.dir' in HiveConf
  8. );
  9. USE CATALOG my_hive;
  10. CREATE TABLE mydb.t (
  11. pk INT,
  12. col1 STRING,
  13. col2 STRING
  14. ) WITH (
  15. 'bucket' = '-1',
  16. 'metastore.tag-to-partition' = 'dt',
  17. -- preview tag creation mode process-time
  18. -- paimon will create partitions early based on process-time
  19. 'metastore.tag-to-partition.preview' = 'process-time'
  20. );
  21. INSERT INTO t VALUES (1, '10', '100'), (2, '20', '200');
  22. -- create tag '2023-10-16' for snapshot 1
  23. CALL sys.create_tag('mydb.t', '2023-10-16', 1);
  24. -- new data in '2023-10-17'
  25. INSERT INTO t VALUES (3, '30', '300'), (4, '40', '400');
  26. -- haven't finished writing the data for '2023-10-17' yet, so there's no need to create a tag for now
  27. -- but the data is already visible for Hive

Step 2: Query table in Hive with Partition Pruning

Hive

  1. SHOW PARTITIONS t;
  2. /*
  3. OK
  4. dt=2023-10-16
  5. dt=2023-10-17
  6. */
  7. SELECT * FROM t WHERE dt='2023-10-17';
  8. -- preview tag '2023-10-17'
  9. /*
  10. OK
  11. 1 10 100 2023-10-17
  12. 2 20 200 2023-10-17
  13. 3 30 300 2023-10-17
  14. 4 40 400 2023-10-17
  15. */