gpt4 book ai didi

hadoop - 如何将mysql DDL转换成hive DDL

转载 作者:可可西里 更新时间:2023-11-01 15:03:13 29 4
gpt4 key购买 nike

给定一个包含用于在 MySQL 数据库中创建表的 DDL 的 SQL 脚本,我想将该脚本转换为 Hive DDL,以便我可以将表创建到 hive 中。我本可以自己编写一个解释器,但我认为我可能会错过一些细节(例如数据格式转换、int、bigint、时间、日期等),因为我对 hive DDL 还很陌生。

我看过这个帖子 How to transfer mysql table to hive? , 其中提到了 sqoop http://archive.cloudera.com/cdh/3/sqoop/SqoopUserGuide.html .然而,据我所知,sqoop 肯定会翻译 DDL,但只是作为一个中间步骤(因此翻译后的 DDL 无处可寻)。我是否缺少将 MySQL DDL 作为输入输出翻译的命令?

例如,我的 MySQL DDL 如下所示:

CREATE TABLE `user_keyword` (
`username` varchar(32) NOT NULL DEFAULT '',
`keyword_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`username`,`keyword_id`),
KEY `keyword_id` (`keyword_id`),
CONSTRAINT `analyst_keywords_ibfk_1` FOREIGN KEY (`keyword_id`) REFERENCES `keywords` (`keyword_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Hive DDL 输出如下:

CREATE TABLE user_keyword (
username string,
keyword_id int,
);

最佳答案

我实际上认为这不受支持,但在查看源代码后,我在 HiveImport.java 中看到了:

/**
* @return true if we're just generating the DDL for the import, but
* not actually running it (i.e., --generate-only mode). If so, don't
* do any side-effecting actions in Hive.
*/
private boolean isGenerateOnly() {
return generateOnly;
}

/**
* @return a File object that can be used to write the DDL statement.
* If we're in gen-only mode, this should be a file in the outdir, named
* after the Hive table we're creating. If we're in import mode, this should
* be a one-off temporary file.
*/
private File getScriptFile(String outputTableName) throws IOException {
if (!isGenerateOnly()) {
return File.createTempFile("hive-script-", ".txt",
new File(options.getTempDir()));
} else {
return new File(new File(options.getCodeOutputDir()),
outputTableName + ".q");
}
}

所以基本上你应该只能使用选项--generate-only 来生成 DDL在以您的表指定和命名的输出目录中。

例如根据您提供的链接:

sqoop import --verbose --fields-terminated-by ',' --connect jdbc:mysql://localhost/test --table employee --hive-import --warehouse-dir /user/hive/warehouse --fields-terminated-by ',' --split-by id --hive-table employee --outdir /tmp/mysql_to_hive/ddl --generate-only

将创建 /tmp/mysql_to_hive/ddl/employee.q

关于hadoop - 如何将mysql DDL转换成hive DDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14289495/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com