参考示例
下面使用 Python SDK 的方式,创建一个 App,作用是拷贝一个输入路径 inputparam1 的内容到一个输出路径 output,并打印自定义的环境变量,App 相关定义如下:
- 输入信息
- inputparam1:自定义输入参数1,OSS 路径,映射到本地的 /home/input/ 目录。
- inputparam2:自定义输入参数2,非 OSS 路径,这里无具体含义,用于演示输入参数用法。
- 输出信息
- outout:自定义输出参数,OSS 路径,映射到本地的 /home/output/ 目录。
- 环境变量:
- env1:自定义环境变量,这里无具体含义,用于演示环境变量用法。
使用 Python SDK 创建基于 VM 镜像的 App
#encoding=utf-8
import sys
from batchcompute import Client, ClientError
from batchcompute import CN_BEIJING as REGION
from batchcompute.resources import (
JobDescription, TaskDescription, DAG, AutoCluster, GroupDescription, ClusterDescription, AppDescription
)
ACCESS_KEY_ID='xxxx' # 填写您的 ACCESS_KEY_ID
ACCESS_KEY_SECRET='xxxx' # 填写您的 ACCESS_KEY_SECRET
def main():
try:
client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
app_desc = {
"Name":"DemoApp_vm",
"Daemonize":False,
"VM":{
"ECSImageId":"img-ubuntu-vpc",
},
"CommandLine":"/bin/bash -c 'cp -r ${inputparam1} ${output} && echo ${inputparam2} && echo $env1'",
"InputParameters":{
"inputparam1":{
"Description":"",
"Type":"String",
"Default":"",
"LocalPath":"/home/input/"
},
"inputparam2":{
"Description":"",
"Type":"String",
"Default":"",
"LocalPath":""
}
},
"OutputParameters":{
"output":{
"Description":"",
"Type":"String",
"LocalPath":"/home/output/"
}
},
"Config":{
"ResourceType":{
"Default":"OnDemand",
"Overwritable":True
},
"InstanceType":{
"Description":"",
"Default":"ecs.c5.large",
"Overwritable":True
},
"InstanceCount":{
"Description":"",
"Default":1,
"Overwritable":True
},
"MinDiskSize":{
"Description":"",
"Default":40,
"Overwritable":True
},
"DiskType":{
"Description":"",
"Default":"cloud_efficiency",
"Overwritable":True
},
"MaxRetryCount":{
"Description":"",
"Default":0,
"Overwritable":True
},
"Timeout":{
"Description":"",
"Default":86400,
"Overwritable":True
}
},
"EnvVars":{
"env1":"abcd"
}
}
appName = client.create_app(app_desc).Name
print('App created: %s' % appName)
except ClientError, e:
print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
if __name__ == '__main__':
sys.exit(main())
使用 Python SDK 创建基于 Docker 镜像的 App
#encoding=utf-8
import sys
from batchcompute import Client, ClientError
from batchcompute import CN_BEIJING as REGION
from batchcompute.resources import (
JobDescription, TaskDescription, DAG, AutoCluster, GroupDescription, ClusterDescription, AppDescription
)
ACCESS_KEY_ID='xxxx' # 填写您的 ACCESS_KEY_SECRET
ACCESS_KEY_SECRET='xxxxxx' # 填写您的 ACCESS_KEY_SECRET
def main():
try:
client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
app_desc = {
"Name":"DemoApp_Docker",
"Daemonize":False,
"Docker":{
"Image":"localhost:5000/dockerimagename",
"RegistryOSSPath":"oss://bucket-name/dockers/"
},
"CommandLine":"/bin/bash -c 'cp -r ${inputparam1} ${output} && echo ${inputparam2} && echo $env1'",
"InputParameters":{
"inputparam1":{
"Description":"",
"Type":"String",
"Default":"",
"LocalPath":"/home/input/"
},
"inputparam2":{
"Description":"",
"Type":"String",
"Default":"",
"LocalPath":""
}
},
"OutputParameters":{
"output":{
"Description":"",
"Type":"String",
"LocalPath":"/home/output/"
}
},
"Config":{
"ResourceType":{
"Default":"OnDemand",
"Overwritable":True
},
"InstanceType":{
"Description":"",
"Default":"ecs.c5.large",
"Overwritable":True
},
"InstanceCount":{
"Description":"",
"Default":1,
"Overwritable":True
},
"MinDiskSize":{
"Description":"",
"Default":40,
"Overwritable":True
},
"DiskType":{
"Description":"",
"Default":"cloud_efficiency",
"Overwritable":True
},
"MaxRetryCount":{
"Description":"",
"Default":0,
"Overwritable":True
},
"Timeout":{
"Description":"",
"Default":86400,
"Overwritable":True
}
},
"EnvVars":{
"env1":"abcd"
}
}
appName = client.create_app(app_desc).Name
print('App created: %s' % appName)
except ClientError, e:
print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
if __name__ == '__main__':
sys.exit(main())
使用 Python SDK 提交 App 作业
#encoding=utf-8
import sys
from batchcompute import Client, ClientError
from batchcompute import CN_BEIJING as REGION
from batchcompute.resources import (
JobDescription, TaskDescription, DAG, AutoCluster, GroupDescription, ClusterDescription, AppDescription, AppJobDescription
)
ACCESS_KEY_ID='xxxx' # 填写您的 ACCESS_KEY_ID
ACCESS_KEY_SECRET='xxxx' # 填写您的 ACCESS_KEY_SECRET
LOG_PATH = 'oss://bucket-name/bc_test/log/'
ERR_PATH = 'oss://bucket-name/bc_test/error/'
def main():
try:
client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
job_desc = AppJobDescription()
# Create job description.
job_desc.Name = "App_demo_test_job_docker"
job_desc.Description = "Test of create app job."
job_desc.Type = 'App'
job_desc.App.AppName = 'DemoApp_Docker'
job_desc.App.Config.InstanceType = "ecs.sn2.medium"
job_desc.App.Inputs["inputparam1"] = "oss://bucket-name/bc_test/input/";
job_desc.App.Inputs["inputparam2"] = "abcd1234";
job_desc.App.Outputs["output"] = "oss://bucket-name/bc_test/output/"
job_desc.App.Logging.StdoutPath = LOG_PATH
job_desc.App.Logging.StderrPath = ERR_PATH
job_id = client.create_job(job_desc).Id
print('App job created: %s' % job_id)
except ClientError, e:
print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
if __name__ == '__main__':
sys.exit(main())