Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Developing an MRS Spark Python Job

Updated on 2022-08-17 GMT+08:00

This section describes how to develop an MRS Spark Python on DataArts Factory.

Case 1: Using an MRS Spark Python Job to Count the Number of Words

Prerequisites

You have the permission to access OBS paths.

Data preparation

  • Prepare the script file wordcount.py with the following content:
    # -*- coding: utf-8 -*
    import sys
    from pyspark import SparkConf, SparkContext
    def show(x):
        print(x)
    if __name__ == "__main__":
        if len(sys.argv) < 2:
            print ("Usage: wordcount <inputPath> <outputPath>")
            exit(-1)
        # Create SparkConf.
        conf = SparkConf().setAppName("wordcount")
        # Create SparkContext. Pass the conf=conf parameter.
        sc = SparkContext(conf=conf)
        inputPath = sys.argv[1]
        outputPath = sys.argv[2]
        lines = sc.textFile(name = inputPath)
        # Split each line of data by space to obtain words.
        words = lines.flatMap(lambda line:line.split(" "),True)
        # Pair each word into a tuple count 1.
        pairWords = words.map(lambda word:(word,1),True)
        # Use three partitions (reduceByKey) for summarization.
        result = pairWords.reduceByKey(lambda v1,v2:v1+v2)
        # Print the result.
        result.foreach(lambda t :show(t))
        # Save the result to a file.
        result.saveAsTextFile(outputPath)
        # Stop SparkContext.
        sc.stop()
    NOTE:

    The encoding format must be set to UTF-8. Otherwise, an error will occur during script execution.

  • Prepare the data file in.txt, which contains some English words.

Procedure

  1. Upload the script and data file to the OBS bucket.

    Figure 1 Uploading files to an OBS bucket
    NOTE:

    In this example, upload wordcount.py and in.txt to obs://obs-tongji/python/.

  2. Create an empty job named job_MRS_Spark_Python.

    Figure 2 Creating a job

  3. Go to the job development page, drag the MRS Spark Python node to the canvas, and click the node to configure its properties.

    Figure 3 Configuring properties for an MRS Spark Python node

    Parameter descriptions:

    --master
    yarn
    --deploy-mode
    cluster
    obs://obs-tongji/python/wordcount.py
    obs://obs-tongji/python/in.txt
    obs://obs-tongji/python/out

    Specifically:

    obs://obs-tongji/python/wordcount.py is the directory where the script is stored.

    obs://obs-tongji/python/in.txt is the directory where the wordcount.py parameters are passed. You can pass the words to count.

    obs://obs-tongji/python/out is the directory where output parameters are stored. This directory will also be created in the OBS bucket automatically. If the out directory already exists in the OBS bucket, an error will occur.

  4. Click Test to execute the script job.
  5. After the test is complete, click Submit.
  6. Choose Monitor Job in the navigation pane and view the job execution result.

    Figure 4 Viewing the job execution result

    The job log shows that the job was successfully executed.

    Figure 5 Job run logs
    Figure 6 Job execution status

  7. View the returned records in the OBS bucket. (Skip this step if the return function is not configured.)

    Figure 7 Viewing the returned records in the OBS bucket

Case 2: Using an MRS Spark Python Job to Print hello python

Prerequisites

You have the permission to access OBS paths.

Data preparation

Prepare the script file zt_test_sparkPython1.py with the following content:

from pyspark import SparkContext, SparkConf
conf = SparkConf().setAppName("master"). setMaster("yarn")
sc = SparkContext(conf=conf)
print("hello python")
sc.stop()

Procedure

  1. Upload the script file to an OBS bucket.
  2. Create an empty job.
  3. Go to the job development page, drag the MRS Spark Python node to the canvas, and click the node to configure its properties.

    Parameter descriptions:

    --master
    yarn
    --deploy-mode
    cluster
    obs://obs-tongji/python/zt_test_sparkPython1.py

    zt_test_sparkPython1.py indicates the directory where the script is stored.

  4. Click Test to execute the script job.
  5. After the test is complete, click Submit.
  6. Choose Monitor Job in the navigation pane and view the job execution result.

    Figure 8 Viewing the job execution result

  7. Verify the log.

    Log in to MRS Manager and check that the log on YARN contains hello python.

    Figure 9 Viewing logs on YARN

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback