sqoop数据的导入导出

时间:2020-05-20
本文章向大家介绍sqoop数据的导入导出,主要包括sqoop数据的导入导出使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 sqoop数据的导入导出
 2 一:导入到hdfs
 3 1:sqoop安装目录下:通过某一个command来验证sqoop配置是否正确: bin/sqoop help
 4 2:出现以下表示成功:
 5 Available commands:
 6   codegen            Generate code to interact with database records
 7   create-hive-table     Import a table definition into Hive
 8   eval               Evaluate a SQL statement and display the results
 9   export             Export an HDFS directory to a database table
10   help               List available commands
11   import             Import a table from a database to HDFS
12   import-all-tables     Import tables from a database to HDFS
13   import-mainframe    Import datasets from a mainframe server to HDFS
14   job                Work with saved jobs
15   list-databases        List available databases on a server
16   list-tables           List available tables in a database
17   merge              Merge results of incremental imports
18   metastore           Run a standalone Sqoop metastore
19   version            Display version information
20 3:测试Sqoop是否能够成功连接数据库
21 bin/sqoop list-databases --connect jdbc:mysql://hadoop101:3306/ --username root --password 000000
22 出现如下输出:
23 information_schema
24 metastore
25 mysql
26 oozie
27 performance_schema
28 4: 确定Mysql服务开启正常
29  在Mysql中新建一张表并插入一些数据
30 mysql -uroot -p000000
31 create database company;
32 use company;
33 create table company.staff(id int(4) primary key not null auto_increment, name varchar(255), sex varchar(255));
34 insert into company.staff(name, sex) values('Thomas', 'Male');
35 insert into company.staff(name, sex) values('Catalina', 'FeMale');
36 5:在/opt/module/datas 下创建company.sql  例如:/opt/module/datas/aaa.sql  把第四步mysql脚本放进去
37 6: 执行命令:mysql -u root -p000000    接着:source /opt/module/datas/company.sql  存入数据库 
38 进sqlyog看产生的数据库和表  
39 7:开启hdfs  在hadoop安装目录下输入命令:
40 进hadoop页面 
41 bin/sqoop import \
42 --connect jdbc:mysql://hadoop101:3306/company \
43 --username root \
44 --password 000000 \
45 --table staff \
46 --target-dir /user/company \
47 --delete-target-dir \
48 --num-mappers 2 \
49 --split-by id \
50 --fields-terminated-by "\t"
51 
52 
53 一:导出到sql
54 1:将导入的数据下载   放入/opt/module/datas/xxxHDFS
55 2:把文件上传到hdfs的user目录下:
56 hadoop fs -put /opt/module/datas/xxxHDFS /user/
57 3:在数据库创建--create table company.emp1(id int(4) primary key not null auto_increment, name varchar(255), sex varchar(255));
58 4:在sqoop安装路径下执行
59 bin/sqoop export \
60 --connect jdbc:mysql://hadoop101:3306/company \
61 --username root \
62 --password 000000 \
63 --table emp \
64 --num-mappers 2 \
65 --export-dir /user/empHDFS \
66 --input-fields-terminated-by "\t"
67 5:查看数据库数据
68 6:结束:

原文地址:https://www.cnblogs.com/xjqi/p/12923000.html