事前準備
事前にローカルマシンでライブラリの入ったzipファイルを作る(ここではazure-storage==0.32.0をインストール。0.33.0だとうまく行かなかった)
midir pylib
pip3 install -t pylib azure-storage==0.32.0
cd pylib
zip -r ../dependances.zip .
方法1
sparkのプログラムの中で以下のようにする。JupiterNotebookでやりたいときはこれしかない。
sc = spark.sparkContext
sc.addPyFile('dependances.zipのおいてあるアドレス')
from azure.storage.blob import BlockBlobService
方法2
spark-submitするときに引数につける
spark-submit --py-files dependances.zip main.py
Sparkプログラムの中から普通に呼べる
from azure.storage.blob import BlockBlobService
追記
oggファイルを追加すると、dynamicに内容が書き換わるライブラリでもOK
参考文献
オライリー「入門PySpark」p.222
動機
HDInsightにほしいpythonパッケージが入っていなかった場合、actionscript でインストールする方法があるが、これはインストールに時間がかかったり、エラーが出やすい。自前でpytonパッケージを追加する方法が判明したのでメモ。
作戦
- ほしいモジュールのoggを作成する。
- spark-submitにくっつける。
spark-submit --py-files mylib.ogg main.py
- main.pyの中から呼べる! main.py
import mylib
手順
## あらすじ
大まかさ作戦として、.whlパッケージが用意されているモジュールはhumptyで.whlから.oggに変換する。
setup.pyが用意されているパッケージは python setup.py bdist_egg
でoggのパッケージを作成する。
詳細
事前にhumptyをインストールしておく。
pip install humpty
まずローカルPCでpythonモジュール(ogg)を作成する。ここではsendgrid==5.6.0のoggモジュールを作成する。
$ mkdir pylib
$ cd pylib
$ pip download sendgrid==5.6.0 -d ./
Collecting sendgrid==5.6.0
Using cached https://files.pythonhosted.org/packages/24/21/9bea4c51f949497cdce11f46fd58f1a77c6fcccd926cc1bb4e14be39a5c0/sendgrid-5.6.0-py2.py3-none-any.whl
Saved ./sendgrid-5.6.0-py2.py3-none-any.whl
Collecting python-http-client>=3.0 (from sendgrid==5.6.0)
Using cached https://files.pythonhosted.org/packages/e0/74/e43e494a44e78a1188552aa9dfc2f738cd4089b6f8a18c49f186f2b841a3/python_http_client-3.1.0.tar.gz
Saved ./python_http_client-3.1.0.tar.gz
Successfully downloaded sendgrid python-http-client
You are using pip version 10.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
$ ls
python_http_client-3.1.0.tar.gz sendgrid-5.6.0-py2.py3-none-any.whl
ここで、sendgridのwhlをoggに変換
$ humpty sendgrid-5.6.0-py2.py3-none-any.whl
Converting sendgrid-5.6.0-py2.py3-none-any.whl to dist/sendgrid-5.6.0-py3.5.egg
Unable to read legacy script metadata, so cannot generate scripts
$ ls
dist python_http_client-3.1.0.tar.gz sendgrid-5.6.0-py2.py3-none-any.whl
$ ls dist
sendgrid-5.6.0-py3.5.egg
$
さらに、sendgridが依存しているpython-http-clientのoggファイルも作成する。
$ tar -xvf python_http_client-3.1.0.tar.gz
x python_http_client-3.1.0/
x python_http_client-3.1.0/PKG-INFO
x python_http_client-3.1.0/MANIFEST.in
x python_http_client-3.1.0/setup.py
x python_http_client-3.1.0/python_http_client.egg-info/
x python_http_client-3.1.0/python_http_client.egg-info/PKG-INFO
x python_http_client-3.1.0/python_http_client.egg-info/SOURCES.txt
x python_http_client-3.1.0/python_http_client.egg-info/top_level.txt
x python_http_client-3.1.0/python_http_client.egg-info/dependency_links.txt
x python_http_client-3.1.0/README.txt
x python_http_client-3.1.0/setup.cfg
x python_http_client-3.1.0/LICENSE.txt
x python_http_client-3.1.0/python_http_client/
x python_http_client-3.1.0/python_http_client/client.py
x python_http_client-3.1.0/python_http_client/__init__.py
x python_http_client-3.1.0/python_http_client/exceptions.py
$ cd python_http_client-3.1.0
$ python setup.py bdist_egg
running bdist_egg
running egg_info
writing python_http_client.egg-info/PKG-INFO
writing top-level names to python_http_client.egg-info/top_level.txt
writing dependency_links to python_http_client.egg-info/dependency_links.txt
reading manifest file 'python_http_client.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'python_http_client.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.6-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/python_http_client
copying python_http_client/client.py -> build/lib/python_http_client
copying python_http_client/__init__.py -> build/lib/python_http_client
copying python_http_client/exceptions.py -> build/lib/python_http_client
creating build/bdist.macosx-10.6-x86_64
creating build/bdist.macosx-10.6-x86_64/egg
creating build/bdist.macosx-10.6-x86_64/egg/python_http_client
copying build/lib/python_http_client/client.py -> build/bdist.macosx-10.6-x86_64/egg/python_http_client
copying build/lib/python_http_client/__init__.py -> build/bdist.macosx-10.6-x86_64/egg/python_http_client
copying build/lib/python_http_client/exceptions.py -> build/bdist.macosx-10.6-x86_64/egg/python_http_client
byte-compiling build/bdist.macosx-10.6-x86_64/egg/python_http_client/client.py to client.cpython-35.pyc
byte-compiling build/bdist.macosx-10.6-x86_64/egg/python_http_client/__init__.py to __init__.cpython-35.pyc
byte-compiling build/bdist.macosx-10.6-x86_64/egg/python_http_client/exceptions.py to exceptions.cpython-35.pyc
creating build/bdist.macosx-10.6-x86_64/egg/EGG-INFO
copying python_http_client.egg-info/PKG-INFO -> build/bdist.macosx-10.6-x86_64/egg/EGG-INFO
copying python_http_client.egg-info/SOURCES.txt -> build/bdist.macosx-10.6-x86_64/egg/EGG-INFO
copying python_http_client.egg-info/dependency_links.txt -> build/bdist.macosx-10.6-x86_64/egg/EGG-INFO
copying python_http_client.egg-info/top_level.txt -> build/bdist.macosx-10.6-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/python_http_client-3.1.0-py3.5.egg' and adding 'build/bdist.macosx-10.6-x86_64/egg' to it
removing 'build/bdist.macosx-10.6-x86_64/egg' (and everything under it)
$ ls dist
python_http_client-3.1.0-py3.5.egg
$
後は作ったpython_http_client-3.1.0-py3.5.eggとsendgrid-5.6.0-py3.5.eggをspark-submitにつけるだけ!