PyProton 是一个实现了fsspec(fsspec 是一个旨在为本地、远程和嵌入式文件系统以及字节存储提供统一 Python 风格接口的项目。)标准接口的Proton Python Client。PyProton 可以让Python用户以他们习惯的方式轻松地访问Proton所支持的各种后端存储(如TOS、HDFS)的同时自动得获得Proton访问存储的各种优势(如稳定性、性能优化等,详情请参考Proton官方文档)。
说明
最新安装包地址:https://proton-pkgs.tos-cn-beijing.volces.com/public/pyproton-2.1.dev0-py3-none-any.whl。
# 可以先通过 pip list 检查是否已预装; pip list | grep 'pyproton' # 将下面的包替换为具体的名称; pip install pyproton-x.whl
说明
参数设置有两种方式:
环境变量只能设置部分参数,代码设置支持设置全部参数。如果检测到某个参数在两种环境下都有设置,则代码设置高优于环境变量。
说明
依次执行如下命令,或将其保存为脚本执行。
# 替换下文中的bucket name, ak, sk为真实值! cat > setup-pyproton-env.sh << 'EOF' #!/usr/bin/env bash set -exv # 配置 (ak & sk) 或者 assume role的session token 两者二选一 echo "export TOS_ACCESS_KEY_ID='ak'" >> /etc/profile echo "export TOS_SECRET_ACCESS_KEY='sk'" >> /etc/profile echo "export TOS_SESSION_TOKEN='sessiontoken'" >> /etc/profile # 请注意内外网 end point不同 echo "export TOS_ENDPOINT='http://tos-cn-beijing.volces.com'" >> /etc/profile # 用于配置pyproton 的log level,如果想调试程序,可以配置为“DEBUG”模式,建议非生产环境下开启 echo "export PROTONFS_LOGGING_LEVEL='INFO'" >> /etc/profile # 显式覆盖 pyproton 内部Proton SDK的JVM -Xmx参数(默认值为2g) echo "export PYPROTON_JVM_XMX='4g'" >> /etc/profile EOF chmod +x setup-pyproton-env.sh ./setup-pyproton-env.sh source /etc/profile
from pyproton.protonfs import ( ProtonFileSystem, ProtonError, ) from pyproton.protonfs import ProtonFile import string import os fs = ProtonFileSystem( endpoint_url="http://tos-cn-beijing.volces.com", key="", secret="", session_token="", )
from pyproton.protonfs import ( ProtonFileSystem, ProtonError, ) from pyproton.protonfs import ProtonFile import string import os fs = ProtonFileSystem( endpoint_url="http://tos-cn-beijing.volces.com", key="", secret="", session_token="", # 用户可按需调整JVM参数,默认的使用无需指定,使用PyProton内置的参数即可 jvm_args = [ "-Xms64m", "-Xmx2g", "-XX:+UseG1GC", "-XX:+UnlockExperimentalVMOptions", "-XX:MaxGCPauseMillis=100", "-XX:G1NewSizePercent=5", "-XX:InitiatingHeapOccupancyPercent=65", "-XX:+ParallelRefProcEnabled", "-XX:ConcGCThreads=4", "-XX:ParallelGCThreads=16", "-XX:MaxTenuringThreshold=1", "-XX:G1HeapRegionSize=32m", "-XX:G1MixedGCCountTarget=64", "-XX:G1OldCSetRegionThresholdPercent=5", "-Dfile.encoding=UTF-8", "-Duser.timezone=GMT+08", "-XX:+HeapDumpOnOutOfMemoryError", "-XX:HeapDumpPath=./java_heapdump.hprof", ], # proton sdk支持的参数设置 config_dict = { # PROTON_CONFIG_KEY_TOS_ENDPOINT: "http://tos-cn-beijing.volces.com", # PROTON_CONFIG_KEY_TOS_AK: "", # PROTON_CONFIG_KEY_TOS_SK: "", # PROTON_CONFIG_KEY_TOS_SESSION_TOKEN: "", "fs.tos.credentials.provider": "io.proton.common.object.tos.auth.DefaultCredentialsProviderChain", # noqa: E501 "fs.AbstractFileSystem.tos.impl": "io.proton.fs.ProtonFS", "fs.tos.impl": "io.proton.fs.ProtonFileSystem", "fs.tos.credential.provider.custom.classes": "io.proton.common.object.tos.auth.EmrSidecarCredentialProvider,io.proton.common.object.tos.auth.EnvironmentCredentialsProvider,io.proton.common.object.tos.auth.SimpleCredentialsProvider,io.proton.common.object.tos.auth.IAMInstanceCredentialsProvider", # noqa: E501 # "fs.tos.multipart.staging-dir": "", # "fs.tos.multipart.size": "8388608", # "fs.tos.multipart.thread-pool-size": "96", # "fs.tos.multipart.staging-buffer-size": "4096", # "fs.tos.multipart.threshold": "10485760", # "fs.tos.task.thread-pool-size": "96", } )
编程设置参数时,PyProton充分考虑了易用性和灵活性,主要体现在两个参数上:
获得fs
对象后,用户就可以调用如ls
、mkdir
等fsspec兼容的API来操作或访问Proton支持的后端存储:
fs.mkdir("xxxx") fs.ls("xxxx") with fs.open("path", mode="wb") as f: f.write("xxx")
说明
with as
语句以确保文件能被即时关闭。-Xmx
参数。PROTONFS_LOGGING_LEVEL
系统环境变量进行控制 )。目前PyProton支持的API列表如下:
说明
Ray 镜像对PyProton的集成版本信息,请参考Ray镜像说明文档。
PyProton可以以两种方式在Ray引擎上使用:
from pyproton.protonfs import ( ProtonFileSystem, ProtonError, PROTON_CONFIG_KEY_TOS_ENDPOINT, PROTON_CONFIG_KEY_TOS_AK, PROTON_CONFIG_KEY_TOS_SK ) from pyproton.protonfs import ProtonFile from pyarrow import fs from pyarrow._fs import ( # noqa PyFileSystem ) import ray import random import string import os from jpype import JClass proton_fs = ProtonFileSystem( # .... ) pyarrow_fs = PyFileSystem(fs.FSSpecHandler(proton_fs)) dt = ray.data.range(100) dt.show(5) dt.write_csv(path = "tos://bucket/path", filesystem = pyarrow_fs)
注意
如果代码中需调用ray.init(),请将其置于初始化ProtonFileSystem之前,否则会报错。