【问题标题】:Get the Filesystem Directory Sizes using Hive SQL使用 Hive SQL 获取文件系统目录大小
【发布时间】:2021-09-13 04:54:48
【问题描述】:

我每天都将我的文件系统信息加载到 Hive,我只想获取所有目录大小。

我有一张这样的桌子

Path                   Size               Date
/                        0             01-07-2021
/tmp                     0             01-07-2021
/tmp/file1               2             01-07-2021
/tmp/file2               2             01-07-2021
/tmp/dir1                0             01-07-2021
/tmp/dir1/file3          3             01-07-2021
/opt/                    0             01-07-2021
/opt/file1               2             01-07-2021
/opt/dir1                0             01-07-2021
/opt/dir1/file2          3             01-07-2021
/opt/dir2/               0             01-07-2021
/opt/dir2/file3          4             01-07-2021
...
...
...
/                        0             02-07-2021
/tmp                     0             02-07-2021
/tmp/file1               2             02-07-2021
/tmp/file2               2             02-07-2021
/tmp/dir1                0             02-07-2021
/tmp/dir1/file3          3             02-07-2021
/opt/                    0             02-07-2021
/opt/file1               2             02-07-2021
/opt/dir1                0             02-07-2021
/opt/dir1/file2          3             02-07-2021
/opt/dir2/               0             02-07-2021
/opt/dir2/file3          4             02-07-2021

我想要一个输出查询或像这样创建一个新表。

Path                   Size               Date
/                        16            01-07-2021
/tmp                     7             01-07-2021
/tmp/dir1                3             01-07-2021
/opt                     9             01-07-2021
/opt/dir1                3             01-07-2021
/opt/dir2                4             01-07-2021
...
...
...
/                        16            02-07-2021
/tmp                     7             02-07-2021
/tmp/dir1                3             02-07-2021
/opt                     9             02-07-2021
/opt/dir1                3             02-07-2021
/opt/dir2                4             02-07-2021

我是 SQL 新手,请帮助我。谢谢。

【问题讨论】:

  • 如果这是针对 hive 的,为什么还要使用mysql 标签?
  • MySQL 也可以,只要我能把这个想法翻译成 Hive。任何输入都是好的。
  • 并非 mysql 中的所有内容都转换为 hive,反之亦然。如果要求是在hive中运行,而不是在mysql中,请去掉mysql标签。

标签: sql hdfs bigdata hiveql


【解决方案1】:

我不是 hiveql 专家,但从我简要阅读的内容来看,它不能很好地支持递归查询。所以这是一种蛮力方法。

它将文件路径拆分为多个部分,然后将它们再次聚合回来,以便每个父目录的每个文件都重复。然后聚合每个共享父目录的文件。

希望有人给出更好的答案。 (另外,请原谅错别字,我在打电话……)

select sub_path, sum(size) as size, date
from
(
  select path, size, date, concat_ws('/', collect_list(b.element)) as sub_path
  from YourTable
  lateral view outer posexplode(split(path, '/')) a as pos, element
  lateral view outer posexplode(split(path, '/')) b as pos, element
  where b.pos <= a.pos
  group by path, size, date, a.pos
)
  sub_paths
group by sub_path, date

我建议单独测试子查询,以测试一小组虚拟数据(只有几个目录中的几个文件)。这将展示它在做什么,并帮助您调试任何拼写错误。

它还假设没有包含转义斜杠的混乱目录或文件名。此代码不会识别斜线已被转义,并将其视为普通斜线,表示目录结构中的新级别。

【讨论】:

  • Hive 在collect_list() 和元素的顺序方面真的很奇怪。我推测您的代码可能有效。但是,显式排序将确保生成的路径实际上是原始路径,而不是某种神秘的顺序。但是,您可以查看我的答案,该答案提供了类似但更简单的方法。
【解决方案2】:

MatBaille 是在正确的轨道上。这个想法是将目录中每个级别的每一行相乘,然后聚合。我认为更安全的方法是使用substring_index(),它的行为与在 MySQL 中一样。这确实需要生成一系列数字,使用split(spaces) hack:

select substring_index(path, pe.i) as path,
       max(case when path = substring_index(path, i) then date end) as date,
       sum(size) as total_size
from (select t.*,
             1 + length(path) - length(replace(path, '/', '')) as depth
      from t
     ) t lateral view
     posexplode(split(space(t.depth))) pe as i, x
group by substring_index(path, pe.i);

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    相关资源
    最近更新 更多