【问题标题】:How to convert mysql DDL into hive DDL如何将 mysql DDL 转换为 hive DDL
【发布时间】:2012-12-26 16:53:52
【问题描述】:

给定一个包含用于在 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,
);

【问题讨论】:

    标签: hadoop schema translation hive


    【解决方案1】:

    我其实以为这不被支持,但是在查看了这里的源代码之后,我在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--outdir 一起生成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

    【讨论】:

    • 感谢您提供非常有用的信息。在没有安装 sqoop 的情况下,我最终编写了一个简单的解释器,它采用 mysql 表创建模式的脚本并打印配置单元模式。从你提到的java类,我来到了org.apache.sqoop.hive.HiveTypes,里面有sql类型和hive的映射关系。
    • 似乎没有这样的选项 --generate-only 可用,至少在我正在使用的 Sqoop 1.4.5 中没有。也许它在一段时间前被弃用了?通过检查代码,我发现为了使用不导入的 DDL 生成,您需要使用 codegen 工具。它不在导入工具中 - 因为那里的所有 HiveImport 实例都是用用于 generateOnly 的硬编码“假”标志构造的。唯一用 'true' 构造的地方是在 codegen 中,见这里:codatlas.com/github.com/apache/sqoop/trunk/src/java/org/apache/…
    【解决方案2】:

    或者,可以使用 create-hive-table 工具来执行此操作。 create-hive-table 工具使用基于先前导入到 HDFS 的数据库表或计划导入的数据库表的表定义填充 Hive 元存储。这有效地执行了 sqoop-import --hive-import 步骤,而无需运行前面的导入。例如,

    sqoop create-hive-table --connect jdbc:mysql://localhost/demo -username root --table t2 --fields-terminated-by ',' --hive-table t2

    此命令将根据 MySQL 中同一张表的 schema 创建一个空白的 hive 表 t2,而不导入数据。

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 2013-02-04
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多