我想在pyspark中使用matplotlib.bblpath或shapely.geometry库。
当我尝试导入其中任何一个时,我收到以下错误:
>>> from shapely.geometry import polygon
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named shapely.geometry
我知道该模块不存在,但是如何将这些软件包带到我的pyspark库?
答案 0 :(得分:15)
在Spark上下文中尝试使用:
SparkContext.addPyFile("module.py") # also .zip
,引自docs:
为要在此上执行的所有任务添加.py或.zip依赖项 将来SparkContext。传递的路径可以是本地路径 文件,HDFS(或其他Hadoop支持的文件系统)中的文件,或 HTTP,HTTPS或FTP URI。
答案 1 :(得分:7)
这就是我在AWS EMR集群中使用它的方式(在任何其他集群中也应如此)。我创建了以下shell脚本并将其作为bootstrap-actions执行:
#!/bin/bash
# shapely installation
wget http://download.osgeo.org/geos/geos-3.5.0.tar.bz2
tar jxf geos-3.5.0.tar.bz2
cd geos-3.5.0 && ./configure --prefix=$HOME/geos-bin && make && make install
sudo cp /home/hadoop/geos-bin/lib/* /usr/lib
sudo /bin/sh -c 'echo "/usr/lib" >> /etc/ld.so.conf'
sudo /bin/sh -c 'echo "/usr/lib/local" >> /etc/ld.so.conf'
sudo /sbin/ldconfig
sudo /bin/sh -c 'echo -e "\nexport LD_LIBRARY_PATH=/usr/lib" >> /home/hadoop/.bashrc'
source /home/hadoop/.bashrc
sudo pip install shapely
echo "Shapely installation complete"
pip install https://pypi.python.org/packages/74/84/fa80c5e92854c7456b591f6e797c5be18315994afd3ef16a58694e1b5eb1/Geohash-1.0.tar.gz
#
exit 0
注意:不是作为引导操作运行,而是可以在群集中的每个节点中独立执行此脚本。我测试了两种情况。
以下是一个示例pyspark和匀称代码(Spark SQL UDF),以确保上述命令按预期工作:
Python 2.7.10 (default, Dec 8 2015, 18:25:23)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 1.6.1
/_/
Using Python version 2.7.10 (default, Dec 8 2015 18:25:23)
SparkContext available as sc, HiveContext available as sqlContext.
>>> from pyspark.sql.functions import udf
>>> from pyspark.sql.types import StringType
>>> from shapely.wkt import loads as load_wkt
>>> def parse_region(region):
... from shapely.wkt import loads as load_wkt
... reverse_coordinate = lambda coord: ' '.join(reversed(coord.split(':')))
... coordinate_list = map(reverse_coordinate, region.split(', '))
... if coordinate_list[0] != coordinate_list[-1]:
... coordinate_list.append(coordinate_list[0])
... return str(load_wkt('POLYGON ((%s))' % ','.join(coordinate_list)).wkt)
...
>>> udf_parse_region=udf(parse_region, StringType())
16/09/06 22:18:34 WARN ObjectStore: Version information not found in metastore. hive.metastore.schema.verification is not enabled so recording the schema version 1.2.0
16/09/06 22:18:34 WARN ObjectStore: Failed to get database default, returning NoSuchObjectException
>>> df = sqlContext.sql('select id, bounds from <schema.table_name> limit 10')
>>> df2 = df.withColumn('bounds1', udf_parse_region('bounds'))
>>> df2.first()
Row(id=u'0089d43a-1b42-4fba-80d6-dda2552ee08e', bounds=u'33.42838509594465:-119.0533447265625, 33.39170168789402:-119.0203857421875, 33.29992542601392:-119.0478515625', bounds1=u'POLYGON ((-119.0533447265625 33.42838509594465, -119.0203857421875 33.39170168789402, -119.0478515625 33.29992542601392, -119.0533447265625 33.42838509594465))')
>>>
谢谢, Hussain Bohra
答案 2 :(得分:1)
这是独立的(即笔记本电脑/台式机)还是集群环境(例如AWS EMR)?
如果在您的笔记本电脑/台式机上,pip install shapely
应该可以正常使用。您可能需要检查环境变量以获取默认的python环境。例如,如果您通常使用Python 3但是使用Python 2进行pyspark,则pyspark的形状不可用。
如果在AWS EMR等集群环境中,您可以尝试:
import os
def myfun(x):`
os.system("pip install shapely")
return x
rdd = sc.parallelize([1,2,3,4]) ## assuming 4 worker nodes
rdd.map(lambda x: myfun(x)).collect()
## call each cluster to run the code to import the library
“我知道该模块不存在,但我想知道如何将这些软件包带到我的pyspark库中。”
在EMR上,如果您希望使用所需的任何其他库和配置预先准备pyspark,则可以使用引导步骤进行这些调整。除此之外,你不能在没有编译Scala中的Spark的情况下向pyspark“添加”一个库(如果你不熟悉SBT,那将会很痛苦。)
答案 3 :(得分:0)
我使用SparkContext从AWS Docs找到了一个很棒的解决方案。我可以使用以下方法添加Pandas和其他软件包:
Using SparkContext to add packages to notebook with PySpark Kernel in EMR
sc.install_pypi_package(“ pandas == 0.25.1”)