本章將介紹如何創(chuàng)建一個表以及如何將數(shù)據(jù)插入。創(chuàng)造表的約定在Hive中非常類似于使用SQL創(chuàng)建表。
Create Table是用于在Hive中創(chuàng)建表的語句。語法和示例如下:
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT table_comment] [ROW FORMAT row_format] [STORED AS file_format]
假設(shè)需要使用CREATE TABLE語句創(chuàng)建一個名為employee表。下表列出了employee表中的字段和數(shù)據(jù)類型:
Sr.No | 字段名稱 | 數(shù)據(jù)類型 |
---|---|---|
1 | Eid | int |
2 | Name | String |
3 | Salary | Float |
4 | Designation | string |
下面的數(shù)據(jù)是一個注釋,行格式字段,如字段終止符,行終止符,并保存的文件類型。
COMMENT ‘Employee details’ FIELDS TERMINATED BY ‘\t’ LINES TERMINATED BY ‘\n’ STORED IN TEXT FILE
下面的查詢創(chuàng)建使用上述數(shù)據(jù)的表名為 employee。
hive> CREATE TABLE IF NOT EXISTS employee ( eid int, name String, > salary String, destination String) > COMMENT ‘Employee details’ > ROW FORMAT DELIMITED > FIELDS TERMINATED BY ‘\t’ > LINES TERMINATED BY ‘\n’ > STORED AS TEXTFILE;
如果添加選項IF NOT EXISTS,Hive 忽略大小寫,萬一表已經(jīng)存在的聲明。
成功創(chuàng)建表后,能看到以下回應(yīng):
OK Time taken: 5.905 seconds hive>
以下是使用JDBC程序來創(chuàng)建表給出的一個例子。
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveCreateTable { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; public static void main(String[] args) throws SQLException { // Register driver and create driver instance Class.forName(driverName); // get connection Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", ""); // create statement Statement stmt = con.createStatement(); // execute statement stmt.executeQuery("CREATE TABLE IF NOT EXISTS " +" employee ( eid int, name String, " +" salary String, destignation String)" +" COMMENT ‘Employee details’" +" ROW FORMAT DELIMITED" +" FIELDS TERMINATED BY ‘\t’" +" LINES TERMINATED BY ‘\n’" +" STORED AS TEXTFILE;"); System.out.println(“ Table employee created.”); con.close(); } }
將該程序保存在一個名為HiveCreateDb.java文件。下面的命令用于編譯和執(zhí)行這個程序。
$ javac HiveCreateDb.java $ java HiveCreateDb
Table employee created.
一般來說,在SQL創(chuàng)建表后,我們就可以使用INSERT語句插入數(shù)據(jù)。但在Hive中,可以使用LOAD DATA語句插入數(shù)據(jù)。
同時將數(shù)據(jù)插入到Hive,最好是使用LOAD DATA來存儲大量記錄。有兩種方法用來加載數(shù)據(jù):一種是從本地文件系統(tǒng),第二種是從Hadoop文件系統(tǒng)。
加載數(shù)據(jù)的語法如下:
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
我們將插入下列數(shù)據(jù)到表中。在/home/user目錄中名為sample.txt的文件。
1201 Gopal 45000 Technical manager 1202 Manisha 45000 Proof reader 1203 Masthanvali 40000 Technical writer 1204 Kiran 40000 Hr Admin 1205 Kranthi 30000 Op Admin
下面的查詢加載給定文本插入表中。
hive> LOAD DATA LOCAL INPATH '/home/user/sample.txt' > OVERWRITE INTO TABLE employee;
下載成功完成,能看到以下回應(yīng):
OK Time taken: 15.905 seconds hive>
下面給出的是JDBC程序?qū)⒔o定的數(shù)據(jù)加載到表中。
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveLoadData { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; public static void main(String[] args) throws SQLException { // Register driver and create driver instance Class.forName(driverName); // get connection Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", ""); // create statement Statement stmt = con.createStatement(); // execute statement stmt.executeQuery("LOAD DATA LOCAL INPATH '/home/user/sample.txt'" + "OVERWRITE INTO TABLE employee;"); System.out.println("Load Data into employee successful"); con.close(); } }
將該程序保存在一個名為HiveLoadData.java文件。使用下面的命令來編譯和執(zhí)行這個程序。
$ javac HiveLoadData.java $ java HiveLoadData
Load Data into employee successful
更多建議: