当前位置:文档之家› 数据的导入导出--Hive

数据的导入导出--Hive

Hive

Hive是建立在Hadoop基础上的数据仓库,使用MapReduce对存储于HDFS上的数据进行分析。它专门定义了一种类SQL查询语言,称之为HiveQL。

一、安装与配置

1.解压安装包,配置环境变量;

2.在HDFS上创建Hive所需的路径:/tmp(存放查询语句执行后产生的临时数据),

/user/hive/warehouse(存储写入Hive表中的数据);

3.修改上述路径的访问权限,是用户组具有写入权限:$ hadoop fs –chmod g+w /tmp

4.启动Hive:$ hive

二、实践:

1.创建employees数据表

hive> create table employees(name string, job string, salary int, entryDate string) > row format delimited

> fields terminated by '\t'

> stored as textfile;

[ROW FORMAT DELIMITED]关键字,是用来设置创建的表在加载数据的时候,支持的列分隔符;[STORED AS file_format]关键字是用来设置加载数据的数据类型。Hive本身支持的文件格式只有:Text File,Sequence File。如果文件数据是纯文本,可以使用[STORED AS TEXTFILE]。如果数据需要压缩,使用[STORED AS SEQUENCE] 。通常情况,只要不需要保存序列化的对象,我们默认采用[STORED AS TEXTFILE]。

2.将数据文件employee.tsv拷贝到HDFS

$ Hadoop fs –put employee.tsv /tmp

3.将文件数据插入表中

Hive> load data inpath '/tmp/employees.tsv' overwrite into table employees;

4.检查HDFS上存放数据文件的目录

$ Hadoop fs –ls /tmp --结果/tmp/hive-user

5.Local inpath可直接将本地文件系统中的源文件导入到Hive表中

Hive> load data local inpath '/home/joe/Desktop/employees.tsv' overwrite into table employees;

6.导出数据

hive> insert overwrite local directory '/home/joe/export'

> row format delimited

> fields terminated by '\t'

> select * from employees;

Or

$ hive -e "select * from employees" >> export/out.txt

7.验证表(不使用Hive shell)

$ hive –e “select * from employees”

8.执行HiveQL脚本

脚本command.hql内容:

drop table employees;

create table employees(name string, job string, salary int, entryDate string)

row format delimited

fields terminated by '\t'

stored as textfile;

load data local inpath '/home/joe/Desktop/employees.tsv' overwrite into table employees;

执行命令:

$ hive –f commands.hql

MySQL

一、把employees.tsv文件数据导入数据表:

$ mysql --local-infile –u hadoopuser –p

mysql> load data local infile '/home/joe/Desktop/employees.tsv' into table employees fields terminated by '\t' lines terminated by '\n';

二、利用MySQL导出数据:

mysql> select * from employees into outfile '/tmp/mysql_out.csv' fields terminated by ',' lines terminated by '\n';

注:此命令运行成功的条件1. $ mysql –u root –p 2. Outfile 存放目录为“/tmp/”why?

相关主题
相关文档 最新文档