zoukankan      html  css  js  c++  java
  • Hive的基本操作和数据类型

    Hive的基本操作

     1.启动Hive
    	  bin/hive
     2.查看数据库
     	hive>show databases;
     3. 打开默认数据库
     	hive>use default;
     4.显示default数据库中的所有表
        hive>show tables; 
     5.创建一张表
     	hive> create table student(id int, name string) ;
     6.显示数据库中的所有表
     	hive>show tables;
     7.查看表结构
     	hive>desc student;
     8.向表中插入数据
    	hive> insert into student values(1000,"ss");
    9.查询表中数据
    	hive>select * from student;
    10.退出Hive
    	hive>quit;
    
    

    将本地文件数据导入到hive中

    需求:将本地/opt/module/datas/student.txt这个目录下的数据,
    导入到hive的student(id int, name string)表中。
    
    student.txt:文件内容使用	进行分割
    1001  zhangshan
    1002  lishi
    1003  zhaoliu
    
    $ bin/hive
    hive>show databases;
    hive>use default;
    hive>show tables;
    hive> drop table student;
    
    创建 student 表, 并声明文件分隔符’	’
    hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '	';
    
    加载/opt/module/datas/student.txt 文件到 student 数据库表中。
    hive> load data local inpath '/opt/module/datas/student.txt' into table student;
    
    Hive 查询结果
    hive> select * from student;
    
    

    中间出现的问题

    Caused  by:  java.lang.RuntimeException:  Unable  to  instantiate
    org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
    
    原因是,Metastore 默认存储在自带的 derby 数据库中,推荐使用 MySQL 存储 Metastore;
    

    Hive 元数据配置到 MySql

    前提,确认MySqlServer安装成功

    1.拷贝Mysql驱动到hive/lib包下
    mysql-connector-java-5.1.27-bin.jar
    
    2.配置 Metastore 到 到 MySql
    	配置hive-site.xml
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
    	<property>
    		<name>javax.jdo.option.ConnectionURL</name>
    		<value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseIfNotExist=true</value>
    		<description>JDBC connect string for a JDBC metastore</description>
    	</property>
    	<property>
    		<name>javax.jdo.option.ConnectionDriverName</name>
    		<value>com.mysql.jdbc.Driver</value>
    		<description>Driver class name for a JDBC metastore</description>
    	</property>
    	<property>
    		<name>javax.jdo.option.ConnectionUserName</name>
    		<value>root</value>
    		<description>username to use against metastore database</description>
    	</property>
    	<property>
    		<name>javax.jdo.option.ConnectionPassword</name>
    		<value>000000</value>
    		<description>password to use against metastore database</description>
    	</property>
    	<property>
    		<name>hive.cli.print.current.db</name>
    		<value>true</value>
    		<description>Whether  to  include  the  current  database  in  the  Hive
    		prompt.</description>
    	</property>
    	<property>
    		<name>hive.cli.print.header</name>
    		<value>false</value>
    		<description>Whether to print the names of the columns in query output.</description>
    	</property>
    </configuration>
    
    配置完毕后,如果启动 hive 异常,可以重新启动虚拟机。(重启后,别忘了启动 hadoop集群)
    
    

    Hive 常用的交互命令

    “-e”不进入 hive 的交互窗口执行 sql 语句
    	$ bin/hive -e "select id from student;"
    
    “-f”执行脚本中 sql 语句
    	$ bin/hive -f /opt/module/datas/hivef.sql
    
    执行文件中的 sql 语句并将结果写入文件中
    $  bin/hive  -f  /opt/module/datas/hivef.sql  > /opt/module/datas/hive_result.txt
    

    Hive 其他命令操作

    1)退出 hive 窗口: 
    	hive(default)>exit;
    	hive(default)>quit; 
    	exit:先隐性提交数据,再退出;
    	quit:不提交数据,退出;
    	
    2)在 hive cli 命令窗口中如何查看 hdfs 文件系统
    	hive(default)>dfs -ls /;
    	
    3)在 hive cli 命令窗口中如何查看 hdfs 本地系统
    	hive(default)>! ls /opt/module/datas;
    	
    4)查看在 hive 中输入的所有历史命令
    	(1)进入到当前用户的根目录/root 或/home/upuptop
    	(2)查看. hivehistory 文件
    	[upuptop@hadoop102 ~]$ cat .hivehistory
    

    Hive 常见属性配置

    Hive  数据仓库位置配置
    	1)Default 数据仓库的最原始位置是在 hdfs 上的:/user/hive/warehouse 路径下
    	2)在仓库目录下,没有对默认的数据库 default 创建文件夹。如果某张表属于 default	数据库,		
    	直接在数据仓库目录下创建一个文件夹。
    	3)修改 default 数据仓库原始位置(将 hive-default.xml.template 如下配置信息拷贝到	hive-site.xml 文件中)
    		<property>
    			<name>hive.metastore.warehouse.dir</name>
    			<value>/user/hive/warehouse</value>
    			<description>location of default database for the warehouse</description>
    		</property>
    	配置同组用户有执行权限
    	bin/hdfs dfs -chmod g+w /user/hive/warehouse
    
    查询后信息显示配置
    	在 hive-site.xml 文件中添加如下配置信息,就可以实现显示当前数据库,以及查询	表的头信息配置
    	<property>
    		<name>hive.cli.print.header</name>
    		<value>true</value>
    		</property>
    		<property>
    		<name>hive.cli.print.current.db</name>
    		<value>true</value>
    	</property>
    
    Hive 运行日志信息配置
    	Hive 的 log 默认存放在/tmp/uupuptop/hive.log 目录下(当前用户名下)
      修改 hive 的 log 存放日志到/opt/module/hive/logs
    	(1)修改/opt/module/hive/conf/hive-log4j.properties.template 文件名称为hive-log4j.properties
    	$ pwd
    	opt/module/hive/conf
    	$ mv hive-log4j.properties.template hive-log4j.properties
    	(2)在 hive-log4j.properties 文件中修改 log 存放位置
    	$ hive.log.dir=/opt/module/hive/logs
    
    参数配置方式
    	1)查看当前所有的配置信息
    		hive>set;
    	2)参数的配置三种方式
    		(1)配置文件方式
    				默认配置文件:hive-default.xml
    				用户自定义配置文件:hive-site.xml
    				注意:用户自定义配置会覆盖默认配置。另外,Hive 也会读入 Hadoop 的配置,
    				因为 Hive 是作为 Hadoop 的客户端启动的,Hive 的配置会覆盖 Hadoop 的配置。
    				配置文件的设定对本机启动的所有 Hive 进程都有效。
    		(2)命令行参数方式
    			启动 Hive 时,可以在命令行添加-hiveconf param=value 来设定参数。
    			例如:
    			[upuptop@hadoop103 hive]$ bin/hive -hiveconf mapred.reduce.tasks=10;
    			注意:仅对本次 hive 启动有效
    			查看参数设置:
    			hive (default)> set mapred.reduce.tasks;
    		
    		(3)参数声明方式
    			可以在 HQL 中使用 SET 关键字设定参数
    			例如:
    				hive (default)> set mapred.reduce.tasks=100;
    				注意:仅对本次 hive 启动有效。
    		查看参数设置: hive (default)> set mapred.reduce.tasks
    	上述三种设定方式的优先级依次递增。即配置文件<命令行参数<参数声明。注意某些系统级的参数,
    	例如 log4j 相关的设定,必须用前两种方式设定,因为那些参数的读取在会
    	话建立以前已经完成了。
    	
    

    Hive 数据类型

    基本数据类型

    Hive 数据类型 Java 数据类型 长度 例子
    TINYINT byte 1byte有符号整数 20
    SMALINT short 2byte有符号整数 20
    INT int 4byte 有符号整数 20
    BIGINT long 8byte 有符号整数 20
    BOOLEAN boolean 布尔类型,true 或者false TRUE FALSE
    FLOAT float 单精度浮点数 3.14159
    DOUBLE double 双精度浮点数 3.14159
    STRING string 字符系列。可以指定字符集。可以使用单引号或者双引号。 ‘now is the time’ “forall good men”
    TIMESTAMP 时间类型
    BINARY 字节数组

    对于 Hive 的 String 类型相当于数据库的 varchar 类型,该类型是一个可变的字符串,不过它不能声明其中最多能存储多少个字符,理论上它可以存储 2GB 的字符数。

    集合数据类型

    数据类型 描述 语法示例
    STRUCT 和 c 语言中的 struct 类似,都可以通过“点”符号访问元素内容。例如,如果某个列的数据类型是 STRUCT{first STRING, lastSTRING},那么第 1 个元素可以通过字段.first 来引用。 struct()
    MAP MAP 是一组键-值对元组集合,使用数组表示法可以访问数据。例如,如果某个列的数据类型是 MAP,其中键->值对是’first’->’John’和’last’->’Doe’,那么可以通过字段名[‘last’]获取最后一个元素 map()
    ARRAY 数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。例如,数组值为[‘John’, ‘Doe’],那么第 2 个元素可以通过数组名[1]进行引用。 Array()

    Hive 有三种复杂数据类型 ARRAY、MAP 和 STRUCT。ARRAY 和 MAP 与 Java 中的 Array 和 Map 类似,而 STRUCT 与 C 语言中的 Struct 类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。

    案例实操

    1)假设某表有如下一行,我们用 JSON 格式来表示其数据结构。在 Hive 下访问的格式为

    {
    	"name": "songsong",
    	"friends": ["bingbing", "lili"], //列表 Array,
    	"children": { //键值 Map,
    		"xiao song": 18,
    		"xiaoxiao song": 19
    	}
    	"address": { //结构 Struct,
    		"street": "hui long guan",
    		"city": "beijing"
    	}
    }
    

    2)基于上述数据结构,我们在 Hive 里创建对应的表,并导入数据。
    创建本地测试文件 test.txt

    songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
    yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
    

    注意,MAP,STRUCT 和 ARRAY 里的元素间关系都可以用同一个字符表示,这里用“_”。

    3)Hive 上创建测试表 test

    create table test(
    	name string,
    	friends array<string>,
    	children map<string, int>,
    	address struct<street:string, city:string>
    ) row format delimited fields terminated by ','
    collection items terminated by '_'
    map keys terminated by ':'
    lines terminated by '
    ';
    
    字段解释:
    row format delimited fields terminated by ',' -- 列分隔符
    collection items terminated by '_' --MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)
    map keys terminated by ':'  -- MAP 中的 key 与 value 的分隔符
    lines terminated by '
    '; -- 行分隔符
    

    4)导入文本数据到测试表

    hive (default)> load data local inpath '/opt/module/datas/test.txt' into table test;
    

    5)访问三种集合列里的数据,以下分别是 ARRAY,MAP,STRUCT 的访问方式

    hive (default)> select friends[1],children['xiao song'],address.city from test where
    name="songsong";
    OK
    _c0 _c1 city
    lili 18 beijing
    Time taken: 0.076 seconds, Fetched: 1 row(s)
    

    本博客仅为博主学习总结,感谢各大网络平台的资料。蟹蟹!!

  • 相关阅读:
    Redis命令行之Hash
    Redis命令行之String
    Redis配置
    访问者模式【行为模式】
    状态模式【行为模式】
    责任链模式【行为模式】
    观察者模式【行为模式】
    策略模式【行为模式】
    模板方法模式【行为模式】
    组合模式【结构模式】
  • 原文地址:https://www.cnblogs.com/shaofeer/p/11154304.html
Copyright © 2011-2022 走看看