一,选择数据库,这里使用标准mysql sakila数据库

mysql -u root -D sakila -p

二。首先尝试把表中的数据导入到hdfs文件中,这样后续就可以使用spark来dataframe或者rdd来处理数据

sqoop import --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --table rental --target-dir "SqoopImport/rental" --num-mappers 1

\\SqoopImport 目录必须有,rental 目录可以不存在

三。如果要导入到hive里面,要使用 --warehouse参数。

sqoop import --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --table rental --warehouse-dir "/user/hive/warehouse/sakila.db" --num-mappers 2

\\因为之前我们已经全表导入过一次了,会提示文件已经存在的错误

hadoop fs -mv /user/hive/warehouse/sakila.db/rental /user/hive/warehouse/sakila.db/rental2
\\把原来的目录移走
sqoop import --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --table rental --warehouse-dir "/user/hive/warehouse/sakila.db" --num-mappers 2

四。也可以通过sqoop命令来查看hive的元数据库。

1.查看多少个数据库

sqoop list-databases --connect "jdbc:mysql://host03.xyy:3306" --username root --password root

2.查看多少给表

sqoop list-tables --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root

3.sqoop执行select语句。

sqoop eval --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --query "select * from rental limit 10"

五。导入hive或者hdfs中使用追加模式

sqoop import --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --table rental --where "date(return_date) < '2005-07-30'" --warehouse-dir "/user/hive/warehouse/sakila.db" --append --num-mappers 2

Total MapReduce CPU Time Spent: 9 seconds 250 msec
OK
23191
Time taken: 30.055 seconds, Fetched: 1 row(s)
hive>
\\原来数据hive里面的表格数据是16044条,重新append一批数据以后百年城23191条
\\apend 也可以应用 hdfs文件中,和target-dir配合使用

六。导入hdfs和hive中使用删除模式

sqoop import --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --table rental --where "date(return_date) < '2005-07-30'" --warehouse-dir "/user/hive/warehouse/sakila.db" --delete-target-dir --num-mappers 2

OK
7147
Time taken: 32.115 seconds, Fetched: 1 row(s)
hive>
\\--delete-target-dir 是删除模式导入,清空原来的数据,这个命令也可以在导入hdfs下使用

注意以上例子中都是使用了where条件的导入。

七。关于导入数据的并行数量

前面几个例子都是使用了--num-mapper 2 也就是两个并行。

实际上默认是因为原来的mysql表中有主键,如果没有主键是不能直接指定并行为2 的。因为系统不知道怎么切割数据。

如果要并行需要使用另外一个参数

在mysql中执行复制一个表格
create table customer_copy like customer;
insert into customer_copy select * from customer

sqoop import --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --table customer_copy --warehouse-dir "/user/hive/warehouse/sakila.db" --delete-target-dir -split-by address_id --num-mappers 2

八。sqoop全表导入

//导入数据库mysql到hive

sqoop import-all-tables --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --hive-import --hive-database sakila  --m 2

如果其中部分表格没有主键 并行就有问题。需要使用一个参数 --autoreset-to-one-mapper

sqoop import-all-tables --connect "jdbc:mysql://host03.xyy:3306/sakila" --username root --password root --warehouse-dir  "SqoopImport/sakila"   --autoreset-to-one-mapper --m 2

这样对于没有主键的自动变成一个map去处理

九。文件格式

通过参数决定每个表存入hdfs中的格式

--as-textfile  (default)
--as-avrodatafile
--as-sequencefile
--as-parquetfile

10.sqoop import参数列表

Argument Description
--append Append data to an existing dataset in HDFS
--as-avrodatafile Imports data to Avro Data Files
--as-sequencefile Imports data to SequenceFiles
--as-textfile Imports data as plain text (default)
--as-parquetfile Imports data to Parquet Files
--boundary-query <statement> Boundary query to use for creating splits
--columns <col,col,col…> Columns to import from table
--delete-target-dir Delete the import target directory if it exists
--direct Use direct connector if exists for the database
--fetch-size <n> Number of entries to read from database at once.
--inline-lob-limit <n> Set the maximum size for an inline LOB
-m,--num-mappers <n> Use n map tasks to import in parallel
-e,--query <statement> Import the results of statement.
--split-by <column-name> Column of the table used to split work units. Cannot be used with --autoreset-to-one-mapper option.
--split-limit <n> Upper Limit for each split size. This only applies to Integer and Date columns. For date or timestamp fields it is calculated in seconds.
--autoreset-to-one-mapper Import should use one mapper if a table has no primary key and no split-by column is provided. Cannot be used with --split-by <col>option.
--table <table-name> Table to read
--target-dir <dir> HDFS destination dir
--temporary-rootdir <dir> HDFS directory for temporary files created during import (overrides default "_sqoop")
--warehouse-dir <dir> HDFS parent for table destination
--where <where clause> WHERE clause to use during import
-z,--compress Enable compression
--compression-codec <c> Use Hadoop codec (default gzip)
--null-string <null-string> The string to be written for a null value for string columns
--null-non-string <null-string> The string to be written for a null value for non-string columns

大数据入门到精通18--sqoop 导入关系库到hdfs中和hive表中的更多相关文章

  1. 大数据入门到精通1--大数据环境下的基础文件HDFS 操作

    1.使用hdfs用户或者hadoop用户登录 2.在linux shell下执行命令 hadoop fs -put '本地文件名' hadoop fs - put '/home/hdfs/sample ...

  2. 大数据入门到精通19--mysql 数据导入到hive数据中

    一.正常按照数据库和表导入 \\前面介绍了通过底层文件得形式导入到hive的表中,或者直接导入到hdfs中,\\现在介绍通过hive的database和table命令来从上层操作.sqoop impo ...

  3. 大数据入门到精通13--为后续和MySQL数据库准备

    We will be using the sakila database extensively inside the rest of the course and it would be great ...

  4. 大数据入门到精通2--spark rdd 获得数据的三种方法

    通过hdfs或者spark用户登录操作系统,执行spark-shell spark-shell 也可以带参数,这样就覆盖了默认得参数 spark-shell --master yarn --num-e ...

  5. 大数据学习之路之sqoop导入

    按照网上的代码导入 hadoop(十九)-Sqoop数据清洗 - 简书 (jianshu.com) ./sqoop import --connect "jdbc:mysql://192.16 ...

  6. 大数据入门到精通16--hive 的条件语句和聚合函数

    一.条件表达 case when ... then when .... then ... when ... then ...end select film_id,rpad(title,20," ...

  7. 大数据入门到精通12--spark dataframe 注册成hive 的临时表

    一.获得最初的数据并形成dataframe val ny= sc.textFile("data/new_york/")val header=ny.firstval filterNY ...

  8. 大数据入门到精通11-spark dataframe 基础操作

    // dataframe is the topic 一.获得基础数据.先通过rdd的方式获得数据 val ny= sc.textFile("data/new_york/")val ...

  9. 大数据入门到精通10--spark rdd groupbykey的使用

    //groupbykey 一.准备数据val flights=sc.textFile("data/Flights/flights.csv")val sampleFlights=sc ...

随机推荐

  1. php.ini文件下载

    该php.ini文件为修改版,完美运行,存于360云盘.

  2. 工控随笔_12_西门子_WinCC的VBS脚本_03_变量类型

    说到编程语言,总是绕不开数据类型,因为数据类型决定了数据可以进行什么样的操作.同时数据类型 从广义上来说是一种数据结构,在过程式编程的过程中,曾经有过这样一种说法: 程序 = 数据结构 + 算法 可见 ...

  3. python基础知识12---函数2

    阅读目录 一 引子 二 定义函数 三 调用函数 四 函数的参数 五 练习题 一 引子 1 为何要用函数之不用函数的问题 #1.代码的组织结构不清晰,可读性差 #2.遇到重复的功能只能重复编写实现代码, ...

  4. [蓝桥杯]PREV-26.历届试题_最大子阵

    问题描述 给定一个n*m的矩阵A,求A中的一个非空子矩阵,使这个子矩阵中的元素和最大. 其中,A的子矩阵指在A中行和列均连续的一块. 输入格式 输入的第一行包含两个整数n, m,分别表示矩阵A的行数和 ...

  5. RTP协议

    RTP协议格式: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+ ...

  6. springboot集成schedule

    背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...

  7. .net 多线程之async await

    主线程遇到await 关键字后就交给子线程执行了 先定义一个task 可以让主线程和子线程同时执行,通过await关键字可以让主线程等待子线程执行完毕,await后面的代码可以视为异步方法的回调,可以 ...

  8. RocketMQ消费批拉超过32不生效

    由于一些原因,我需要RocketMQ消费的时候,一批拉400条,一批处理400条.设置如下: 为了简单验证是否正确,消费如下: 直接通过打印msgs.size()观察情况即可. 现象 实验的topic ...

  9. Ehcart整合百度地图

    最近上班有些时间,学习了一下Ehcart的知识,自己制作了一份Ehcart整合百度地图的示例代码. GItHub地址:https://github.com/TianYanFd/tianjin-powe ...

  10. Oracle常见错误:ORA-06550、ORA-00911、ORA-02085

    ORA-06550:检查标点符号,如果是在写存储过程时候,切记每行每条语句都应该以“;”结束 ORA-00911: invalid character 包含中文报错 ORA-02085:数据库连接 X ...