天天看點

hadoop——hive學習一、前言二、什麼是hive三、hive的使用場景四、hive的安裝五、hive之shell學習六:hive之API操作:

Hive 是一個基于 Hadoop 檔案系統之上的資料倉庫架構。它可以将結構化的資料檔案映射為一張資料庫表,并提供簡單的 sql 查詢功能。還可以将 sql 語句轉換為 MapReduce 任務運作。
Hive在hadoop生态圈中屬于資料倉庫的角色。他能夠<b>管理**hadoop中的資料,同時可以</b>查詢**hadoop中的資料。本質上講,hive是一個<b>SQL解析引擎</b>。Hive可以把<b>SQL查詢</b>轉換為MapReduce中的job,然後在Hadoop上運作。
<b>Hive</b>誕生于 facebook 的日志分析需求,面對海量的結構化資料,Hive在Facebook主要用于日志的資料報表、統計分析、資料挖掘等功能。Hive主要是可用類似于SQL的文法,對HDFS海量資料庫中的資料進行查詢、統計、更新等操作。形象地說Hive更像一個資料倉庫管理工具。 因為Hive基于Hadoop,是以可以自定義Hadoop的Map Reduce流程,從Hive中調用。Hive是對Hadoop的一個上層封裝,他将HQL翻譯成對Hadoop 中Map Reduce的調用。
略過。網上教程。
進入shell互動指令
檢視hive的shell指令幫助
一:檢視meatstore資料庫中的表 二:查詢資料庫 三:建立表及查詢 <b>四:多字段資料表的建立</b> **************************************************************** CREATE TABLE  records(year String,tempaature INT,quality INT) ROW FORMAT DELEMITED FIELDS BY TERMINATED BY '\t'; 五:加載資料到hive表t1中 <b>如果是hdfs檔案路徑的話:load data inpath 'HDFS路徑' into table tableName</b> 六:删除表 <b>七:從其他表導入資料:</b> INERT OVERWRITE TABLE T1 SELECT * FROM T2; <b>八:複制表結構不複制資料</b> CREATE TABLE T3 LIKE T2; 九:HIVE視圖 CREATE VIEW V_HIVE AS SELECT A,B FROM HIVE_3 WHERE C&gt;30;

<b>hive分區</b>

<b>分區文法:</b> create table if not exists employees( name string, salary string, subordinates array&lt;string&gt;, deductions map&lt;String,float&gt;, address struct&lt;street:string,city:string,state:string,zip:int&gt; ) partitioned by (dtstring,typestring) row format delimited fields terminated by'\t' collection items terminated by',' mapkeys terminated by':' lines terminated by'\n' stored as textfile <b>分區表操作:</b> <b>alter table <b>employees </b>add if not exists partion(country ='xxx'[,state='yyyy'])</b> <b>Alter table employees drop if exists partition(country='xxx'[,state='yyyy’)</b>

<b>hive分桶</b>

<b>對于每一個表(table)或者分區,Hive可以進一步組織成桶,也就是說捅是更為細粒度的資料範困劃分。</b> Hive是針對某一列進行分捅。 Hive采用對列值哈希,然後除以捅的個數求餘的方式決定該條記錄存放在哪個桶當中。 <b>好處</b> 獲得更高的查詢處理效率。 使取樣(sampling)更高效 <b>分桶文法</b> create table bucketed_user(id string,name string)clustered by(id) sorted by(name)into 4 buckets row format delimited fields terminated by'\t' stored as textfile; <b>設定</b> set hive.enforce.bucketing =true; <b>插入資料</b> insert overwrite table bucketed_user select addr ,name from testtable;
<b>一、hive分區:</b> <b>1、在Hive Select查詢中一般會掃描整個表内容,會消耗很多時間做沒必要的工作。有時候隻需要掃描表中關心的一部分資料,是以建表時引入了partition概念。</b> 2、分區表指的是在建立表時指定的partition的分區空間。 3、如果需要建立有分區的表,需要在create表的時候調用可選參數partitioned by,詳見表建立的文法結構。 二、技術細節 1、一個表可以擁有一個或者多個分區,每個分區以檔案夾的形式單獨存在表檔案夾的目錄下。 2、表和列名不區分大小寫。 3、分區是以字段的形式在表結構中存在,通過describe table指令可以檢視到字段存在,但是該字段不存放實際的資料内容,僅僅是分區的表示。 4、建表的文法(建分區可參見PARTITIONED BY參數): CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT table_comment] [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] [ROW FORMAT row_format] [STORED AS file_format] [LOCATION hdfs_path] 5、分區建表分為2種,一種是單分區,也就是說在表檔案夾目錄下隻有一級檔案夾目錄。另外一種是多分區,表檔案夾下出現多檔案夾嵌套模式。 a、單分區建表語句:create table day_table (id int, content string) partitioned by (dt string);單分區表,按天分區,在表結構中存在id,content,dt三列。 b、雙分區建表語句:create table day_hour_table (id int, content string) partitioned by (dt string, hour string);雙分區表,按天和小時分區,在表結構中新增加了dt和hour兩列。 表檔案夾目錄示意圖(多分區表): <a href="https://link.jianshu.com?t=http://hi.csdn.net/attachment/201007/21/0_1279701454lpF1.gif" target="_blank"></a> 6、添加分區表文法(表已建立,在此基礎上添加分區): ALTER TABLE table_name ADD partition_spec [ LOCATION 'location1' ] partition_spec [ LOCATION 'location2' ] ... partition_spec: : PARTITION (partition_col = partition_col_value, partition_col = partiton_col_value, ...) 使用者可以用ALTER TABLE ADD PARTITION來向一個表中增加分區。當分區名是字元串時加引号。例: ALTER TABLE day_table ADD PARTITION (dt='2008-08-08', hour='08') location '/path/pv1.txt' PARTITION (dt='2008-08-08', hour='09') location '/path/pv2.txt'; 7、删除分區文法: ALTER TABLE table_name DROP partition_spec, partition_spec,... 使用者可以用ALTER TABLE DROP PARTITION來删除分區。分區的中繼資料和資料将被一并删除。例: ALTER TABLE day_hour_table DROP PARTITION (dt='2008-08-08', hour='09'); 8、資料加載進分區表中文法: LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)] 例: LOAD DATA INPATH '/user/pv.txt' INTO TABLE day_hour_table PARTITION(dt='2008-08- 08', hour='08'); LOAD DATA local INPATH '/user/hua/*' INTO TABLE day_hour partition(dt='2010-07- 07'); 當資料被加載至表中時,不會對資料進行任何轉換。Load操作隻是将資料複制至Hive表對應的位置。資料加載時在表下自動建立一個目錄,檔案存放在該分區下。 9、基于分區的查詢的語句: SELECT day_table.* FROM day_table WHERE day_table.dt&gt;= '2008-08-08'; 10、檢視分區語句: hive&gt; show partitions day_hour_table; OK dt=2008-08-08/hour=08 dt=2008-08-08/hour=09 dt=2008-08-09/hour=09 <b>11、hive兩種分區:靜态分區與動态分區的差别</b> 在加載資料的時候,動态分區不需要指定分區KEY的值,會根據KEY對應列的值自動分區寫入,如果改列值對應分區的目錄未建立,會自動建立并寫入。 例如: 三、總結 1、在Hive中,表中的一個Partition對應于表下的一個目錄,所有的Partition的資料都存儲在最字集的目錄中。 2、總的說來partition就是輔助查詢,縮小查詢範圍,加快資料的檢索速度和對資料按照一定的規格和條件進行管理。 <b></b>
<b>1.確定hive服務打開</b> <b>2.java代碼連接配接hiveserver</b> private static String riverName="org.apache.hadoop.hive.jdbc.HiveDriver"; public static void main(String[] args)throws Exception {       try{            Class.forName(driverName);            }catch(ClassNotFoundException e) {                     e.printStackTrace();                     System.exit(1);             } Connection con = DriverManager.getConnection("jdbc:hive://localhost:10002/default","wyp",""); Statement stmt = con.createStatement(); String tableName ="wyphao"; stmt.execute("drop table if exists "+ tableName); stmt.execute("create table "+ tableName +" (key int, value string)"); System.out.println("Create table success!"); // show tables String sql ="show tables '"+ tableName +"'"; System.out.println("Running: "+ sql); ResultSet res = stmt.executeQuery(sql); if(res.next()) {      System.out.println(res.getString(1)); } // describe table sql ="describe "+ tableName; res = stmt.executeQuery(sql); while(res.next()) {       System.out.println(res.getString(1) +"\t"+ res.getString(2)); sql ="select * from "+ tableName;      System.out.println(String.valueOf(res.getInt(1)) +"\t"                                 + res.getString(2)); sql ="select count(1) from "+ tableName;             System.out.println(res.getString(1));