本文翻译自官网:Use Hive connector in scala shell https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/hive/scala_shell_hive.html

Flink Scala Shell 是尝试 flink 的便捷方法。您也可以在 scala shell 中使用 hive,而不是在pom文件中指定 hive 依赖关系,打包程序并通过flink run命令提交。为了在scala shell中使用 hive 连接器,您需要将以下hive 连接器依赖项放在flink dist的lib文件夹下。

flink-connector-hive_ {scala_version}-{flink.version} .jar

flink-hadoop-compatibility_ {scala_version}-{flink.version} .jar

flink-shaded-hadoop-2-uber- {hadoop.version}-{flink-shaded.version} .jar

hive-exec-2.x.jar(对于Hive 1.x,您需要复制hive-exec-1.x.jar,hive-metastore-1.x.jar,libfb303-0.9.2.jar和libthrift- 0.9.2.jar)

然后,您可以在scala shell 中使用 hive 连接器,如下所示:

Scala-Flink> import org.apache.flink.table.catalog.hive.HiveCatalog
Scala-Flink> val hiveCatalog = new HiveCatalog("hive", "default", "<Replace it with HIVE_CONF_DIR>", "2.3.4");
Scala-Flink> btenv.registerCatalog("hive", hiveCatalog)
Scala-Flink> btenv.useCatalog("hive")
Scala-Flink> btenv.listTables
Scala-Flink> btenv.sqlQuery("<sql query>").toDataSet[Row].print()