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的官网介绍:点击阅读

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Elasticsearch hadoop使用示例 & 运维实战之集群规划 &presto-elasticsearch connecto...
elasticsearch-hadoop使用示例 在elasticsearch-hadoop的具体使用中碰到了几个问题,有必要记录一下,避免下次遇到时又要重新研究。 利用spark读取es数据源的简单示例 import org.elasticsearch.spark.sql._ val esOptions = Map("es.nodes"->"192.168.1.2,192.168.1.3", "es.scroll.size"->"1000", "es.field.read.as.array.include"->"SampleField") val esDF = sqlContext.read.format("org.elasticsearch.spark.sql").options(esOptions).load("sampleindex/es-spark") esDF.registerTempTable("esdemotbl") es.scroll.size一次性读入的记录数,默认是10, 如果不设置为大一点的值,要从es中读取1亿条数据,那将是一个漫长的过程 e...
- 下一篇
Spark-Yarn架构介绍
1.简介 MapReduct框架从hadoop-0.23版本以后发生了重大的变革, 新的计算框架我们称为MapReudce 2.0 或者 YARN(Yet-Another-Resource-Negotiator) 这篇文章主要是简单的介绍YARN的架构, 因为后续我们都会使用YARN提交Spark程序 MapReduct 2.0最主要的变革是把资源分配和任务调度隔离. 一个全局的资源管理称为ResourceManager (RM), 同时每个应用本身有一个ApplicationMaster (AM). 应用可以是单独的Job也可用是MapReduce job或者是是一个DAG job. ResourceManager和NodeManager (NM)组成数据处理框架, ResourceManager对所有应用进行资源分配,管理和调度 每个应用程序都有一个ApplicationMaster, ApplicationMaster从ResourceManager分配到资源, 在NodeManager执行和监控任务 2. 架构 在 YARN 架构中,一个全局 ResourceManager 以...
相关文章
文章评论
共有0条评论来说两句吧...