hive数据转储

hdfs

$ hdfs dfs -mkdir /emp
$ hdfs dfs -put emp /emp
$ hdfs dfs -ls -emp

>Found 1 items

mysql

create table emp(
    empno int ,
    ename char(20),
    job string,
    mgr int,
    hiredate string,
    sal float,
    comm float,
    deptno int
)
row format delimited fields terminated by ','
stored as textfile
create database hgd defalut character set utf8 collate utf8_general_ci;
create database hgd default character set utf8 collate utf8_general_ci;
use hgd;
CREATE TABLE deptstat(
  deptno char(10), 
  num    bigint, 
  avgsal double, 
  maxsal float, 
  minsal float
)
charset utf8 collate utf8_general_ci;

hive

hive> create database hgd;
hive> use hgd;
hive> show database;

hive> create table emp(
hive>     empno int ,
hive>     ename char(20),
hive>     job string,
hive>     mgr int,
hive>     hiredate string,
hive>     sal float,
hive>     comm float,
hive>     deptno int
hive> )
hive> row format delimited fields terminated by ','
hive> stored as textfile
hive> ;
hive> load data inpath '/emp' into table emp
hive> ;
hive> select * from emp ;

​ 将emp数据导入到emp表,然后deptstat表对emp表的数据进行处理,形成一张处理后的新表

hive> create table deptstat
hive> as
hive> select deptno , count(*) num , avg(sal) avgsal ,
		max(sal) maxsal , min(sal) minsal
hive> from emp
hive> group by deptno
hive> order by deptno
hive> ;
hive> create table deptjob
hive> as
hive> select deptno , job , count(*) num ,
		avg(sal) avgsal
hive> from emp
hive> group by deptno,job
hive> order by deptno,job
hive> ;
# 这个表是没有用的
hive> select * from deptstat ;

sqoop

$ sqoop export --connect "jdbc:mysql://localhost:3306/hgd?useSSH=false&useUnicode=true&characterEncoding=utf-8" --username root --password root --table deptstat  --export-dir /user/hive/warehouse/deptstat/ --input-fields-terminated-by  '\001'  -m 1
$ sqoop export --connect "jdbc:mysql://localhost:3306/【数据库名称】?useSSH=false&useUnicode=true&characterEncoding=utf-8" --username root --password root --table 【mysql中的数据表】  --export-dir 【hive数据表路径/hive数据表】 --input-fields-terminated-by  '\001'  -m 1

​ 建立从hive(的deptstat表)到mysql(的deptstat表)的映射,将数据从hive导出到mysql

$ sqoop export 
--connect "jdbc:mysql://localhost:3306/hgd?useSSH=false&useUnicode=true&characterEncoding=utf-8" 
--username root --password root --table deptstat  
--export-dir /user/hive/warehouse/deptstat/ 
--input-fields-terminated-by  '\001'  
-m 1
sqoop export 前缀

--connext 连接到数据库

--username root --password root 登录数据库

--table deptstat 指定关系数据库中的表名
						
--export-dir 参数设置为Hive表对应的目录
				指定要导出HDFS源文件路径
				
--input-fields-terminated-by '' 指定源文件中字段之间的分隔符

-m 指定并行导出操作的Map任务个数,默认为4个