给定一个包含用于在MySQL数据库中创建表的DDL的SQL脚本,我想将该脚本转换为Hive DDL,以便可以将表创建为Hive。我本可以自己编写一个解释器,但由于可能是DDL的新手,所以可能会想念一些细节(例如,数据格式转换,int,bigint,时间,日期等)。

我已经看到了这个线程How to transfer mysql table to hive?,其中提到了sqoop http://archive.cloudera.com/cdh/3/sqoop/SqoopUserGuide.html。但是,从我的 Angular 来看,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,
);

最佳答案

我实际上以为不支持此功能,但是在查看Source之后,这就是我在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一起在cunjunction中使用的选项--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

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

10-12 23:31