程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Centos7安装Hive2.0.1集群

发布于2021-06-08 12:19     阅读(916)     评论(0)     点赞(8)     收藏(4)


1、准备工作
1.1、安装jdk1.8和mysql5.7.21,略
1.2、安装Hadoop2.6.0,略
1.3、机器介绍

  1. 192.168.1.89 node1
  2. 192.168.1.149 node2
  3. 192.168.1.180 node3

node1、node2、node3上都已经安装了Hadoop2.6.0
2、下载并上传(三个节点都操作)
http://archive.apache.org/dist/hive/hive-2.0.1/apache-hive-2.0.1-bin.tar.gz
上传至三台机器的/data/server目录下,并解压:
tar -zxvf apache-hive-2.0.1-bin.tar.gz
3、修改配置文件(三个节点都操作)

  1. cd apache-hive-2.0.1-bin/conf
  2. cp hive-default.xml.template hive-site.xml
  3. vi hive-site.xml,新增:
  4. <configuration>
  5. <property>
  6. <name>datanucleus.schema.autoCreateAll</name>
  7. <value>true</value>
  8. </property>
  9. <property>
  10. <name>javax.jdo.option.ConnectionURL</name>
  11. <value>jdbc:mysql://192.168.1.166:3306/hive_db?createDatabaseIfNotExist=true</value>
  12. </property>
  13. <property>
  14. <name>javax.jdo.option.ConnectionDriverName</name>
  15. <value>com.mysql.jdbc.Driver</value>
  16. </property>
  17. <property>
  18. <name>javax.jdo.option.ConnectionUserName</name>
  19. <value>root</value>
  20. </property>
  21. <property>
  22. <name>javax.jdo.option.ConnectionPassword</name>
  23. <value>123456</value>
  24. </property>
  25. <property>
  26. <name>hive.support.concurrency</name>
  27. <value>true</value>
  28. </property>
  29. <property>
  30. <name>hive.enforce.bucketing</name>
  31. <value>true</value>
  32. </property>
  33. <property>
  34. <name>hive.exec.dynamic.partition.mode</name>
  35. <value>nonstrict</value>
  36. </property>
  37. <property>
  38. <name>hive.txn.manager</name>
  39. <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
  40. </property>
  41. <property>
  42. <name>hive.compactor.initiator.on</name>
  43. <value>true</value>
  44. </property>
  45. <property>
  46. <name>hive.compactor.worker.threads</name>
  47. <value>1</value>
  48. </property>
  49. <property>
  50. <name>hive.in.test</name>
  51. <value>true</value>
  52. </property>
  53. <property>
  54. <name>hive.server2.transport.mode</name>
  55. <value>binary</value>
  56. <description>
  57. Expects one of [binary, http].
  58. Transport mode of HiveServer2.
  59. </description>
  60. </property>
  61. <property>
  62. <name>hive.server2.thrift.bind.host</name>
  63. <value>机器IP</value>
  64. <description>
  65. Bind host on which to run the HiveServer2 Thrift interface.Can
  66. be overridden by setting $HIVE_SERVER2_THRIFT_BIND_HOST
  67. </description>
  68. </property>
  69. <property>
  70. <name>hive.server2.thrift.port</name>
  71. <value>10000</value>
  72. <description>Port number of HiveServer2 Thrift interface when hive.server2.transport.mode is 'binary'.</description>
  73. </property>
  74. </configuration>

4、修改环境变量(三个节点都操作)

  1. vim /etc/profile
  2. export HIVE_HOME=/data/server/apache-hive-2.0.1-bin
  3. export HIVE_CONF_DIR=$HIVE_HOME/conf
  4. export PATH=$HIVE_HOME/bin:$PATH
  5. 激活配置:source /etc/profile

5、上传mysql驱动(三个节点都操作)

  1. cd /data/server/apache-hive-2.0.1-bin/lib/
  2. 上传mysql-connector-java-5.1.30.jar

6、测试是否成功(三个节点都操作)
(1) hive    //登录hive
(2) show databases;  //显示hive表名
7、后台启动服务(三个节点都操作)
//不需要启动 nohup hive --service metastore  &
nohup hive --service hiveserver2  &
8、简单使用1(node1上操作,不可修改或删除行数据)
使用beeline命令打开客户端,使用!connect jdbc:hive2://node1:10000/default连接上hive默认库并回车
8.1、创建库

create database test_db;

8.2、创建表

  1. use test_db;
  2. CREATE TABLE t_test1 (a int, b int, c int) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

关键字释义:
ROW FORMAT DELIMITED 是指明后面的关键词是列和元素分隔符的
FIELDS TERMINATED BY 是字段分隔符,
8.3、导入数据

vi /data/server/apache-hive-2.0.1-bin/t_test1.txt,字段之间以tab隔开:

16      2       3
61      12      13
41      2       31
17      21      3
71      2       31
1       12      34
11      2       34
导入脚本:

LOAD DATA LOCAL INPATH '/data/server/apache-hive-2.0.1-bin/t_test1.txt' OVERWRITE INTO TABLE t_test1;

8.4、新增数据

insert into t_test1(a,b,c) values(12,13,14);

8.5、查看数据(可到其他节点查询数据是否同步,从而验证集群)

select * from t_test1;

8.6、查看表结构

desc t_test1;

8.7、删除表

drop table t_test1;

9、简单使用2(node1上操作,可修改或删除行数据)
使用beeline命令打开客户端,使用!connect jdbc:hive2://node1:10000/default连接上hive默认库并回车
9.1、创建表

  1. use test_db;
  2. create table t_test2(id int ,name string ) clustered by (id) into 2 buckets stored as orc TBLPROPERTIES('transactional'='true');

关键字释义:
clustered by (id) into 2 buckets 将表根据id分为2个桶
stored as orc 表存储文件的格式为orc
TBLPROPERTIES('transactional'='true')启用表的事务支持
9.2、导入数据
不支持文件导入
9.3、新增数据

insert into t_test2(id,name) values(1,'你好');

9.4、修改数据

update t_test2 set name = '你不好' where id=1;

9.5、查看数据(可到其他节点查询数据是否同步,从而验证集群)

select * from t_test2;

9.6、删除数据

delete from t_test2 where id=1;

9.7、查看表结构

desc t_test1;

9.8、删除表

drop table t_test1;

9.9、注意事项
如果在创建orc表时出现lock错误提示,可以添加Hive元数据(使用mysql存储)INSERT INTO NEXT_LOCK_ID VALUES(1);
10、java调用hive实例
10.1、导入maven依赖

  1. <dependency>
  2. <groupId>org.apache.hive</groupId>
  3. <artifactId>hive-jdbc</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>

10.2、代码实现

  1. package com.x.y.test;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import org.junit.After;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. public class HiveJDBC {
  10. private static String driverName="org.apache.hive.jdbc.HiveDriver";
  11. private static String url = "jdbc:hive2://192.168.1.89:10000/test_db";
  12. private static Connection conn = null;
  13. private static Statement stmt = null;
  14. private static ResultSet rs = null;
  15. public static void main(String[] args) throws Exception {
  16. Class.forName(driverName);
  17. conn = DriverManager.getConnection(url, null, null);
  18. stmt = conn.createStatement();
  19. String sql = "select * from t_xxl";
  20. System.out.println("Running: " + sql);
  21. rs = stmt.executeQuery(sql);
  22. System.out.println("a" + "\t" + "b" +"\t" + "c" );
  23. while (rs.next()) {
  24. System.out.println(rs.getInt(1) + "\t" + rs.getString(2)+ "\t"+ rs.getString(3));
  25. }
  26. if (rs != null) {
  27. rs.close();
  28. }
  29. if (stmt != null) {
  30. stmt.close();
  31. }
  32. if (conn != null) {
  33. conn.close();
  34. }
  35. }
  36. }

原文链接:https://blog.csdn.net/xuxile/article/details/117535170



所属网站分类: 技术文章 > 博客

作者:我很伤感

链接:http://www.javaheidong.com/blog/article/219610/7c041d9abecb3f32ebe3/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)