SQOOP -- 在 SQL Server 中使用 SCHEMA 查询

Posted

技术标签:

【中文标题】SQOOP -- 在 SQL Server 中使用 SCHEMA 查询【英文标题】:SQOOP --query with SCHEMA in SQL Server 【发布时间】:2017-04-21 03:36:44 【问题描述】:

我正在尝试使用 sqoop 中的--query 选项从 SQL Server 导入数据。我关心的是,我们如何在 SQL Server 中声明与 --query 一起使用的架构。

我的脚本:

sqoop \
--options-file sqoop/aw_mssql.cfg \
--query "select BusinessEntityId, LoginID, cast(OrganizationNode as string) from Employee where \$CONDITIONS" \
--hive-table employees \
--hive-database mssql \
-- --schema=HumanResources

仍然产生错误

无效的对象名称“员工”

也试过了

--connect "jdbc:sqlserver://192.168.1.17;database=AdventureWorks;schema=HumanResources"

但这也失败了。

【问题讨论】:

【参考方案1】:

你可以试试下面的代码:

sqoop import \
--connect jdbc:sqlserver://192.168.1.17;database=AdventureWorks \
--username "Your User" \
--password "Your Password" \
--driver  com.microsoft.sqlserver.jdbc.SQLServerDriver \
--verbose  \
--query "select BusinessEntityId, LoginID, cast(OrganizationNode as string) from HumanResources.Employee where \$CONDITIONS" \
--split-by "EmpID" \ 
--where " EmpID='Employee ID' " \
-m 1  \
--target-dir /user/cloudera/ingest/raw/Employee\
--fields-terminated-by "," \
--hive-import \
--create-hive-table \
--hive-table mssql.employees \
    hive-import – 将表导入 Hive(使用 Hive 的默认分隔符 如果没有设置。) create-hive-table - 它将创建新的 HIBE 表。 Note:工作 如果 Hive 表已经存在,将会失败。它适用于此 案例。 hive-table – 指定 <db_name>.<table_name>

【讨论】:

嗨,我担心的是源表 EMPLOYEE 位于 AdventureWorks 数据库下的 HumanResources 模式下。如何声明脚本以使用 HumanResources 模式作为源。谢谢 然后在 --query 参数中尝试使用HumanResources.Employee 糟糕!我的错! HumanResources.Employee 是解决方案。只是没有意识到 Parquet 不支持二进制。非常感谢! 你能试试这个,让我知道错误:sqoop import --connect 'jdbc:sqlserver://192.168.1.17;username=myuser;password=mypassword;database=AdventureWorks' --table HumanResources.Employee --target-dir /data/Employee --split-by EmployeeID【参考方案2】:

您使用的 sqoop 命令缺少一些内容。首先,您需要指定这是一个 sqoop 导入作业。除此之外,您的查询需要有一个连接字符串。此外,我不知道您在选项文件中传递了哪些参数,所以如果您发布了详细信息,它会更容易,而且我不确定-- --schema=HumanResources 的事情,因为我没有看到它。一个正确的工作 sqoop 示例查询是:

sqoop import --connect <connection string> --username <username> --password <password> --query <query> --hive-import --target-table <table_name> -m <no_if_mappers

此外,在使用--query 工具时请记住这一点,您无需指定--table 工具,否则会抛出错误。

【讨论】:

问题是,我的源表位于 HumanResources 架构下,而不是默认的 dbo 架构下。基本上它就像 AdventureWorks.HumanResources.Employee。如何将脚本设置为在 HumanResources 架构下进行查询?谢谢 你应该试试 --query 'SELECT * FROM HumanResources.Employee WHERE $CONDITIONS'【参考方案3】:

-schema 可以与-table 一起使用,但不能与-query 一起使用。想想这意味着什么,它需要解析查询的文本并用两部分名称替换每个不合格的表引用,而不是已经是两部分、三部分或四部分名称的表引用。并完全匹配后端(本例中为 SQL Server)的语法规则。这是不可行的。

在查询中明确指定架构:

select BusinessEntityId, LoginID, cast(OrganizationNode as string)
from HumanResources.Employee 
where ...

【讨论】:

尝试了 HumanResources.Employee 但仍然失败。 ERROR manager.SqlManager: Error execution statement: com.microsoft.sqlserver.jdbc.SQLServerException: Type string is not a defined system type. 是的,请使用有效的 SQL Server 语法。请参阅Transact-SQL reference。 T-SQL 中没有string 这样的类型来 CAST 到。 糟糕!我的错! HumanResources.Employee 是解决方案。只是没有意识到 Parquet 不支持二进制。非常感谢!

以上是关于SQOOP -- 在 SQL Server 中使用 SCHEMA 查询的主要内容,如果未能解决你的问题,请参考以下文章