分区表上的hive增量

emeijp43  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(459)

我正致力于在配置单元表a上实现增量过程;表a-已在配置单元中创建,已在yearmonth(yyyymm列)上分区,卷已满。
我们正在计划从源代码导入更新/插入,并在配置单元增量表中捕获;
如下图所示,增量表表示新的更新与分区有关(201804/201611/201705)。
对于增量过程,我计划
从原始表中选择3个受影响的分区。
insert into delta2从yyyymm所在的表中选择yyyymm(从delta中选择不同的yyyymm);
将增量表中的这3个分区与原始表中相应的分区合并我可以按照horton works的4步策略应用更新)

Merge Delta2 + Delta : = new 3 partitions.

从原始表中删除3个分区

Alter Table Drop partitions 201804 / 201611 / 201705

将新合并的分区添加回原始表(具有新的更新)
我需要自动化这个脚本-你能不能建议如何把上面的逻辑放到HiveQL或spark中-专门识别分区并从原始表中删除它们。

ecr0jaav

ecr0jaav1#

您可以使用pyspark构建解决方案。我用一些基本的例子来解释这个方法。您可以根据业务需要重新修改。
假设您在配置下面的配置单元中有一个分区表。

CREATE TABLE IF NOT EXISTS udb.emp_partition_Load_tbl (
 emp_id                 smallint
,emp_name               VARCHAR(30)
,emp_city               VARCHAR(10)
,emp_dept               VARCHAR(30)
,emp_salary             BIGINT
)
PARTITIONED BY (Year String, Month String)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
STORED AS ORC;

您得到了一些csv文件,其中包含一些输入记录,您希望将这些记录加载到分区表中

1|vikrant singh rana|Gurgaon|Information Technology|20000

dataframe = spark.read.format("com.databricks.spark.csv") \
  .option("mode", "DROPMALFORMED") \
  .option("header", "false") \
  .option("inferschema", "true") \
  .schema(userschema) \
  .option("delimiter", "|").load("file:///filelocation/userinput")

newdf = dataframe.withColumn('year', lit('2018')).withColumn('month',lit('01'))

+------+------------------+--------+----------------------+----------+----+-----+
|emp-id|emp-name          |emp-city|emp-department        |emp-salary|year|month|
+------+------------------+--------+----------------------+----------+----+-----+
|1     |vikrant singh rana|Gurgaon |Information Technology|20000     |2018|01   |
+------+------------------+--------+----------------------+----------+----+-----+

设置以下属性以仅覆盖特定分区数据。

spark.conf.set("spark.sql.sources.partitionOverwriteMode","dynamic")
spark.sql("set spark.hadoop.hive.exec.dynamic.partition=true");
spark.sql("set spark.hadoop.hive.exec.dynamic.partition.mode=nonstrict");

newdf.write.format('orc').mode("overwrite").insertInto('udb.emp_partition_Load_tbl')

假设您得到了另一组数据并希望插入到其他分区中

+------+--------+--------+--------------+----------+----+-----+
|emp-id|emp-name|emp-city|emp-department|emp-salary|year|month|
+------+--------+--------+--------------+----------+----+-----+
|     2|     ABC| Gurgaon|HUMAN RESOURCE|     10000|2018|   02|
+------+--------+--------+--------------+----------+----+-----+
newdf.write.format('orc').mode("overwrite").insertInto('udb.emp_partition_Load_tbl')

> show partitions udb.emp_partition_Load_tbl;
+---------------------+--+
|      partition      |
+---------------------+--+
| year=2018/month=01  |
| year=2018/month=02  |
+---------------------+--+

假设您有另一组与现有分区相关的记录。

3|XYZ|Gurgaon|HUMAN RESOURCE|80000

newdf = dataframe.withColumn('year', lit('2018')).withColumn('month',lit('02'))
+------+--------+--------+--------------+----------+----+-----+
|emp-id|emp-name|emp-city|emp-department|emp-salary|year|month|
+------+--------+--------+--------------+----------+----+-----+
|     3|     XYZ| Gurgaon|HUMAN RESOURCE|     80000|2018|   02|
+------+--------+--------+--------------+----------+----+-----+

newdf.write.format('orc').mode("overwrite").insertInto('udb.emp_partition_Load_tbl')

 select * from udb.emp_partition_Load_tbl where year ='2018' and month ='02';
+---------+-----------+-----------+-----------------+-------------+-------+--------+--+
| emp_id  | emp_name  | emp_city  |    emp_dept     | emp_salary  | year  | month  |
+---------+-----------+-----------+-----------------+-------------+-------+--------+--+
| 3       | XYZ       | Gurgaon   | HUMAN RESOURCE  | 80000       | 2018  | 02     |
| 2       | ABC       | Gurgaon   | HUMAN RESOURCE  | 10000       | 2018  | 02     |
+---------+-----------+-----------+-----------------+-------------+-------+--------+--+

您可以在下面看到其他分区数据未被触及。

> select * from udb.emp_partition_Load_tbl where year ='2018' and month ='01';

+---------+---------------------+-----------+-------------------------+-------------+-------+--------+--+
| emp_id  |      emp_name       | emp_city  |        emp_dept         | emp_salary  | year  | month  |
+---------+---------------------+-----------+-------------------------+-------------+-------+--------+--+
| 1       | vikrant singh rana  | Gurgaon   | Information Technology  | 20000       | 2018  | 01     |
+---------+---------------------+-----------+-------------------------+-------------+-------+--------+--+

相关问题