hive配置及使用说明
1、首先我们要搭建好hadoop集群环境。本文这里不做介绍。
2、其次hive配置。笔者hive安装目录为/home/hadoop/hive-2.3.3/配置文件:/home/hadoop/hive-2.3.3/conf/hive-site.xml下面是笔者主节点上hive的详细配置文件
3、<?xml version="1.0" encoding="UTF-8" sta荏鱿胫协ndalone="no"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration><!--Hive作业的HDFS根目录位置 --><property><name>hive.exec.scratchdir</name><value>/home/hadoop/hive/tmp</value></property><!--Hive作业的HDFS根目录创建写权限 --><property><name>hive.scratch.dir.permission</name><value>733</value></property><!--hdfs上hive元数据存放位置 --><property><name>hive.metastore.warehouse.dir</name><value>/home/hadoop/hive/warehouse</value></property><!--连接数据库地址,名称 --><property><name>javax.jdo.option.ConnectionURL</name><value>jdbc:mysql://192.168.1.212:3306/hive?createDatabaseIfNotExist=true</value></property><!--连接数据库驱动 --><property><name>javax.jdo.option.ConnectionDriverName</name><value>com.mysql.jdbc.Driver</value></property><!--连接数据库用户名称 --><property><name>javax.jdo.option.ConnectionUserName</name><value>root</value></property><!--连接数据库用户密码 --><property><name>javax.jdo.option.ConnectionPassword</name><value>root</value></property><!--客户端显示当前查询表的头信息 --><property><name>hive.cli.print.header</name><value>true</value></property><!--客户端显示当前数据库名称信息 --><property><name>hive.cli.print.current.db</name><value>true</value></property><property><name>hive.metastore.schema.verification</name><value>false</value></property><property><name>hive.metastore.local</name><value>false</value></property><!-- 主节点元数据服务 --><property><name>hive.metastore.uris</name><value>thrift://192.168.1.216:9083</value></property></configuration>
4、hive服务启动nohup hive --service metastore & --启动元数据服务nohup hive --service hiveserver2 & --启动jdbc驱动访问服务
5、Hive元数据存储的三种模式单用户模式hive自带的有一个Derby数据库,在同一时间只能有一个进程连接使吹涡皋陕用数据库。hive-site.xml文件采用默认配置即可。多用户模式启动一个元数据服务,多用户操作元数据产生的元数据冲突可以由元数据服务来自行解决。需要配置一个元数据存储的数据库,多数以mysql作为元数据存储。如图:
6、远程模式在多用户模式基础上,将元数据配置为远程数据库。
7、hive数据存储的迷撞笸痉两种方式内部表(managed table)hive默认创建的表为内部表,其特点是元数据和表数据都由hive管理。外部表(external table)亟冁雇乏需要在建表hsql添加external关键字。如CREATE [EXTERNAL] TABLE [IF NOT EXISTS]table_name[(col_name data_type [COMMENT col_comment], ...)] comment ‘’ --注释[COMMENT table_comment] --注释[PARTITIONED BY(col_name data_type [COMMENT col_comment], ...)][CLUSTERED BY (col_name, col_name, ...)[SORTED BY(col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] --声明表中某个字段是有序的[ROW FORMAT row_format] row format delimited fiedls terminated by '\t'; --(一般必加的语句)[STORED AS file_format][LOCATION hdfs_path]
8、hive数据查询的三种方式Cli方式输入hive,进入交互模式。
9、Beeline方式输入beeline,进入交互模式
10、客户端工具如dbvisualizer,oracle sql developer
11、Java访问hive数据,引用的时候,需要添加hive对应的jdbc驱动,示例源码如下:
12、import java.sql.Connection;import java.sql.DriverM锾攒揉敫anager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class HiveTest { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; // 填写hive的IP,之前在配置文件中配置的IP private static String Url = "jdbc:hive2://192.168.1.216:10000/default"; private static Connection conn; private static PreparedStatement ps; private static ResultSet rs;// 创建连接public static Connection getConnnection() { try { Class.forName(driverName); conn = DriverManager.getConnection(Url, "root", "root"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn;}public static PreparedStatement prepare(Connection conn, String sql) { PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return ps;}public static void getDatas(String tablename) { conn = getConnnection(); String sql = "select * from " + tablename; System.out.println(sql); try { ps = prepare(conn, sql); rs = ps.executeQuery(); int columns = rs.getMetaData().getColumnCount(); System.out.println("col_count=" + columns); while (rs.next()) { for (int i = 1; i <= columns; i++) { System.out.println(rs.getString(i)); System.out.print("\t\t"); } System.out.println(); } } catch (SQLException e) { e.printStackTrace(); }}public static void main(String[] args) { String tablename = "tb1";//你在hive里建的表 getDatas(tablename);}}