百度智能云

All Product Document

          MapReduce

          Cluster

          Create Clusters

          The following code is used to create a cluster, which contains 1 master node and 2 core codes and has Hive, Pig, and HBase applications installed. Notes: When referring to the following sample code, you need to modify the BOS path specified for log_uri parameters to make it suitable for your account.

          instance_groups = [
              BmrClient.instance_group(
                  'Master',
                  'bmr.g1.xlarge',
                  1,
                  'ig-master'
              ),
              BmrClient.instance_group(
                  'Core',
                  'bmr.g1.xlarge',
                  2,
                  'ig-core'
              )
          ]  # construct request body
          request_body = {
              'applications': [
                  {
                      "name": "hive",
                      "version": "0.13.0"
                  },
                  {
                      "name": "pig",
                      "version": "0.11.0"
                  },
                  {
                      "name": "hbase",
                      "version": "0.98.0"
                  }
              ],
              'auto_terminate': False,
              'log_uri': 'bos://tester01/sdk/',
              'name': 'sdk-cluster01',
              'service_ha_enabled': False,
              'safe_mode_enabled': False
          }
          
          try:
              response = bmr_client.create_cluster(
                  'hadoop',
                  '0.1.0',
                  instance_groups,
                  **request_body)
              cluster_id = response.cluster_id
          except BceHttpClientError as e:
              if isinstance(e.last_error, BceServerError):
                  LOG.error('create_cluster failed. Response %s, code: %s, msg: %s'
                            % (e.last_error.status_code, e.last_error.code, e.last_error.message))
              else:
                  LOG.error('create_cluster failed. Unknown exception: %s' % e)
                  
           

          Please refer to https://cloud.baidu.com/doc/BMR/s/6jwvxw85z.

          List All Clusters

          The following code is used to list all clusters of the request caller. The query parameter maxKeys is used to restrict the number of clusters returned under every request:

          try:
              response = bmr_client.list_clusters(max_keys=5)
              LOG.debug('list total %s clusters' % len(response.clusters))
              # output of cluster information
              for cluster in response.clusters:
                  LOG.debug('cluster: %s' % cluster)
          except BceHttpClientError as e:
              if isinstance(e.last_error, BceServerError):
                  LOG.error('list_clusters failed. Response %s, code: %s, msg: %s'
                            % (e.last_error.status_code, e.last_error.code, e.last_error.message))
              else:
                  LOG.error('list_clusters failed. Unknown exception: %s' % e)

          The paging query is used to obtain all clusters, and the configuration of max_keys is used to restrict the number of clusters returned in every page query:

          try:
              page = 1
              next_marker = None
              max_keys = 5
              is_truncated = True
              while is_truncated:
                  response = bmr_client.list_clusters(marker=next_marker, max_keys=max_keys)
                  LOG.debug('list %s clusters on Page %s' % (len(response.clusters), page))
                  page += 1
                  is_truncated = response.is_truncated
                  next_marker = response.next_marker
          except BceHttpClientError as e:
              if isinstance(e.last_error, BceServerError):
                  LOG.error('list_clusters failed. Response %s, code: %s, msg: %s'
                            % (e.last_error.status_code, e.last_error.code, e.last_error.message))
              else:
                  LOG.error('list_clusters failed. Unknown exception: %s' % e)

          Query Specified Clusters

          After obtaining the cluster ID, you can use the following code to query the information on the specified cluster:

          try:
              response = bmr_client.get_cluster(cluster_id)
              # output of information on cluster status
              status = response.status.state
              LOG.debug('check cluster %s: status %s' % (cluster_id, status))
          except BceHttpClientError as e:
              if isinstance(e.last_error, BceServerError):
                  LOG.error('get_cluster failed. Response %s, code: %s, msg: %s'
                            % (e.last_error.status_code, e.last_error.code, e.last_error.message))
              else:
                  LOG.error('describe_cluster failed. Unknown exception: %s' % e)

          Terminate Specified Clusters

          The following code is used to terminate the specified cluster:

          try:
              response = bmr_client.terminate_cluster(cluster_id)
              LOG.debug('terminate cluster %s: status %s' % (cluster_id, response.status))
          except BceHttpClientError as e:
              if isinstance(e.last_error, BceServerError):
                  LOG.error('terminate_cluster failed. Response %s, code: %s, msg: %s'
                            % (e.last_error.status_code, e.last_error.code, e.last_error.message))
              else:
                  LOG.error('terminate_cluster failed. Unknown exception: %s' % e)
          Previous
          InstanceGroup
          Next
          Step