Sqoop 将新添加的列导入 mysql table 到现有配置单元 table

Sqoop import newly added column to mysql table to existing hive table

我在 mysql 中进行了 table 测试,如下所示:

id  name  address
1  Km  sky
2  hd  heaven
3  Ab  null
4  en  null

现在我做了如下的sqoop导入

sqoop import--connect jdbc:mysql://XXXXXX/testing --username XXXX --password XXXX --query "select * from  testing.test where $CONDITIONS" --null-string '' --null-non-string '' -m 1\ 
--hive-import --hive-database testing --hive-table test --create-hive-table --target-dir  /user/hive/warehouse/testing.db/test

我得到了想要的结果。

然后我们在 mysql table 中添加了一个新列,其中包含额外的 2 行

id  name  address  nation

1  Km  sky  null
2  hd  heaven  null
3  Ab  null  null
4  en  null  null
5  abc efd  USA
6  fge cde  UK

现在我想要现有配置单元 table 更新上面的列和行。我完成了以下 sqoop 作业

Sqoop 作业:

sqoop job --create sqoop_test -- import --connect jdbc:mysql:xxxxxxx/testing --username XXXXX --password XXXX --query "SELECT * from testing.test WHERE $CONDITIONS" --incremental append\ 
--check-column id --last-value "3" --split-by 'id' --target-dir  /user/hive/warehouse/testing.db/test 

但是当我查询配置单元 table 时,我得到的新行结果为空,新列没有显示。喜欢下面

id  name  address

NULL  NULL  NULL
NULL  NULL  NULL
1  Km  sky
2  hd  heaven
3  Ab  
4  en  

我们如何将新列附加到现有的 table 中并添加新行?

或者是我使用的方法完全错误。请告诉我

您的假设是错误的,这是因为您导入的数据布局不同。您创建的第一个 table 有 3 列,而在第二次导入中,您导入了 4 列,因此,Hive 无法解析这些新记录并简单地为所有列打印 null。如果您没有充分的理由以文本文件格式导入数据,我建议您在 avro 中创建 table 并使用模式演变功能来添加新列。

当您在 avro 中导入数据时,Sqoop 会自动为您生成方案。所以你唯一需要做的就是创建一个指向导入数据的 table 并使用生成的模式。在未来导入新字段的情况下,您将需要添加具有有效默认值的字段或使用默认值使它们可为空,如下所示(例如字符串列)

{ "name": "newcolumnname", "type": [ "null", "string" ], "default": "null" },

甚至指定其他有效的默认值

{ "name": "newcolumnname", "type": [ "string" ], "default": "val1" }, //default value 1
{ "name": "newcolumnname", "type": [ "string" ], "default": "" }, //default value empty