Skip to main content
 首页 » 编程设计

python之在本地测试 Hive + spark python 程序

2024年10月24日10tuyile006

我想用 spark + hive 开发程序并在本地进行单元测试。

有没有办法让 hive 在进程中运行?或者其他有助于单元测试的东西?

我在 Mac 上使用 python 2.7

请您参考如下方法:

编辑:从 spark 2 开始,可以创建一个可在测试中使用的本地配置单元元存储。原始答案在底部。


来自 spark sql programming guide :

When working with Hive, one must instantiate SparkSession with Hive support, including connectivity to a persistent Hive metastore, support for Hive serdes, and Hive user-defined functions. Users who do not have an existing Hive deployment can still enable Hive support. When not configured by the hive-site.xml, the context automatically creates metastore_db in the current directory and creates a directory configured by spark.sql.warehouse.dir, which defaults to the directory spark-warehouse in the current directory that the Spark application is started. Note that the hive.metastore.warehouse.dir property in hive-site.xml is deprecated since Spark 2.0.0. Instead, use spark.sql.warehouse.dir to specify the default location of database in warehouse. You may need to grant write privilege to the user who starts the Spark application.

基本上这意味着如果你不配置 hive,spark 会为你创建一个 Metastore,并将其存储在本地磁盘上。

2 你应该知道的配置:

  • spark.sql.warehouse.dir - spark 配置,指向表中数据在磁盘上的存储位置,即:"/path/to/test/folder/仓库/"
  • javax.jdo.option.ConnectionURL - 这是一个配置单元配置,应该在 hive-site.xml 中设置(或作为系统属性),即: "jdbc:derby:;databaseName=/path/to/test/folder/metastore_db;create=true"

这些不是强制性的(因为它们有默认值),但有时显式设置它们很方便

您需要确保在测试之间清理测试文件夹,以便为每个套件提供干净的环境


原答案:

我建议安装 vagrant包含您计算机上 VM 中的完整(小型)hadoop 集群的框。

你可以在这里找到一个现成的流浪汉:http://blog.cloudera.com/blog/2014/06/how-to-install-a-virtual-apache-hadoop-cluster-with-vagrant-and-cloudera-manager/

这样您的测试就可以在与生产相同的环境中运行