当我们使用sqoop import
将RDBMS表传输到HIVE时,表的约束如主键是否仍然存在?
即,作为主键的表的列是否仍然是HIVE
的主键。这些信息会在Hive Metastore中吗?
非常感谢。
正如您在下面的Hive QL官方文档的链接中所看到的,自Hive版本2.1.0以来已添加了PRIMARY和FOREIGN约束。 Hive QL
因此,我假设使用sqoop将表导入Hive时,PRIMARY和FOREIGN Keys约束将保留。
我测试了一个sqoop导入的MySQL数据库,我可以看到在导入过程中没有维护PRIMARY KEY CONSTRAINT。
MySQL表格式:
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中描述表时,我看不到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
总结一下:在将RDBMS数据导入Hive时,Sqoop不保留PK约束