在Hive SQL中爆炸列表以识别空格

时间:2019-03-15 16:12:18

标签: hive hiveql

我在配置单元表中有一列名为part_nos_list的列作为数组<\ string>。显然,该列为空白,我想用'-'更新它。但是,一组中的42行的代码却显示为空白。我试图检查个人记录,但未成功。这是配置单元sql。这个sql这里有什么问题吗

 while (true) { 
            Random rnd = new Random();
            int trust = rnd.Next(0, 100);            
            Console.WriteLine("Tell me your name");
            string name = Console.ReadLine();
            Console.WriteLine(name +" " + "is" + " " + trust + " " + "points `enter code here`trustworthy");



            if (trust <= 20)
            {
                Console.WriteLine("gtfo");
            }

            if (trust >= 21 && trust <= 50)
            {
                Console.WriteLine("not gonna tell you anything");
            }

            if (trust >= 50 && trust <= 70)
            {
                Console.WriteLine("not that trustworthy");
            }

            if (trust >= 71 && trust <= 90)
            {
                Console.WriteLine("quite trustworthy");
            }

            if (trust >= 91 && trust <= 100)
            {
                Console.WriteLine(" you are trustworthy");
            }
            Console.ReadKey();
            Console.WriteLine("\nAgain? (y/n)");
            if (Console.ReadLine().ToLower() != "yes")
                    break;
            }

但是此按SQL分组显示的空格为42

SELECT order_id, exploded_part_nos
FROM   sales.order_detail LATERAL VIEW explode(part_no_list) part_nos AS exploded_part_nos where sale_type in ('POS', 'OTC' , 'CCC') and exploded_part_nos = ''

这是蜂巢表的样子

select * from (SELECT explo,count(*) as uni_explo_cnt 
FROM sales.order_detail 
LATERAL VIEW explode(split(concat_ws("##", part_no_list),'##')) yy AS explo where sale_type in ('POS', 'OTC' , 'CCC') group by explo order by explo asc) DD

预先感谢

1 个答案:

答案 0 :(得分:1)

要测试爆炸数组元素是否为空,请使用以下方法:

select * from(
select explode( array("OTC","POS","CCC",""))  as explo 
) s where explo=''

结果是一个空字符串:http://demo.gethue.com/hue/editor?editor=289227&type=hive

如果要标识包含空元素的数组,请使用array_contains

select * from(
select array("OTC","POS","CCC","")  as a
) s where array_contains(a,'')

结果:

["OTC","POS","CCC",""]

在此处查看测试:http://demo.gethue.com/hue/editor?editor=289234

如果要查找仅包含一个元素的数组-空字符串请使用size(array)=1 and array_contains(array,''),请参见此处:http://demo.gethue.com/hue/editor?editor=289236

但是还有一个空数组http://demo.gethue.com/hue/editor?editor=289239&type=hive 它显示与包含空元素的数组相同,但不相同: http://demo.gethue.com/hue/editor?editor=289240&type=hive

要查找空数组,请使用size()=0 示例:http://demo.gethue.com/hue/editor?editor=289241

select * from(
select array()  as a
) s where size(a)=0

返回[]

对您的数据运行所有这些查询,您将大开眼界。我认为这是空数组,在您的情况下不是空元素

空数组不是NULL,因为它仍然是零大小的数组对象:http://demo.gethue.com/hue/editor?editor=289242

select * from(
select array()  as a
) s where a is null

不返回任何行

只有更好的查询数组才不会爆炸,并使用array_containssize查找空数组和空元素。使用LATERAL VIEW 外部来生成行,即使LATERAL VIEW通常不会生成行。没有OUTER字词的横向视图可作为INNER JOIN使用,请参阅有关LATERAL VIEW OUTER

的文档