[关闭]
@Arslan6and6 2016-10-10T06:56:23.000000Z 字数 6488 阅读 1141

IDEA 14.1.1 导入 SPARK 1.6.1 源码 并测试

spark


在此输入正文
image_1aofrul5b143sed2dqsb31ee4m.png-37.4kB
image_1aofs2fl4md91m90qmod4c1ucc13.png-86.6kB

image_1aoftugbt1itoaaeisg1opspnu1g.png-111.2kB
image_1aofv485eghpal73vqh981ask2n.png-87.9kB
image_1aofv1hqs5lodriita3idhv71t.png-25.9kB
image_1aofv3bnq15n8uijqpe1gralrl2a.png-337.2kB

  1. package p1.com.ibeifeng.bigdata.senior.spark
  2. import org.apache.spark.{SparkContext, SparkConf}
  3. /**
  4. * Driver Program
  5. */
  6. object SparkApp {
  7. def main (args: Array[String]) {
  8. //step 0:SparkContext
  9. val sparkConf = new SparkConf()
  10. .setAppName("SparkApplicaiton") //APPname
  11. .setMaster("local[2]") //--master local[2] | spark:// | yarn
  12. //Create SparkContext
  13. val sc = new SparkContext(sparkConf)
  14. /**======================================================================================*/
  15. //step 1:input data
  16. val rdd = sc.textFile("hdfs://hadoop-senior.ibeifeng.com:8020/input/page_views.data")
  17. //step 2:porcess data
  18. val sessionCountTop10= rdd
  19. .map(line=>line.split("\t"))
  20. .map(x=>(x(2),1))
  21. .reduceByKey(_+_)
  22. .map(tuple=>(tuple._2,tuple._1))
  23. .sortByKey(false)
  24. .take(10)
  25. //step 3:output data
  26. sessionCountTop10.foreach(println(_))
  27. /**======================================================================================*/
  28. sc.stop()
  29. }
  30. }
  31. 16/07/25 16:03:07 INFO DAGScheduler: Job 1 finished: take at SparkApp.scala:30, took 0.995711 s
  32. (65,32d99014d80e5b683dc87e7c93f7c422)
  33. (65,2FF3QB5Z1ZYMFTQKCM6DUCUDDZXX1NAX)
  34. (59,3697499e2a4bc361c29d8e0652408915)
  35. (59,9364b2b10e1397e4673d77a37eb16b6e)
  36. (55,78e20c225db1e478727d51d7f23f1381)
  37. (51,edf91bcfd29523b0ba65367807ba3585)
  38. (50,784aaaaa4d570e9fb9d448f45eba2617)
  39. (44,74c300ab78b78337fb9a7ef5a12462f5)
  40. (43,53701e01643c9939ce492e0f5a60eb27)
  41. (42,1.2.4)

文件默认读取路径设置为HDFS ,代码可以写为 val rdd = sc.textFile("/input/page_views.data")

  1. cp /opt/modules/hadoop-2.5.0-cdh5.3.6/etc/hadoop/core-site.xml /home/beifeng/IdeaProjects/scala_demo1/p1/src/main/resources
  2. cp /opt/modules/hadoop-2.5.0-cdh5.3.6/etc/hadoop/hdfs-site.xml /home/beifeng/IdeaProjects/scala_demo1/p1/src/main/resources

打包jar包
image_1aogkj94kooaiqa1tro105k1ibcm.png-32.4kB
image_1aogkk4mj128q93e1o7r17idk7r13.png-37.8kB
image_1aogkmpnd1sm51029m181t3q1hop1g.png-29.8kB
image_1aogl4nuc1taj126j1g1719mj1mvk2a.png-125.7kB
image_1aogl9is71l261bku1f5n15opf8n2n.png-39.5kB
image_1aogld93ag6c1p3p16hu1fvcs2534.png-4.7kB

  1. $ pwd
  2. /home/beifeng/IdeaProjects/idea_jars
  3. [beifeng@hadoop-senior idea_jars]$ ll
  4. total 12
  5. -rw-rw-r-- 1 beifeng beifeng 9688 Jul 25 17:16 p1.jar
  6. [beifeng@hadoop-senior idea_jars]$ cp p1.jar /opt/modules/spark-1.6.1-bin-2.5.0-cdh5.3.6/

image_1aoglndln1o9p1o4v10lmavv2vm3h.png-40kB
image_1aogq8c50139c1tac16vb1in31c6s3u.png-40.2kB

  1. [beifeng@hadoop-senior spark-1.6.1-bin-2.5.0-cdh5.3.6]$ bin/spark-submit --help
  2. Usage: spark-submit [options] <app jar | python file> [app arguments]
  3. Usage: spark-submit --kill [submission ID] --master [spark://...]
  4. Usage: spark-submit --status [submission ID] --master [spark://...]
  5. Options:
  6. --master MASTER_URL spark://host:port, mesos://host:port, yarn, or local.
  7. --deploy-mode DEPLOY_MODE Whether to launch the driver program locally ("client") or
  8. on one of the worker machines inside the cluster ("cluster")
  9. (Default: client).
  10. --class CLASS_NAME Your application's main class (for Java / Scala apps).
  11. --name NAME A name of your application.
  12. --jars JARS Comma-separated list of local jars to include on the driver
  13. and executor classpaths.
  14. --packages Comma-separated list of maven coordinates of jars to include
  15. on the driver and executor classpaths. Will search the local
  16. maven repo, then maven central and any additional remote
  17. repositories given by --repositories. The format for the
  18. coordinates should be groupId:artifactId:version.
  19. --exclude-packages Comma-separated list of groupId:artifactId, to exclude while
  20. resolving the dependencies provided in --packages to avoid
  21. dependency conflicts.
  22. --repositories Comma-separated list of additional remote repositories to
  23. search for the maven coordinates given with --packages.
  24. --py-files PY_FILES Comma-separated list of .zip, .egg, or .py files to place
  25. on the PYTHONPATH for Python apps.
  26. --files FILES Comma-separated list of files to be placed in the working
  27. directory of each executor.
  28. --conf PROP=VALUE Arbitrary Spark configuration property.
  29. --properties-file FILE Path to a file from which to load extra properties. If not
  30. specified, this will look for conf/spark-defaults.conf.
  31. --driver-memory MEM Memory for driver (e.g. 1000M, 2G) (Default: 1024M).
  32. --driver-java-options Extra Java options to pass to the driver.
  33. --driver-library-path Extra library path entries to pass to the driver.
  34. --driver-class-path Extra class path entries to pass to the driver. Note that
  35. jars added with --jars are automatically included in the
  36. classpath.
  37. --executor-memory MEM Memory per executor (e.g. 1000M, 2G) (Default: 1G).
  38. --proxy-user NAME User to impersonate when submitting the application.
  39. --help, -h Show this help message and exit
  40. --verbose, -v Print additional debug output
  41. --version, Print the version of current Spark
  42. Spark standalone with cluster deploy mode only:
  43. --driver-cores NUM Cores for driver (Default: 1).
  44. Spark standalone or Mesos with cluster deploy mode only:
  45. --supervise If given, restarts the driver on failure.
  46. --kill SUBMISSION_ID If given, kills the driver specified.
  47. --status SUBMISSION_ID If given, requests the status of the driver specified.
  48. Spark standalone and Mesos only:
  49. --total-executor-cores NUM Total cores for all executors.
  50. Spark standalone and YARN only:
  51. --executor-cores NUM Number of cores per executor. (Default: 1 in YARN mode,
  52. or all available cores on the worker in standalone mode)
  53. YARN-only:
  54. --driver-cores NUM Number of cores used by the driver, only in cluster mode
  55. (Default: 1).
  56. --queue QUEUE_NAME The YARN queue to submit to (Default: "default").
  57. --num-executors NUM Number of executors to launch (Default: 2).
  58. --archives ARCHIVES Comma separated list of archives to be extracted into the
  59. working directory of each executor.
  60. --principal PRINCIPAL Principal to be used to login to KDC, while running on
  61. secure HDFS.
  62. --keytab KEYTAB The full path to the file that contains the keytab for the
  63. principal specified above. This keytab will be copied to
  64. the node running the Application Master via the Secure
  65. Distributed Cache, for renewing the login tickets and the
  66. delegation tokens periodically.

--master 由setMaster("local2")指定
--name 由setAppName("SparkApplicaiton")指定
--class 指定jar包

  1. [beifeng@hadoop-senior spark-1.6.1-bin-2.5.0-cdh5.3.6]$ bin/spark-submit --class p1.com.ibeifeng.bigdata.senior.spark.SparkApp p1.jar
  2. ...
  3. (65,32d99014d80e5b683dc87e7c93f7c422)
  4. (65,2FF3QB5Z1ZYMFTQKCM6DUCUDDZXX1NAX)
  5. (59,3697499e2a4bc361c29d8e0652408915)
  6. (59,9364b2b10e1397e4673d77a37eb16b6e)
  7. (55,78e20c225db1e478727d51d7f23f1381)
  8. (51,edf91bcfd29523b0ba65367807ba3585)
  9. (50,784aaaaa4d570e9fb9d448f45eba2617)
  10. (44,74c300ab78b78337fb9a7ef5a12462f5)
  11. (43,53701e01643c9939ce492e0f5a60eb27)
  12. (42,1.2.4)

在集群上运行: 需要把 .setMaster("local2")注释,删除原jar包,并在idea点击Build=>Bulid Artifacts=>Rebuild,重新生成jar包,改jar包运行位置与提交运行位置一致

  1. [beifeng@hadoop-senior spark-1.6.1-bin-2.5.0-cdh5.3.6]$ bin/spark-submit --class p1.com.ibeifeng.bigdata.senior.spark.SparkApp --master spark://hadoop-senior.ibeifeng.com:7077 --deploy-mode cluster /home/beifeng/IdeaProjects/idea_jars/p1.jar
  2. Running Spark using the REST application submission protocol.
  3. 16/08/08 20:30:53 INFO rest.RestSubmissionClient: Submitting a request to launch an application in spark://hadoop-senior.ibeifeng.com:7077.
  4. 16/08/08 20:31:04 WARN rest.RestSubmissionClient: Unable to connect to server spark://hadoop-senior.ibeifeng.com:7077.
  5. Warning: Master endpoint spark://hadoop-senior.ibeifeng.com:7077 was not a REST server. Falling back to legacy submission gateway instead.
  6. 16/08/08 20:31:04 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

image_1apl27rj511in19iboct39m130f9.png-114.1kB
重载页面后
image_1apl2juou1shd1ks51ihjl493vm.png-79.5kB
可以看到输出结果
image_1apl2l3jmgsk1u421b704oc19ln13.png-29.4kB
以及日志信息,还可以再此查看已经加载的外部jar包
image_1apl2nbkm3mh1bsm1g7k5sk1jld1g.png-29.7kB
image_1apl2ohknjvb7o21t6o14u418021t.png-141.5kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注