转载请注明出处:http://blog.csdn.net/gamer_gyt
博主微博:http://weibo.com/234654758
Github:https://github.com/thinkgamer
1.写在前边的话
自己电脑上部署的hive版本是hive1.1.1,在终端执行hive 命令时,凡是涉及where语句时,就会出现异常退出,对于一个开发者怎么能忍受这种情况,于是果断升级到hive1.21
同时需要注意的是在hive 1.1.1版本中创建table的时候,最后凡是添加一个stored as…的时候也会报错退出,暂时不清楚原因,不过小主估计也是hive版本的问题。(因为版本换到1.2.1就正常了)
2.Hive中所谓的Derby单用户
其实我们好多人都知道hive里默认配置一个derby数据库,只支持单用户操作,其实不然,只要保证在同一个文件夹下启动hive数据库,也是支持多用户的,但是当有很多台客户一个例子端访问hive时,并不能保证在同一目录下,所以就成了单用户操作,同样对于同一个用户来说,只要启动hive数据库的目录不一样,同样看不到他在另外一个文件夹下创建的数据库,因为derby在启动hive的时候会在启动目录创建元数据库,所以当环一个目录时,元原先的元数据库信息并不会迁移
3.hive使用的一些小技巧
hadoop 的safenode
有时候我们会发现执行hive语句总会报错,原因便是hadoop处于安全模式下,此时就需要离开safenode了,命令是
hadoop dfsadmin -safenamenode leave
显示当前操作的数据库
在hive中执行
set hive.cli.print.current.db=true
查询时显示相应的字段名
set hive.cli.print.header=true
显示表具体信息
正常人是这样查看的
desc table_name;
知道多点的是这样查看的
desc extended hot_word;
知道更多的人是这样查看的
desc formatted tablename; (太长了截图不够,就贴代码了)
hive (weibohotword)> desc formatted hot_word;
OK
col_name data_type comment
# col_name data_type comment
day string
hour string
word string
num int
# Partition Information
# col_name data_type comment
timeday string
# Detailed Table Information
Database: weibohotword
Owner: root
CreateTime: Fri Sep 16 17:55:58 CST 2016
LastAccessTime: UNKNOWN
Protect Mode: None
Retention: 0
Location: hdfs://localhost:9000/user/hive/warehouse/weibohotword.db/hot_word
Table Type: MANAGED_TABLE
Table Parameters:
comment this is hot words table
transient_lastDdlTime 1474019758
# Storage Information
SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat: org.apache.hadoop.mapred.TextInputFormat
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Compressed: No
Num Buckets: -1
Bucket Columns: []
Sort Columns: []
Storage Desc Params:
field.delim \t
line.delim \n
serialization.format \t
Time taken: 0.184 seconds, Fetched: 37 row(s)
显示创建表的语句
show create table hot_word;
hive (weibohotword)> show create table hot_word;
OK
createtab_stmt
CREATE TABLE `hot_word`(
`day` string,
`hour` string,
`word` string,
`num` int)
COMMENT 'this is hot words table'
PARTITIONED BY (
`timeday` string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
'hdfs://localhost:9000/user/hive/warehouse/weibohotword.db/hot_word'
TBLPROPERTIES (
'transient_lastDdlTime'='1474019758')
Time taken: 0.198 seconds, Fetched: 19 row(s)
4.自定义UDF(用户自定义函数)
IDEA创建项目引入hive lib目录下的jar包
需要引入hadoop的jar包和hive的jar包,引入后整个项目如图:
编写UDF代码
/**
* Created by thinkgamer on 16-9-17.
*/
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class JDBCHive {
private static String Driver ="org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://127.0.0.1:10000";
private static String name = "root";
private static String password = "root";
public static void main(String[] args) {
try {
Class.forName(Driver);
Connection conn = DriverManager.getConnection(url, name, password);
Statement stat = conn.createStatement();
String sql = "show databases";
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1));//hive的索引从1开始
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
查看效果
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
2016-09-17 01:26:13,988 INFO jdbc.Utils (Utils.java:parseURL(310)) - Supplied authorities: 127.0.0.1:10000
2016-09-17 01:26:13,992 INFO jdbc.Utils (Utils.java:parseURL(397)) - Resolved authority: 127.0.0.1:10000
2016-09-17 01:26:14,216 INFO jdbc.HiveConnection (HiveConnection.java:openTransport(203)) - Will try to open client transport with JDBC Uri: jdbc:hive2://127.0.0.1:10000
default
weibohotword
Process finished with exit code 0
可以看出hive数据仓库中有两个数据库分别是default和weibohotword
hiveserver2的官网介绍:点击阅读