Apache Sqoop Cookbook 英文版-翻译学习!!
Sqoop 主要命令
19/05/31 05:49:02 INFO sqoop.Sqoop: Running Sqoop version: 1.4.5.2.2.4.2-2
usage: sqoop COMMAND [ARGS]
Available commands:
codegen Generate code to interact with database records
create-hive-table Import a table definition into Hive
eval Evaluate a SQL statement and display the results
export Export an HDFS directory to a database table
help List available commands
import Import a table from a database to HDFS
import-all-tables Import tables from a database to HDFS
job Work with saved jobs
list-databases List available databases on a server
list-tables List available tables in a database
merge Merge results of incremental imports
metastore Run a standalone Sqoop metastore
version Display version information
See 'sqoop help COMMAND' for information on a specific command.
问题
不是导入整张表,需要通过SQL的WHERE子句条件导入不同的数据集。
解决方案
在sqoop命令行参数中使用 --where ,这个参数是针对特定的SQL语句,以达到只导入符合条件的数据。如,对于
cities表中只导入美国的城市,可以使用以下的Sqoop命令:
sqoop import \
--connect jdbc:mysql://localhost:3306/sqoop \
--username sqoop \
--password sqoop \
--table cities \
--warehouse-dir /mydir/test/ \
--where "country='USA'"

讨论
Sqoop将会向每个获取数据的查询传递--where参数,这提供了针对特定数据库的强大的表述能力,任何功能函数,表达式,甚至是自定义的函数都可以使用。由于sql片段将会传递到生成的查询语句中而不需要sqoop的处理。对于任何
有效的sql片段可能会产生一些意想不到的异常而导致了难以调试。对于这些参数可能然sqoop新手产生疑惑。
当使用--where参数时,请记住sqoop会本能的进行并行传输数据。数据将会在几个任务中进行并行的传输,一些耗时
的函数将会对数据造成明显的性能影响,而一些高级函数可能会将某个表锁住,从而阻止Sqoop进行并行数据传输。
这将对传输性能产生影响。所以到导入前,使用高级的过滤功能,执行过滤查询将数据导入到一张临时表中,随后在
使用import命令从临时表将数据导入hadoop,这样做还有一个好处是不需要使用--where参数。
网友评论