使用Hive隨機抽樣

2021-08-21 13:59:43 字數 1111 閱讀 5055

test1  簡單隨機抽樣

select t.varx,t.a

from(

select varx,rand() a

from tablename)t

where t.a between 0 and 0.2

這樣就抽取了五分之一的資料。

--或者像這樣隨機抽取100條資料,與limit結合使用

select distinct a.*

from table a

order by rand(222)

limit 100

test2  資料塊取樣(block sampling)

select * from table1 tablesample (30m)

select * from table1 tablesample (15 percent)

select count(1) from (select * from lxw1 tablesample (200 rows)) x --不懂

select coutn(2) from table1 tablesample (bucket 1 out of 20 on rand()) -- 分桶20抽取第2桶

test3  系統抽樣

mod,rand() 依照userrid取模,分5組,每組隨機抽取100個使用者,實現如:

select *  

from(

select refund_id,user_id,mod,rank_num from

(select refund_id,user_id,cast(10+rand()*100 as double) rank_num,

user_id%5 as mod --依據user_id,取模,獲取 mod

from table1)

distribute by mod sort by mod,rank_num desc --根據mod分組,並排序

) a

where row_number(mod)<=20; --從每個mod裡面抽取20個

test4 分層抽樣

hive 隨機抽樣

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

Hive實現隨機抽樣(附詳解)

select from tab order by rand limit 1000select from select e.cast rand 100000 as int as idx from e t order by t.idx limit 1000表e為乙個存有資料普通表,我們要從表e中隨機抽出...

mR 隨機抽樣

1.問題由來 google曾經有一道非常經典的面試題 給你乙個長度為n的鍊錶。n很大,但你不知道n有多大。你的任務是從這n個元素中隨機取出k個元素。你只能遍歷這個鍊錶一次。你的演算法必須保證取出的元素恰好有k個,且它們是完全隨機的 出現概率均等 這道題的解法非常多,網上討論也非常熱烈。本文要討論的是...