HIVE一些小技巧和java操作hive

转载请注明出处: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的官网介绍:点击阅读


【技术服务】,详情点击查看: https://mp.weixin.qq.com/s/PtX9ukKRBmazAWARprGIAg

扫一扫 关注微信公众号!号主 专注于搜索和推荐系统,尝试使用算法去更好的服务于用户,包括但不局限于机器学习,深度学习,强化学习,自然语言理解,知识图谱,还不定时分享技术,资料,思考等文章!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值