使用 sqoop 从 RDBMS 导入 table 到 HIVE 后,约束是否仍然存在?

Will the constraints remain after Importing table from RDBMS to HIVE with sqoop?

当我们使用sqoop import将RDBMS table传输到HIVE时,table的constraints是否会如主键保留?

即作为 主键 的 table 的列是否会保留为 HIVE 的主键。此信息会在 Hive Metastore 中吗?

非常感谢。

正如您在下面的 Hive QL 官方文档的 link 中看到的,从 Hive 版本 2.1.0 开始添加了 PRIMARY 和 FOREIGN 约束。 Hive QL

因此,我假设当您使用 sqoop 将 tables 导入 Hive 时,PRIMARY 和 FOREIGN Keys 约束将保留。

我测试了 MySQL 数据库的 sqoop 导入,我可以看到在导入过程中没有维护 PRIMARY KEY CONSTRAINT。

MySQL Table 格式:

    mysql> show create table employees;
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                                                                                                                                                                                                   |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| employees | CREATE TABLE `employees` (
  `emp_no` int(11) NOT NULL,
  `birth_date` date NOT NULL,
  `first_name` varchar(14) NOT NULL,
  `last_name` varchar(16) NOT NULL,
  `gender` enum('M','F') NOT NULL,
  `hire_date` date NOT NULL,
  PRIMARY KEY (`emp_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

已使用以下命令将数据从 MySQL 导入 Hive:

sqoop import --connect jdbc:mysql://localhost/employees --username root --password password --table employees --hive-import --create-hive-table --hive-table employees

当我描述hive中的table时,我看不到PRIMARY KEY CONSTRAINT

hive> show create table employees;
OK
CREATE TABLE `employees`(
  `emp_no` int, 
  `birth_date` string, 
  `first_name` string, 
  `last_name` string, 
  `gender` string, 
  `hire_date` string)
COMMENT 'Imported by sqoop on 2019/03/18 00:24:11'
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
WITH SERDEPROPERTIES ( 
  'field.delim'='', 
  'line.delim'='\n', 
  'serialization.format'='') 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://localhost:9000/user/hive/warehouse/employees'
TBLPROPERTIES (
  'transient_lastDdlTime'='1552865076')
Time taken: 1.304 seconds, Fetched: 22 row(s)

我插入了一个具有相同员工编号的新行来检查 Hive 是否管理 PK 约束。如您所见,已添加新行:

hive> insert into employees values (10001, "1986-04-17", "Hichem", 
"BOUSSETTA", "M", "2014-09-91");
Moving data to directory hdfs://localhost:9000/user/hive/warehouse/employees/.hive-staging_hive_2019-03-18_00-32-16_851_8569619447966100947-1/-ext-10000
Loading data to table default.employees
MapReduce Jobs Launched: 
Stage-Stage-1: Map: 1   Cumulative CPU: 5.79 sec   HDFS Read: 5080 HDFS Write: 120 SUCCESS
Total MapReduce CPU Time Spent: 5 seconds 790 msec
OK
Time taken: 42.422 seconds
hive> select * from employees;
OK
10001   1986-04-17  Hichem  BOUSSETTA   M   2014-09-91
10001   1953-09-02  Georgi  Facello M   1986-06-26
10002   1964-06-02  Bezalel Simmel  F   1985-11-21

因此得出结论:Sqoop 在将 RDBMS 数据导入 Hive 时不保留 PK 约束