为什么80%的码农都做不了架构师?>>>
背景
hadoop2的hive采用了新的hive server,称为HiveServer2,HiveServer2中的CLI客户端不建议使用hive CLI,而是建议使用Beeline这个客户端,今天就说说这个客户端的用法。
介绍
HiveServer2提供了一个新的命令行工具beeline来连接hiveserver2,beeline是基于SQLLine CLI的JDBC客户端,因此SQLLine文档也适用于Beeline,SQLLine是一个基于纯Java控制台的实用程序,用于连接关系数据库和执行SQL命令。 它类似于其他命令行数据库访问实用程序,如sqlplus for Oracle,mysql for MySQL和用于Sybase / SQL Server的isql。具体看这个SQLLine
Beeline工作模式有两种,即嵌入模式和远程模式。嵌入模式情况下,它返回一个嵌入式的Hive(类似于Hive CLI)。而远程模式则是通过Thrift协议与某个单独的HiveServer2进程进行连接通信。在远程模式下hiveserver2只接受thrift的接口调用,即使是http模式,它里面也是包含thrift的信息的。
使用
连接数据库:
1 你可以在交互式模式下输入hiveserver连接字符串
2 你也可以在CLI直接输入hiveserver连接字符串,beeline -u xxx -n yyy
beeline连接选项:
-u JDBC连接串,beeline -u db_URL
-r 连接到用户最后一次连接的hiveserver上,前提是用户使用的 !connect去连接的hiveserver,同时使用 !save保存了连接, beeline -r
-n 连接的用户名,beeline -n valid_user
-p 连接的密码,beeline -p valid_password
-d 使用哪个driver连接hiveserver,beeline -d driver_class
-e 用单引号或者双引号括起来的查询语句,beeline -e "query_string"
-f 要执行的脚本,beeline -f filepath
-i (or) --init 初始化文件, beeline -i /tmp/initfile
-w (or) --password-file 从一个文件读取密码
-a (or) --authType jdbc连接时的认证类型
--property-file 读取配置文件,beeline --property-file /tmp/a
--hiveconf property=value 设置hiveconf参数,beeline --hiveconf prop1=value1
--hivevar name=value 设置hivevar参数,beeline --hivevar var1=value1
--color=[true/false] 终端显示是否带颜色,beeline --color=true
--showHeader=[true/false] 查询结果中是否显示表字段名,beeline --showHeader=false
--headerInterval=ROWS 重新显示列标题的间隔,以outputformat为表格的行数。 默认值为100, beeline --headerInterval=50
--autoCommit=[true/false] 是否开启事物的自动提交,默认false
--showWarnings=[true/false] 是CLI使用HQL的时候是否显示warning信息
--showDbInPrompt=[true/false] 是否在命令行提示符显示数据库名,默认false,beeline --showDbInPrompt=true
--showNestedErrs=[true/false] 是否显示嵌套错误,默认false.
--force=[true/false] 当执行脚本发生错误时,是否强制执行,默认false
--autosave=[true/false] 是否自动保存首选项,默认false,beeline --autosave=true
--outputformat=[table/vertical/csv/tsv/dsv/csv2/tsv2] 查询显示的输出格式
--delimiterForDSV= DELIMITER 查询输出值的默认分隔符,默认是 '|'
beeline sql命令:
all 对所有活动的SQL连接执行sql语句 !all {SQL statement}
autocommit 是否开启自动事物提交 !autocommit {on/off}
batch 启动或者执行一批SQL语句 !batch {SQL statement}
brief 开启简洁输出模式 !brief
close 关闭活动的连接 !close
columns 显示表的列 !columns {table name}
commit 提交当前连接的事物 !commit
connect 连接到一个数据库 !connect {url} {username} {password} [driver class]
info 显示当前数据库的元数据 !dbinfo
describe 描述一个表 !describe [table name]
dropall 删除当前数据库的所有表 !dropall
exportedkeys 显示一个表的外键 !exportedkeys {table name}
go 跳转到不同的活动连接 !go {connection number}
help 显示帮助信息 !help
history 显示执行命令历史 !history
!importedkeys {table name}
index 显示一个表的索引 !indexes {table name}
list 显示当前所有活动连接 !list
metadata 调用任意元数据 !metadata {methodname} {params...}
outputformat 设置输出格式 !outputformat {format name}
primarykeys 显示一个表的逐渐 !primarykeys {table name}
procedures 列出当前库的存储过程 !procedures [procedure name pattern]
properties 连接到指定文件中定义的数据库 !properties {file}
quit 退出sqlline !quit
reconnect 重新连接到当前数据库 !reconnect
record 记录来自SQL命令的所有输出到外部文件 !record {file name}
rehash 从数据库中获取所有表和列的列表 !rehash
rollback 回滚当前事物 !rollback
run 执行一个sql脚本 !run {file name}
save 保存当前首选项 !save
scan 扫描JDBC的classpath !scan
script 保存执行命令到指定文件,该文件可以回放 !script {filename}
set 设置首选项 !set {preference} {value}
sql 对数据库执行sql !sql {statement}
tables 列出当前库的所有表 !tables
配置项信息:
autocommit 设置连接自动提交
autosave 任何使用set设置的首选项都会被自动保存
color 终端输出会有颜色
fastconnect
force
headerinterval
historyfile SQLLine将保存发出的所有命令的记录的文件。 HOME / .sqlline / history
incremental
isolation
maxcolumnwidth table输出格式时截断数据之前,每列显示的最大列宽。 默认为15
maxwidth table被截断前显示的最大宽度,默认是当前终端宽度
outputformat 输出格式
showheader 显示列名
showwarnings 显示警告信息
silent 安静模式
verbose 更详细的信息
beeline hive sql命令:
reset 将配置重置为默认值
set = 设置特定配置变量(键)的值
set 打印由用户或Hive覆盖的配置变量列表。
set -v 列出Hadoop和Hive所有配置变量
add FILE[S] * 将一个或多个文件,jar或归档添加到分布式缓存中的资源列表。
add JAR[S] *
add ARCHIVE[S] *
list FILE[S] 列出已添加到分布式缓存中的资源。
list JAR[S]
list ARCHIVE[S]
list FILE[S] * 检查给定的资源是否已经添加到分布式缓存中
list JAR[S] *
list ARCHIVE[S] *
delete FILE[S] * 从分布式缓存中删除资源。
delete JAR[S] *
delete ARCHIVE[S] *
reload 使HiveServer2了解由配置参数hive.reloadable.aux.jars.path指定的路径中的任何jar更改
dfs 执行hadoop dfs命令
执行查询语句并输出
beeline输出格式:
在beeline里面输出的数据可以有几种不同的显示格式,显示格式可以通过设置outputformat来指定。目前支持以下格式:
table:
+-----+---------+-----------------+
| id | value | comment |
+-----+---------+-----------------+
| 1 | Value1 | Test comment 1 |
| 2 | Value2 | Test comment 2 |
| 3 | Value3 | Test comment 3 |
+-----+---------+-----------------+
vertical:
id 1
value Value1
comment Test comment 1
id 2
value Value2
comment Test comment 2
id 3
value Value3
comment Test comment 3
xmlsttr:
xmlelements:
1
Value1
Test comment 1
2
Value2
Test comment 2
3
Value3
Test comment 3
Separated-Value Output Formats:
每一行的value值可以自定义分隔符,可以是csv, tsv, csv2, tsv2 and dsv。