我使用 pyspark 进行一些数据处理,并利用 HiveContext 作为窗口函数。
为了测试代码,我使用 TestHiveContext,基本上是从 pyspark 源代码复制实现:
https://spark.apache.org/docs/preview/api/python/_modules/pyspark/sql/context.html
@classmethod
def _createForTesting(cls, sparkContext):
"""(Internal use only) Create a new HiveContext for testing.
All test code that touches HiveContext *must* go through this method. Otherwise,
you may end up launching multiple derby instances and encounter with incredibly
confusing error messages.
"""
jsc = sparkContext._jsc.sc()
jtestHive = sparkContext._jvm.org.apache.spark.sql.hive.test.TestHiveContext(jsc)
return cls(sparkContext, jtestHive)
然后,我的测试继承可以访问上下文的基类。
这在一段时间内工作正常。但是,当我添加更多测试时,我开始注意到一些间歇性进程内存不足的问题。现在我无法在不失败的情况下运行测试套件。
"java.lang.OutOfMemoryError: Java heap space"
每次测试运行后,我都会显式停止 Spark 上下文,但这似乎并没有杀死 HiveContext。因此,我相信每次运行新测试时,它都会不断创建新的 HiveContext,并且不会删除导致内存泄漏的旧测试。
关于如何拆除基类以杀死HiveContext的任何建议?
如果你愿意在所有测试中使用单一实例来保存 Spark/Hive 上下文,则可以执行以下操作。
test_contexts.py:
_test_spark = None
_test_hive = None
def get_test_spark():
if _test_spark is None:
# Create spark context for tests.
# Not really sure what's involved here for Python.
_test_spark = ...
return _test_spark
def get_test_hive():
if _test_hive is None:
sc = get_test_spark()
jsc = test_spark._jsc.sc()
_test_hive = sc._jvm.org.apache.spark.sql.hive.test.TestHiveContext(jsc)
return _test_hive
然后,您只需在测试中导入这些函数即可。
my_test.py:
from test_contexts import get_test_spark, get_test_hive
def test_some_spark_thing():
sc = get_test_spark()
sqlContext = get_test_hive()
# etc