關於HIVE的SELECT count 優化

2021-06-14 09:13:39 字數 942 閱讀 5957

印象最深的是hive將表的大小作為了元資料儲存在了關聯式資料庫中。

譬如在老版本中執行如下的語句:

from base insert overwrite table user select * ;

你會發現日誌的末尾會有 「999 rowd loaded into table user」 的字樣

代表有999條目記錄儲存在了user表中。

在而在新版本中,執行最後的日誌展示了user表的元資料,其中會有row_count:999的字樣

一看mysql中,999這個資料確實被儲存了起來。

也就是說大家以後不必再用「select count(*) from user 」去查詢user表的大小了,因為hive會將這個語句翻譯為mr作業在hadoop上執行,效率非常低。

新的方法是

hiveconf conf = new hiveconf(sessionstate.class);

hive hive = hive.get(conf);

system.out.println(hive.gettable("user").getttable()

.getparameters());

list list = hive.getpartitions(hive.gettable("user"));

for (partition p : list)

其中列印出來的就是user表的元資料。

ps:1。有人會問  select count(1) from user where age=20 這種帶有條件的計數怎麼辦?

別忘記hive有分割槽的概念,看到上面的api了嗎,分割槽也是有元資料的。

2. conf是什麼東西?

conf是hiveconf  尋找你classpath路徑下的hivesite檔案生成的配置檔案物件。

將伺服器上的hive配置檔案copy到你的專案中,這個api就可以使用了。

hive關於表的語句

alter table table name drop partition id 2019,code 70 show partitions table name select from dynamic human2 where year 2018 alter table 原表名 rename to ...

關於hive的建表操作

hive 建立外部表語句例項 create external table x mac string,did string,uid string,sid string,tc version string,province string,city string,model string,chip str...

hive關於時間函式的用法

做資料探勘的,離不開使用各種時間函式。為了避免遺忘,以及後續各種抓瞎到處亂找,特意總結了hive中大部分常用的時間函式,方便自己也方便他們。1.unix timestamp 返回當前時區的unix時間戳 返回型別 bigint hive tmp select unix timestamp from ...