全部产品
云市场

参考示例

更新时间:2018-12-04 19:10:02

下面使用 Python SDK 的方式,创建一个 App,作用是拷贝一个输入路径 inputparam1 的内容到一个输出路径 output,并打印自定义的环境变量,App 相关定义如下:

  • 输入信息
    • inputparam1:自定义输入参数1,OSS 路径,映射到本地的 /home/input/ 目录。
    • inputparam2:自定义输入参数2,非 OSS 路径,这里无具体含义,用于演示输入参数用法。
  • 输出信息
    • outout:自定义输出参数,OSS 路径,映射到本地的 /home/output/ 目录。
  • 环境变量:
    • env1:自定义环境变量,这里无具体含义,用于演示环境变量用法。

使用 Python SDK 创建基于 VM 镜像的 App

  1. #encoding=utf-8
  2. import sys
  3. from batchcompute import Client, ClientError
  4. from batchcompute import CN_BEIJING as REGION
  5. from batchcompute.resources import (
  6. JobDescription, TaskDescription, DAG, AutoCluster, GroupDescription, ClusterDescription, AppDescription
  7. )
  8. ACCESS_KEY_ID='xxxx' # 填写您的 ACCESS_KEY_ID
  9. ACCESS_KEY_SECRET='xxxx' # 填写您的 ACCESS_KEY_SECRET
  10. def main():
  11. try:
  12. client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
  13. app_desc = {
  14. "Name":"DemoApp_vm",
  15. "Daemonize":False,
  16. "VM":{
  17. "ECSImageId":"img-ubuntu-vpc",
  18. },
  19. "CommandLine":"/bin/bash -c 'cp -r ${inputparam1} ${output} && echo ${inputparam2} && echo $env1'",
  20. "InputParameters":{
  21. "inputparam1":{
  22. "Description":"",
  23. "Type":"String",
  24. "Default":"",
  25. "LocalPath":"/home/input/"
  26. },
  27. "inputparam2":{
  28. "Description":"",
  29. "Type":"String",
  30. "Default":"",
  31. "LocalPath":""
  32. }
  33. },
  34. "OutputParameters":{
  35. "output":{
  36. "Description":"",
  37. "Type":"String",
  38. "LocalPath":"/home/output/"
  39. }
  40. },
  41. "Config":{
  42. "ResourceType":{
  43. "Default":"OnDemand",
  44. "Overwritable":True
  45. },
  46. "InstanceType":{
  47. "Description":"",
  48. "Default":"ecs.c5.large",
  49. "Overwritable":True
  50. },
  51. "InstanceCount":{
  52. "Description":"",
  53. "Default":1,
  54. "Overwritable":True
  55. },
  56. "MinDiskSize":{
  57. "Description":"",
  58. "Default":40,
  59. "Overwritable":True
  60. },
  61. "DiskType":{
  62. "Description":"",
  63. "Default":"cloud_efficiency",
  64. "Overwritable":True
  65. },
  66. "MaxRetryCount":{
  67. "Description":"",
  68. "Default":0,
  69. "Overwritable":True
  70. },
  71. "Timeout":{
  72. "Description":"",
  73. "Default":86400,
  74. "Overwritable":True
  75. }
  76. },
  77. "EnvVars":{
  78. "env1":"abcd"
  79. }
  80. }
  81. appName = client.create_app(app_desc).Name
  82. print('App created: %s' % appName)
  83. except ClientError, e:
  84. print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
  85. if __name__ == '__main__':
  86. sys.exit(main())

使用 Python SDK 创建基于 Docker 镜像的 App

  1. #encoding=utf-8
  2. import sys
  3. from batchcompute import Client, ClientError
  4. from batchcompute import CN_BEIJING as REGION
  5. from batchcompute.resources import (
  6. JobDescription, TaskDescription, DAG, AutoCluster, GroupDescription, ClusterDescription, AppDescription
  7. )
  8. ACCESS_KEY_ID='xxxx' # 填写您的 ACCESS_KEY_SECRET
  9. ACCESS_KEY_SECRET='xxxxxx' # 填写您的 ACCESS_KEY_SECRET
  10. def main():
  11. try:
  12. client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
  13. app_desc = {
  14. "Name":"DemoApp_Docker",
  15. "Daemonize":False,
  16. "Docker":{
  17. "Image":"localhost:5000/dockerimagename",
  18. "RegistryOSSPath":"oss://bucket-name/dockers/"
  19. },
  20. "CommandLine":"/bin/bash -c 'cp -r ${inputparam1} ${output} && echo ${inputparam2} && echo $env1'",
  21. "InputParameters":{
  22. "inputparam1":{
  23. "Description":"",
  24. "Type":"String",
  25. "Default":"",
  26. "LocalPath":"/home/input/"
  27. },
  28. "inputparam2":{
  29. "Description":"",
  30. "Type":"String",
  31. "Default":"",
  32. "LocalPath":""
  33. }
  34. },
  35. "OutputParameters":{
  36. "output":{
  37. "Description":"",
  38. "Type":"String",
  39. "LocalPath":"/home/output/"
  40. }
  41. },
  42. "Config":{
  43. "ResourceType":{
  44. "Default":"OnDemand",
  45. "Overwritable":True
  46. },
  47. "InstanceType":{
  48. "Description":"",
  49. "Default":"ecs.c5.large",
  50. "Overwritable":True
  51. },
  52. "InstanceCount":{
  53. "Description":"",
  54. "Default":1,
  55. "Overwritable":True
  56. },
  57. "MinDiskSize":{
  58. "Description":"",
  59. "Default":40,
  60. "Overwritable":True
  61. },
  62. "DiskType":{
  63. "Description":"",
  64. "Default":"cloud_efficiency",
  65. "Overwritable":True
  66. },
  67. "MaxRetryCount":{
  68. "Description":"",
  69. "Default":0,
  70. "Overwritable":True
  71. },
  72. "Timeout":{
  73. "Description":"",
  74. "Default":86400,
  75. "Overwritable":True
  76. }
  77. },
  78. "EnvVars":{
  79. "env1":"abcd"
  80. }
  81. }
  82. appName = client.create_app(app_desc).Name
  83. print('App created: %s' % appName)
  84. except ClientError, e:
  85. print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
  86. if __name__ == '__main__':
  87. sys.exit(main())

使用 Python SDK 提交 App 作业

  1. #encoding=utf-8
  2. import sys
  3. from batchcompute import Client, ClientError
  4. from batchcompute import CN_BEIJING as REGION
  5. from batchcompute.resources import (
  6. JobDescription, TaskDescription, DAG, AutoCluster, GroupDescription, ClusterDescription, AppDescription, AppJobDescription
  7. )
  8. ACCESS_KEY_ID='xxxx' # 填写您的 ACCESS_KEY_ID
  9. ACCESS_KEY_SECRET='xxxx' # 填写您的 ACCESS_KEY_SECRET
  10. LOG_PATH = 'oss://bucket-name/bc_test/log/'
  11. ERR_PATH = 'oss://bucket-name/bc_test/error/'
  12. def main():
  13. try:
  14. client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
  15. job_desc = AppJobDescription()
  16. # Create job description.
  17. job_desc.Name = "App_demo_test_job_docker"
  18. job_desc.Description = "Test of create app job."
  19. job_desc.Type = 'App'
  20. job_desc.App.AppName = 'DemoApp_Docker'
  21. job_desc.App.Config.InstanceType = "ecs.sn2.medium"
  22. job_desc.App.Inputs["inputparam1"] = "oss://bucket-name/bc_test/input/";
  23. job_desc.App.Inputs["inputparam2"] = "abcd1234";
  24. job_desc.App.Outputs["output"] = "oss://bucket-name/bc_test/output/"
  25. job_desc.App.Logging.StdoutPath = LOG_PATH
  26. job_desc.App.Logging.StderrPath = ERR_PATH
  27. job_id = client.create_job(job_desc).Id
  28. print('App job created: %s' % job_id)
  29. except ClientError, e:
  30. print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
  31. if __name__ == '__main__':
  32. sys.exit(main())