使用Sqoop导入时处理Hive表中的分区

锡德

我对sqoop导入实用程序有疑问。我了解我们可以运行“ sqoop导入”并从RDBMS(在我的情况下为SQL Server)获取数据,然后将其直接放在配置单元表中(将动态创建)。

我的问题是,如果需要的话,如何使用“ sqoop import”实用程序在此配置单元表中创建分区(可能吗?)。

在完成“ sqoop导入到Hive”之后,我总是看到未分区的Hive表。我的要求是在x,y,z列上有一个分区表。

谢谢,席德

维克兰特·拉纳

您可以将数据直接导入到配置单元表,还可以创建分区表并使用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导入将在蜂巢中创建分区表UDB.EMPLOYEE_PARTITION_TABLE并将分区列创建为EMPLOYEE_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

您需要确保几件事。当您使用hive-import时,您的hive-partition-key列名称不应属于数据库表。否则您将得到以下错误。

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

在sqoop导入中指定查询时,将分区列保留在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

让我知道这是否适合您。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章