Hive命令行常用操作(数据库操作,表操作)

数据库操作

查看所有的数据库

hive> show databases ;

使用数据库default

hive> use default;

查看数据库信息

hive > describe database default;

OK

db_name comment location owner_name owner_type parameters

default Default Hive database hdfs://hadoop1:8020/user/hive/warehouse public ROLE

Time taken: 0.042 seconds, Fetched: 1 row(s)

显示地展示当前使用的数据库

hive> set hive.cli.print.current.db=true;

hive(default)>

Hive显示列头

hive (default)> set hive.cli.print.header=true;

hive (default)> desc addressall_2015_07_09;

OK

col_name data_type comment

_c0 string

addr1 bigint

addr2 bigint

addr3 bigint

Time taken: 0.182 seconds, Fetched: 4 row(s)

hive (default)> select * from addressall_2015_07_09;

OK

addressall_2015_07_09._c0 addressall_2015_07_09.addr1 addressall_2015_07_09.addr2 addressall_2015_07_09.addr3

2015_07_09 536 488 493

Time taken: 10.641 seconds, Fetched: 1 row(s)

创建数据库命令

hive (default)> create database liguodong;

OK

Time taken: 10.128 seconds

切换当前的数据库

hive (default)> use liguodong;

OK

Time taken: 0.031 seconds

hive (liguodong)>

删除数据库

删除数据库的时候,不允许删除有数据的数据库,如果数据库里面有数据则会报错。如果要忽略这些内容,则在后面增加CASCADE关键字,则忽略报错,删除数据库。

hive> DROP DATABASE DbName CASCADE(可选);

hive> DROP DATABASE IF EXISTS DbName CASCADE;

表操作

查看当前DB有啥表

hive> SHOW TABLES IN DbName;

hive (liguodong)> SHOW TABLES IN liguodong;

OK

tab_name

Time taken: 0.165 seconds

也可以使用正则表达式 hive> SHOW TABLES LIKE ‘h*‘;

hive (default)> SHOW TABLES LIKE ‘all’ ;

OK

tab_name

addressall_2015_07_09

Time taken: 0.039 seconds, Fetched: 1 row(s)

获得表的建表语句

hive (default)> show create table address1_2015_07_09;

OK

createtab_stmt

CREATE TABLE address1_2015_07_09(

addr1 bigint)

ROW FORMAT SERDE

‘org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe’

STORED AS INPUTFORMAT

‘org.apache.hadoop.mapred.TextInputFormat’

OUTPUTFORMAT

‘org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat’

LOCATION

‘hdfs://nameservice1/user/hive/warehouse/address1_2015_07_09’

TBLPROPERTIES (

‘COLUMN_STATS_ACCURATE’=’true’,

‘numFiles’=’1’,

‘numRows’=’0’,

‘rawDataSize’=’0’,

‘totalSize’=’4’,

‘transient_lastDdlTime’=’1436408451’)

Time taken: 0.11 seconds, Fetched: 17 row(s)

创建表

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)
  [(col_name data_type [COMMENT col_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]
  [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]
     ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
     [STORED AS DIRECTORIES]
  [
   [ROW FORMAT row_format]
   [STORED AS file_format]
     | STORED BY ‘storage.handler.class.name‘ [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)
  ]
  [LOCATION hdfs_path]
  [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)
  [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)

hive (default)> create table test(id int);

OK

Time taken: 10.143 seconds

获得表的建表语句

hive (default)> show create table test;

OK

createtab_stmt

CREATE TABLE test(

id int)

ROW FORMAT SERDE

‘org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe’

STORED AS INPUTFORMAT

‘org.apache.hadoop.mapred.TextInputFormat’

OUTPUTFORMAT

‘org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat’

LOCATION

‘hdfs://nameservice1/user/hive/warehouse/test’

TBLPROPERTIES (

‘transient_lastDdlTime’=’1436799093’)

Time taken: 0.135 seconds, Fetched: 12 row(s)

创建内部表

其他获得表的信息

创建外部表

hive (default)> create external table testexttable(
              > name string comment ‘name value‘,
              > addr string comment ‘addr value‘
              > );
OK
Time taken: 10.172 seconds

create table if not exists testtable(
name string comment ‘name value‘,
addr string comment ‘addr value‘
)
row format delimited fields terminated by ‘\t‘ lines terminated by ‘\n‘ stored as textfile
;

加载数据

hive (default)> load data local inpath ‘/liguodong/hivedata/datatest’ overwrite into table testtable;

hive (default)> load data local inpath ‘/liguodong/hivedata/datatest’ into table testtable;

如果没有使用overwrite,则会再拷贝一份数据,不会覆盖原来的数据。

hive (default)> create external table if not exists employees(
              > name string,
              > salary string,
              > subordinates array<string>,
              > deductions map<string,float>,
              > address struct<street:string,city:string,state:string,zip:int>
              > )
              > row format delimited fields terminated by ‘\t‘
              > collection items terminated by ‘,‘
              > map keys terminated by ‘:‘
              > lines terminated by ‘\n‘
              > stored as textfile
              > location ‘/liguodong/data/‘
              > ;

查找表数据

hive> select * from employees;
OK
tony    1338    ["a1","a2","a3"]        {"k1":1.0,"k2":2.0,"k3":3.0}    {"street":"s1","city":"s2","state":"s3","zip":4}
mark    5453    ["a4","a5","a6"]        {"k4":4.0,"k5":5.0,"k6":6.0}    {"street":"s4","city":"s5","state":"s6","zip":6}
ivy     323     ["a7","a8","a9"]        {"k7":7.0,"k8":8.0,"k9":9.0}    {"street":"s7","city":"s8","state":"s9","zip":9}
Time taken: 10.204 seconds, Fetched: 3 row(s)

查树组
hive> select subordinates[1]  from employees;
Total MapReduce CPU Time Spent: 2 seconds 740 msec
OK
a2
a5
a8
查map
hive> select deductions["k2"]  from employees;

OK
2.0
NULL
NULL
Time taken: 75.812 seconds, Fetched: 3 row(s)

查结构体
hive> select address.city  from employees;
Total MapReduce CPU Time Spent: 2 seconds 200 msec
OK
s2
s5
s8
Time taken: 75.311 seconds, Fetched: 3 row(s)

注意:select * 不执行mapreduce,只进行一个本地的查询。

select 某个字段 生成一个job,执行mapreduce。

select * from employees;
select * from employees limit 10;

删除表

内部表删除,会连同hdfs存储的数据一同删除,而外部表删除,只会删除外部表的元数据信息。

hive (default)> drop table testtable;
OK
Time taken: 10.283 seconds
hive (default)> drop table testexttable;
OK
Time taken: 0.258 seconds

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 16:37:57

Hive命令行常用操作(数据库操作,表操作)的相关文章

Mysql命令行常用操作

Mysql命令行常用操作 一.从命令行登录MySQL数据库服务器 1.登录使用默认3306端口的MySQL /usr/local/mysql/bin/mysql -u root -p 2.通过TCP连接管理不同端口的多个MySQL(注意:MySQL4.1以上版本才有此项功能) /usr/local/mysql/bin/mysql -u root -p --protocol=tcp --host=localhost --port=3307 3.通过socket套接字管理不同端口的多个MySQL /

从命令行运行django数据库操作

从命令行运行django数据库操作,报错: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before

从命令行执行django数据库操作

从命令行执行django数据库操作,报错: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before

linux中命令行安装oracle11g数据库

静默安装oracle11g_R1 补充rhel6.0命令行安装oracle数据库1.安装linux6.0版本服务器(用于安装数据库环境最低要求:磁盘10G的lv,内存1G,swap分区是内存的2倍)2.调试网络.防火墙开放端口.SELINUX允许3.配置本地主机名.域或远程主机名.域4.配置yum,本地yum和网络源yum均可5.开始安装数据库一.安装数据库所需环境yum -y install binutils compat-libstdc++-33 elfutils-libelf elfuti

第二百九十节,MySQL数据库-MySQL命令行导出导入数据库,数据库备份还原

MySQL命令行导出导入数据库,数据库备份还原 MySQL命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server 4.1\bin(或者直接将windows的环境变量path中添加该目录) 2,导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名 如我输入的命令行:mysqldump -u root -p news > news.sql

hive命令行不打印日志

[Author]: kwu hive命令行不打印日志,hive的命令行运行会输出mapreduce的日志,这些日志对大部分用户来说是不需要的. 如何去掉这个日志: hive -S 加上 "-S"  参数即可(-S,--silent ). 版权声明:本文为博主原创文章,未经博主允许不得转载.

Hive命令行

Hive 命令行 Hive 命令行示例 ?从命令行执行指定的sql语句 ?$HIVE_HOME/bin/hive -e 'select a.col from tab1 a' ?以指定的hive环境变量执行指定的sql语句 ?$HIVE_HOME/bin/hive -e 'select a.col from tab1 a' -hiveconf hive.exec.scratchdir=/home/my/hive_scratch -hiveconf mapred.reduce.tasks=32 ?以

提高Linux运维效率的命令行常用快捷键

提高Linux运维效率的命令行常用快捷键 tab 命令或路径等的补全键,Linux最有用快捷键 Ctrl+a 光标回到命令行首 Ctrl+e 光标回到命令行尾 Ctrl+k 剪切(删除)光标处到行尾的字符 Ctrl+u 剪切(删除)光标处到行首的字符 Ctrl+c 中断终端正在执行的任务或者删除整行 Ctrl+d 退出当前Shell命令行 Ctrl+r 搜索命令行使用过的历史命令记录 Ctrl+z 暂停执行在终端运行的任务 !! 执行上一条命令 !pw 执行最近以pw开头的命令 !$ 上一条命令

数据库之表操作,数据操作

注意的几点:1.如果你在cmd中书命令的时候,输入错了就用\c跳出 2.\s查看配置信息 一.操作文件夹(库) 增:create database db1 charset utf8; 删:drop database db1; 改:alter database db1 charset gbk; 查:show databases; #查看所有的数据库 show create database db1; #查看db1数据库 二.操作文件(表) 切换到文件夹下:use db1 增:create tabl