Date 无法创建配置单元唯一分区

Date 无法创建配置单元唯一分区,date,hadoop,hive,hive-partitions,Date,Hadoop,Hive,Hive Partitions,我无法创建唯一的分区。当我上传数据时,它一次又一次地将所有日期创建为分区,甚至日期都是相同的 create table product_order1(id int,user_id int,amount int,product string, city string, txn_date string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'; 嗯 所用时间:0.133秒 LOAD DATA LOCAL INPATH 'txn' INT

我无法创建唯一的分区。当我上传数据时,它一次又一次地将所有日期创建为分区,甚至日期都是相同的

create table product_order1(id int,user_id int,amount int,product string, city string, txn_date string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
嗯 所用时间:0.133秒

    LOAD DATA LOCAL INPATH 'txn' INTO TABLE product_order1;
    Loading data to table oct19.product_order1
    Table oct19.product_order1 stats: [numFiles=1, totalSize=303]
OK
所用时间:0.426秒

    hive> 
    > set hive.exec.dynamic.partition = true;
    hive> 
    > set hive.exec.dynamic.partition.mode = true;

    hive> 
    > create table dyn_part(id int,user_id int,amount int,product string,city string) PARTITIONED BY(txn_date string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
OK
所用时间:0.14秒

    hive >
INSERT OVERWRITE TABLE dyn_part PARTITION(txn_date) select id,user_id,amount,product,city,txn_date from product_order1;
我收到的结果如下:

    Loading data to table oct19.dyn_part partition (txn_date=null)
     Time taken for load dynamic partitions : 944
    Loading partition {txn_date=04-02-2015}
    Loading partition {txn_date= 03-04-2015}
    Loading partition {txn_date=01-02-2015}
    Loading partition {txn_date=03-04-2015}
    Loading partition {txn_date= 01-01-2015}
    Loading partition {txn_date=01-01-2015}
    Loading partition {txn_date= 01-02-2015}
     Time taken for adding to write entity : 5
Partition oct19.dyn_part{txn_date= 01-01-2015} stats: [numFiles=1, numRows=1, totalSize=25, rawDataSize=24]
Partition oct19.dyn_part{txn_date= 01-02-2015} stats: [numFiles=1, numRows=1, totalSize=25, rawDataSize=24]
Partition oct19.dyn_part{txn_date= 03-04-2015} stats: [numFiles=1, numRows=2, totalSize=50, rawDataSize=48]
Partition oct19.dyn_part{txn_date=01-01-2015} stats: [numFiles=1, numRows=1, totalSize=26, rawDataSize=25]
Partition oct19.dyn_part{txn_date=01-02-2015} stats: [numFiles=1, numRows=1, totalSize=26, rawDataSize=25]
Partition oct19.dyn_part{txn_date=03-04-2015} stats: [numFiles=1, numRows=1, totalSize=26, rawDataSize=25]
Partition oct19.dyn_part{txn_date=04-02-2015} stats: [numFiles=1, numRows=1, totalSize=25, rawDataSize=24]
MapReduce Jobs Launched: 
Stage-Stage-1: Map: 1   Cumulative CPU: 4.03 sec   HDFS Read: 4166 HDFS Write: 614 SUCCESS
Total MapReduce CPU Time Spent: 4 seconds 30 msec

我注意到有些日期包含空格,有些日期没有空格:

txn_日期=2015年4月3日
txn_日期=2015年4月3日

尝试添加
修剪

INSERT OVERWRITE TABLE dyn_part PARTITION(txn_date) 
select id, user_id, amount, product, city, trim(txn_date) as txn_date 
from product_order1;
另外,最好使用与配置单元兼容的日期格式,它是可排序的

要同时格式化日期和删除空格,可以使用regexp\u replace。 如果当前格式为
MM dd yyyy
,则可以按如下方式格式化:

select regexp_replace(' 03-04-2015','.*?(\\d{2})-(\\d{2})-(\\d{4})','$3-$1-$2') --fix accordingly if it is dd-MM-yyyy. In this case it should be '$3-$2-$1' in the replacement template.
INSERT OVERWRITE TABLE dyn_part PARTITION(txn_date) 
select id, user_id, amount, product, city, 
       regexp_replace(txn_date,'.*?(\\d{2})-(\\d{2})-(\\d{4})','$3-$1-$2') as txn_date 
  from product_order1;
返回:

2015-03-04
或者像这样加载:

select regexp_replace(' 03-04-2015','.*?(\\d{2})-(\\d{2})-(\\d{4})','$3-$1-$2') --fix accordingly if it is dd-MM-yyyy. In this case it should be '$3-$2-$1' in the replacement template.
INSERT OVERWRITE TABLE dyn_part PARTITION(txn_date) 
select id, user_id, amount, product, city, 
       regexp_replace(txn_date,'.*?(\\d{2})-(\\d{2})-(\\d{4})','$3-$1-$2') as txn_date 
  from product_order1;
regexp是指:

。*?
-任何字符零次或多次

(\\d{2})
-第一组2位数字,将在替换中寻址为
$1

-
破折号

(\\d{2})
-第二组2位数字,将在替换中寻址为
$2

-
破折号

(\\d{4})
-第三组4位数字,将在替换中寻址为
$3


作为替换,
“$3-$1-$2”
我们以正确的顺序从regexp中获取组,以破折号分隔。假设3美元是年,1美元是月,2美元是日。您按正确的顺序放置组以获取
yyyy-MM-dd
,因为无法理解您使用的格式:
MM-dd-yyyy
dd-MM-yyyy

谢谢。我用过修剪,效果很好。“您能用您告诉我要使用的参数解释一下regexp_replace函数吗?”Priyanka解释道。regexp_replace将日期转换为yyyy-MM-dd的正确格式。对于您当前的日期,无法使用带有>或@Priyanka的谓词。当前,您的日期不可排序和比较。例如,2018年1月10日大于2019年1月1日,这是错误的