【问题标题】:How to run SparkR script using spark-submit or sparkR on an EMR cluster?如何在 EMR 集群上使用 spark-submit 或 sparkR 运行 SparkR 脚本?
【发布时间】:2018-04-22 06:09:15
【问题描述】:

我编写了一个 sparkR 代码,想知道是否可以在 EMR 集群上使用 spark-submit 或 sparkR 提交它。

我尝试了几种方法,例如: sparkR mySparkRScript.r 或 sparkR --no-save mySparkScript.r 等。但每次我遇到以下错误:

Error in sparkR.sparkContext(master, appName, sparkHome, sparkConfigMap,  : 
JVM is not ready after 10 seconds

示例代码:

#Set the path for the R libraries you would like to use.
#You may need to modify this if you have custom R libraries.
.libPaths(c(.libPaths(), '/usr/lib/spark/R/lib'))

#Set the SPARK_HOME environment variable to the location on EMR
Sys.setenv(SPARK_HOME = '/usr/lib/spark')

#Load the SparkR library into R
library(SparkR, lib.loc = c(file.path(Sys.getenv("SPARK_HOME"), "R", "lib")))

#Initiate a Spark context and identify where the master node is located.
#local is used here because the RStudio server
#was installed on the master node

sc <- sparkR.session(master = "local[*]", sparkEnvir = list(spark.driver.memory="2g"))

sqlContext <- sparkRSQL.init(sc)

注意:我可以通过直接粘贴或使用 source("mySparkRScript.R") 在 sparkr-shell 中运行我的代码。

参考:

  1. Crunching Statistics at Scale with SparkR on Amazon EMR
  2. SparkR Spark documentation
  3. R on Spark
  4. Executing-existing-r-scripts-from-spark-rutger-de-graaf
  5. Github

【问题讨论】:

    标签: r apache-spark emr sparkr spark-submit


    【解决方案1】:

    我能够通过 Rscript 运行它。您需要做一些事情,这可能是一个过程密集型的。如果你愿意试一试,我会推荐:

    1. 弄清楚如何进行自动化 SparkR 或 sparklyR 构建。来自:https://github.com/UrbanInstitute/spark-social-science
    2. 使用 AWS CLI 首先使用您将通过以下步骤 1 创建的 EMR 模板和引导脚本创建一个集群。(确保将 EMR 模板和 rstudio_sparkr_emrlyr_blah_blah.sh 脚本放入 S3 存储桶)
    3. 将您的 R 代码放入一个文件并将其放入另一个 S3 存储桶中...您提供的示例代码可以正常工作,但我建议您实际执行一些操作,例如从 S3 读取数据,添加重视它,然后将其写回(只是为了在进入您可能已经坐在那里的“繁重”代码之前确认它可以正常工作)
    4. 创建另一个 .sh 文件,将 R 文件从您拥有的 S3 存储桶复制到集群,然后通过 Rscript 执行它。将此 shell 脚本放在与您的 R 代码文件相同的 S3 存储桶中(为简单起见)。该 shell 文件的内容示例可能如下所示:

      #!/bin/bash
      aws s3 cp s3://path/to/the/R/file/from/step3.R theNameOfTheFileToRun.R
      Rscript theNameOfTheFileToRun.R
      
    5. 在 AWS CLI 中,在创建集群时,将 --step 插入到您的集群创建调用中,使用 Amazon 提供的 CUSTOM JAR RUNNER 运行复制并执行 R 代码的 shell 脚本

    6. 确保在 R 代码结束时停止 Spark 会话。

    AWS CLI 命令的示例可能如下所示(在我的示例中,我使用 Amazon 上的 us-east-1 区域,并在集群中的每个工作人员上放置一个 100GB 磁盘...只需放置您的区域在任何你看到“us-east-1”的地方,然后选择你想要的任何大小的磁盘)

    aws emr create-cluster --name "MY COOL SPARKR OR SPARKLYR CLUSTER WITH AN RSCRIPT TO RUN SOME R CODE" --release-label emr-5.8.0 --applications Name=Spark Name=Ganglia Name=Hadoop --instance-groups InstanceGroupType=MASTER,InstanceCount=1,InstanceType=m4.xlarge 'InstanceGroupType=CORE,InstanceCount=2,InstanceType=m4.2xlarge,EbsConfiguration={EbsOptimized=true,EbsBlockDeviceConfigs=[{VolumeSpecification={VolumeType=gp2,SizeInGB=100}},{VolumeSpecification={VolumeType=io1,SizeInGB=100,Iops=100},VolumesPerInstance=1}]}' --log-uri s3://path/to/EMR/sparkr_logs --bootstrap-action Path=s3://path/to/EMR/sparkr_bootstrap/rstudio_sparkr_emr5lyr-proc.sh,Args=['--user','cool_dude','--user-pw','top_secret','--shiny','true','--sparkr','true','sparklyr','true'] --ec2-attributes KeyName=mykeyfilename,InstanceProfile=EMR_EC2_DefaultRole,AdditionalMasterSecurityGroups="sg-abc123",SubnetId="subnet-abc123" --service-role EMR_DefaultRole --scale-down-behavior TERMINATE_AT_TASK_COMPLETION --auto-terminate --region us-east-1 --steps Type=CUSTOM_JAR,Name=CustomJAR,ActionOnFailure=CONTINUE,Jar=s3://us-east-1.elasticmapreduce/libs/script-runner/script-runner.jar,Args=["s3://path/to/the/shell/file/from/step4.sh"]
    

    祝你好运!干杯,内特

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2018-02-24
      • 2020-10-31
      • 2017-05-17
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多