首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将mysql DDL转换为hive DDL

如何将mysql DDL转换为hive DDL
EN

Stack Overflow用户
提问于 2013-01-12 10:13:17
回答 2查看 5.8K关注 0票数 1

给定一个包含用于在MySQL数据库中创建表的DDL的SQL脚本,我想将该脚本转换为hive DDL,这样我就可以在Hive中创建表。我可以自己写一个解释器,但我认为可能会遗漏一些细节(例如,数据格式转换、int、bigint、time、date等)。因为我对蜂巢DDL很陌生。

我看过这个提到sqoop http://archive.cloudera.com/cdh/3/sqoop/SqoopUserGuide.html的帖子How to transfer mysql table to hive?。然而,在我看来,sqoop当然会翻译DDL,但只是作为一个中间步骤(因此翻译后的DDL在哪里都找不到)。我是否错过了将MySQL DDL作为输入输出翻译的命令?

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

代码语言:javascript
复制
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将如下所示:

代码语言:javascript
复制
CREATE TABLE user_keyword (
  username string,
  keyword_id int,
);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-12 11:23:00

我实际上认为这是不受支持的,但在查看源代码后,这里是我在HiveImport.java中看到的内容

代码语言:javascript
复制
/**
 * @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");
  }
}

因此,基本上您应该只能使用cunjunction和--outdir中使用的选项--generate-only来生成DDL,并且您的表将在指定的输出目录中创建,并以您的表命名。

例如,基于您提供的链接:

代码语言:javascript
复制
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

票数 2
EN

Stack Overflow用户

发布于 2014-04-22 05:57:57

或者,可以使用create-hive-table工具来实现这一点。create- Hive -table工具使用基于先前导入HDFS的数据库表或计划导入的数据库表的表定义填充配置单元元存储区。这将有效地执行--的--hive- import 步骤,而无需运行之前的导入。例如,

sqoop create-hive-table --connect jdbc:mysql://localhost/demo -username根目录--表t2 --字段--终止于',‘--hive-table t2

此命令将在不导入数据的情况下,基于MySQL中同一个表的架构创建一个空白配置单元表t2。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14289495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档