使用sqoop导入时处理配置单元表中的分区

e0bqpujr  于 2021-06-03  发布在  Sqoop
关注(0)|答案(1)|浏览(319)

我有一个关于sqoop导入实用程序的问题。我知道我们可以运行一个“sqoop导入”,从rdbms(在我的例子中是sql服务器)获取数据,并直接将其放入一个配置单元表(将动态创建)。
我的问题是,如果必须的话,如何使用“sqoop import”实用程序在这个配置单元表中创建分区(可能吗?)。
在“sqoop导入到配置单元”完成之后,我总是看到一个没有分区的配置单元表。我的要求是在x,y,z列上有一个分区表。。
谢谢,希德

uxhixvfz

uxhixvfz1#

您可以直接将数据导入配置单元表,并可以创建分区表并使用sqoop直接加载它。请查找以下代码:

sqoop import \
--connect "jdbc:sqlserver://yourservername:1433;databases=EMP" \
--connection-manager org.apache.sqoop.manager.SQLServerManager \
--username youruserid \
--password yourpassword \
--fields-terminated-by '|' \
--as-textfile  \
--delete-target-dir \
--target-dir 'hdfspathlocation' \
--hive-import \
--hive-overwrite \
--hive-table UDB.EMPLOYEE_PARTITION_TABLE \
--hive-partition-key EMPLOYEE_CITY \
--hive-partition-value  'NOIDA' \
--num-mappers 1 \
--query "select TEST_EMP_ID,TEST_EMP_NAME,TEST_EMP_DEPARTMENT,TEST_EMP_SALARY,TEST_EMP_CITY FROM EMP.dbo.TEST_EMP_TABLE where TEST_EMP_CITY = 'NOIDA' AND \$CONDITIONS";

如您所见,这个sqoop导入将在hive中创建一个分区表udb.employee\u partition\u table,并创建一个分区列employee\u city。
这将在配置单元中创建一个托管表,其中包含文本格式的数据。下面是配置单元表的架构:

+--------------------------+-----------------------+-----------------------+--+
|         col_name         |       data_type       |        comment        |
+--------------------------+-----------------------+-----------------------+--+
| test_emp_id              | int                   |                       |
| test_emp_name            | string                |                       |
| test_emp_department      | string                |                       |
| test_emp_salary          | int                   |                       |
| test_emp_city            | string                |                       |
| employee_city            | string                |                       |
|                          | NULL                  | NULL                  |
| # Partition Information  | NULL                  | NULL                  |
| # col_name               | data_type             | comment               |
|                          | NULL                  | NULL                  |
| employee_city            | string                |                       |
+--------------------------+-----------------------+-----------------------+--+

0 2018-11-30 00:01 /hdfspathlocation/udb.db/employee_partition_table/employee_city=NOIDA

你需要确定一些事情。使用配置单元导入时,配置单元分区键列名不应是数据库表的一部分。否则你会得到下面的错误。

Imported Failed: Partition key TEST_EMP_CITY cannot be a column to import.

在sqoop import中指定查询时,将分区列保留在select语句的末尾。

select TEST_EMP_ID,TEST_EMP_NAME,TEST_EMP_DEPARTMENT,TEST_EMP_SALARY,TEST_EMP_CITY FROM EMP.dbo.TEST_EMP_TABLE where TEST_EMP_CITY = 'NOIDA' AND \$CONDITIONS

让我知道这是否适合你。

相关问题