本文转载自微信公众号《Java大数据与数据仓库》,作者柯同学。转载本文请联系Java大数据与数据仓库公众号。动态分区调整动态分区属性:设置为true开启动态分区功能(默认为false)hive.exec.dynamic.partition=true;动态分区属性:设置为nonstrict,表示允许所有分区为dynamic(默认为strict)设置为strict,表示至少有一个分区必须是statichive.exec.dynamic.partition.mode=strict;dynamicpartition属性:每个mapper或reducer可以创建的最大动态分区数hive.exec.max。dynamic.partitions.pernode=100;动态分区属性:一条动态分区创建语句最多可以创建的动态分区数hive.exec.max.dynamic.partitions=1000;动态分区属性:hive全局可创建的最大文件数.exec.max.created.files=100000;控制DataNode一次可以打开的文件数。该参数必须在DataNode的$HADOOP_HOME/conf/hdfs-site.xml文件中设置dfs.datanode.max.xcievers8192注意在Hive中,动态分区会导致数据插入时产生过多的碎片小文件。如果需要创建很多分区进行动态分区插入,用户需要写很多条件查询sql将数据插入到对应的分区中。好在Hive提供了动态分区功能,可以根据分区字段的值自动创建分区。启用上面列出的动态分区hive.exec.dynamic.partition,hive.exec.dynamic.partition.mode需要是非严格模式,通常如果分区很多,hive.exec.max.dynamic.partitions.pernode也需要设置大一些,否则会有错误提示。现在有sql:insertoverwritetableemployeespartitions(country,state)select...,se.cnty,se.stfromstaged_employeesse;可以看出Hive是根据select语句中的最后两列来确定partition字段country和state的值的,这里特意用不同的值命名是为了强调source之间的关系表字段和输出分区值是根据位置而不是名称匹配的。动态和静态分区的组合也可以用于动态和静态分区的混合。在上面的例子中,我们可以指定国家的分区值五为静态值US,分区字段state为动态值:insertoverwritetableemployeespartitions(country='US',state)select...,se.cnty,se.stfromstaged_employeesewherese.cnty='美国';注意:静态分区需要出现在动态分区字段之前。默认不启用动态分区功能,默认以严格模式执行。在这种模式下,要求至少有一列分区字段是静态的。这样做的好处是可以防止因为设计或者其他错误的查询而产生大量的partition。比如sql小子一不小心把timestamp作为分区字段,那可就惨了。每天导入一天的数据,通常指定日期为静态分区,小时为动态分区混合导入。示例createtableifnotexiststest.test(idstring,namestring)partitionedby(dtstring,hourstring)rowformatdelimitedfieldsterminatedby'\t';createtableifnotexiststest.test2(idstring,namestring)partitionedby(dtstring,hourstring)rowformatdelimitedfieldsterminatedby'\t'storedasorc;导入数据到test.test表loaddatalocalinpath'/home/hadoop/data/test.txt'intotabletest.testpartition(dt='2019-09-10',hour='02');test.txt001keguang002kg003kk004ikeguang使用动态分区insertinsertoverwritetabletest。test2partition(dt,hour)选择`(dt|hour)?+.+`,dt,hourfromtest.test;这里的(dt|hour)?+.+表示查询test表中除了dt和hour这两个字段之外的所有字段。