Hive 7 資料抽樣

2021-07-08 14:03:15 字數 2950 閱讀 6333

當資料規模不斷膨脹時,我們需要找到乙個資料的子集來加快資料分析效率。因此我們就需要通過篩選和分析資料集為了進行模式 & 趨勢識別。目前來說有三種方式來進行抽樣:隨機抽樣,桶表抽樣,和塊抽樣。

1 隨機抽樣(random sampling):

select * from distribute by rand() sort by rand()

limit ;

2 桶表抽樣(bucket table sampling):

它是一種桶表進行優化的抽樣。

其中的colname值指定抽樣資料的列, 在對整行抽樣的時候可以同樣可以使用rand()函式。如果抽樣欄位跟 clustered by 字段相同,tablesample 語句會更加高效。它的語法如下所示:

select * from 

tablesample(bucket out of on [colname|rand()]) table_alias;

--示例

jdbc:hive2://> select name from employee_id_buckets

. . . . . . .> tablesample(bucket 1 out of 2 on rand()) a;

+----------+

| name |

+----------+

| lucy |

| shelley |

| lucy |

| lucy |

| shelley |

| lucy |

| will |

| shelley |

| michael |

| will |

| will |

| will |

| will |

| will |

| lucy |

+----------+

15 rows selected (0.07 seconds)

3 塊抽樣(block sampling)

它允許 hive隨機抽選 n 行資料 & (n%)百分比的資料量 & n 位元組的資料。抽樣的粒度是 hdfs block 的大小。它的語法結構如下所示:

select * 

from tablesample(n percent|bytelengthliteral|n rows) s;

-- bytelengthliteral

-- (digit)+ ('b' | 'b' | 'k' | 'k' | 'm' | 'm' | 'g' | 'g')

--按行抽樣

jdbc:hive2://> select name 

. . . . . . .> from employee_id_buckets tablesample(4 rows) a;

+----------+

| name |

+----------+

| lucy |

| shelley |

| lucy |

| shelley |

+----------+

4 rows selected (0.055 seconds)

--根據資料比例大小來抽樣 

jdbc:hive2://> select name 

. . . . . . .> from employee_id_buckets tablesample(10 percent) a;

+----------+

| name |

+----------+

| lucy |

| shelley |

| lucy |

+----------+

3 rows selected (0.061 seconds)

--根據資料大小進行抽樣 

jdbc:hive2://> select name 

. . . . . . .> from employee_id_buckets tablesample(3m) a;

+----------+

| name |

+----------+

| lucy |

| shelley |

| lucy |

| shelley |

| lucy |

| shelley |

| lucy |

| shelley |

| lucy |

| will |

| shelley |

| lucy |

| will |

| shelley |

| michael |

| will |

| shelley |

| lucy |

| will |

| will |

| will |

| will |

| will |

| lucy |

| shelley |

+----------+

25 rows selected (0.07 seconds)

HIve實現資料抽樣

1,2,在大規模資料量的資料分析及建模任務中,往往針對全量資料進行挖掘分析時會十分耗時和占用集群資源,因此一般情況下只需要抽取一小部分資料進行分析及建模操作。hive提供了資料取樣 sampling 的功能,能夠根據一定的規則進行資料抽樣,目前支援資料塊抽樣,分桶抽樣和隨機抽樣,具體如下所示 分桶抽...

hive資料抽樣的方法

塊抽樣 block sampling hive 本身提供了抽樣函式,使用 tablesample 抽取指定的 行數 比例 大小,舉例 create table iteblog as select from iteblog1 tablesample 1000 rows create table ite...

hive 隨機抽樣

1.random sampling syntax select from distribute by rand sort by rand limit 2.bucket table sampling 該方式是最佳化取樣bucket表。rand 函式也可以用來取樣整行。如果取樣列同時使用了cluster...