This topic provides the sample code of Python 2.

Note If you use Python 3, you can use the sample code in Common API instructions.

View clusters

#!/usr/bin/python
from aliyunsdkcore import client
from aliyunsdkemr.request.v20160408 import ListClustersRequest
clt = client.AcsClient('SFAW************','Nc2nZ6dQoiqck0*************','cn-hangzhou') # set acessId and accessKey
request = ListClustersRequest.ListClustersRequest()
request.set_accept_format('xml') # xml or json
# Specify filter conditions to obtain the clusters that are in the RUNNING state and the clusters that are in the IDLE state. The Status parameter is optional. 
request.add_query_param('StatusList.1', 'RUNNING')
request.add_query_param('StatusList.2', 'IDLE')
result = clt.do_action(request)
print result

Create a cluster

#!/usr/bin/python
from aliyunsdkcore import client
from aliyunsdkemr.request.v20160408 import CreateClusterRequest
clt = client.AcsClient('SFAW************','Nc2nZ6dQoiqck0*************','cn-hangzhou') # set acessId and accessKey
request = CreateClusterRequest.CreateClusterRequest()
request.set_Name("pydemo")
request.set_ZoneId("cn-hangzhou-b")
request.set_LogEnable(False)
request.set_SecurityGroupId("sg-********")
request.set_IsOpenPublicIp(True)
request.set_ChargeType("PostPaid")
request.set_EmrVer("EMR-1.3.0")
request.set_ClusterType("HADOOP")
request.set_IoOptimized(True)
request.set_InstanceGeneration("ecs-2")
# set EcsOrder
request.add_query_param('EcsOrder.1.NodeCount', '1')
request.add_query_param('EcsOrder.1.NodeType', 'MASTER')
request.add_query_param('EcsOrder.1.InstanceType', 'ecs.n1.large')
request.add_query_param('EcsOrder.1.DiskType', 'CLOUD_EFFICIENCY')
request.add_query_param('EcsOrder.1.DiskCapacity', '80')
request.add_query_param('EcsOrder.1.DiskCount', '1')
request.add_query_param('EcsOrder.1.Index', '1')
request.add_query_param('EcsOrder.2.NodeCount', '3')
request.add_query_param('EcsOrder.2.NodeType', 'CORE')
request.add_query_param('EcsOrder.2.InstanceType', 'ecs.n1.large')
request.add_query_param('EcsOrder.2.DiskType', 'CLOUD_EFFICIENCY')
request.add_query_param('EcsOrder.2.DiskCapacity', '80')
request.add_query_param('EcsOrder.2.DiskCount', '4')
request.add_query_param('EcsOrder.2.Index', '2')
request.set_accept_format('json')
result = clt.do_action(request)
print result
Notice Specific SDKs of Alibaba Cloud services are inconvenient to use because the SDKs of all Alibaba Cloud services are automatically generated. In most scenarios, SDK for Python does not support input parameters of the LIST type. If you use input parameters of the LIST type, special data processing is required. If you set an input parameter to a list that contains values of basic data types, view the configurations of the StatusList parameter in the sample code of the View clusters section. If you set an input parameter to a list that contains complex objects, view the configurations of the EcsOrder parameter in the sample code of the Create a cluster section. In other scenarios, the methods to process input parameters of the LIST type, such as the BootstrapAction parameter, are similar to the preceding methods. We recommend that you use SDK for Java because SDK for Java does not have the preceding issue and is easier to use.

The methods for using other API operations are similar. For more information, see List of operations by function.