mysql导入hive进行分析

-------------------------mysql---创建表----------------------------------
mysql -uroot -p123456
create database if not exists stucour;
use stucour;
create table if not exists student (
id int,
name varchar(20),
hoby varchar(20),
primary key (id)
);

insert into student (id,name,hoby)
values (001,"wangwu","football");

select * from student;

create table if not exists course(
id int,
name varchar(20),
teacher varchar(20),
primary key (id)
);
insert into course values (0001,"math","teacherwang");
insert into course values (0002,"chinese","teacherli");

create table if not exists stu_course(
stuid varchar(20),
courid varchar(20)
);
insert into stu_course values (001,0002);
insert into stu_course values (001,0001);
insert into stu_course values (002,0001);
insert into stu_course values (003,0002);

student.txt course.txt stu_course.txt

-------------------------hive-----创建表------------------------------
show databases;
create database if not exists stucour;
use stucour;
create table student(
id int,
name string,
hoby string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
;
load data local inpath '/opt/datas/student.txt' into table student;
select * from student;

create table if not exists course(
id int,
name string,
teacher string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
load data local inpath '/opt/datas/course.txt' into table course;
truncate table course;
select * from course;


create table if not exists stu_course(
stuid string,
courid string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
load data local inpath '/opt/datas/stu_course.txt' into table stu_course;
----------------sqoop-----import-sql-to-hive-------------------
bin/sqoop import \
--connect jdbc:mysql://master:3306/stucour \
--username root \
--password 123456 \
--table student \
--fields-terminated-by '\t' \
--delete-target-dir \
--num-mappers 1 \
--hive-import \
--hive-database stucour \
--hive-table student

bin/sqoop import \
--connect jdbc:mysql://master:3306/stucour \
--username root \
--password 123456 \
--table course \
--fields-terminated-by '\t' \
--delete-target-dir \
--num-mappers 1 \
--hive-import \
--hive-database stucour \
--hive-table course

bin/sqoop import \
--connect jdbc:mysql://master:3306/stucour \
--username root \
--password 123456 \
--table stu_course \
--fields-terminated-by '\t' \
--delete-target-dir \
--num-mappers 1 \
--hive-import \
--hive-database stucour \
--hive-table stu_course
--------------------------------------------查询hive---------------------------------------
select * from student,stu_course where student.id=stu_course.stuid;
select * from student,stu_course,course where student.id=stu_course.stuid and stu_course.stuid=course.id;

//查询多个表
select distinct id,name,hoby from student;//查询去重

insert overwrite table student select distinct id,name,hoby from student; //将各字段去重后覆盖到原表中

//将HQL查询结果写入MYSQL数据库中

bin/hive -e "select * from student ;" > /opt/datas/studenthive.txt  //将HIVE分析的数据导入到本地
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值