打开IDEA 在src下的main下的scala下右击创建一个scala类 名字为SimpleApp ,内容如下
import org.apache.spark.SparkContext import org.apache.spark.SparkContext._ import org.apache.spark.SparkConf object SimpleApp { def main(args: Array[String]) { val logFile = "/home/spark/opt/spark-1.2.0-bin-hadoop2.4/README.md" // Should be some file on your system val conf = new SparkConf().setAppName("Simple Application") val sc = new SparkContext(conf) val logData = sc.textFile(logFile, 2).cache() val numAs = logData.filter(line => line.contains("a")).count() val numBs = logData.filter(line => line.contains("b")).count() println("Lines with a: %s, Lines with b: %s".format(numAs, numBs)) } }
打包文件:
File-->>ProjectStructure -->点击Artificats-->>点击绿色加号 --> 点击JAR-->>选择 From module with dependices
点击Output Layout 看看是否没有第三方jar包,因为你使用spark集群环境,所以你不需要第三方jar包
重新build:
Build-->>Build Artifcat ..--->>build 或者rebuild
执行后 就会在D:\mygit\study-scala\out\artifacts\study_scala_jar目录下看到study-scala.jar
上传到spark集群服务器的 spark_home下的myApp下
上传/home/spark/opt/spark-1.2.0-bin-hadoop2.4/README.md到HDFS中
提交spark任务:
./bin/spark-submit --class "SimpleApp" --master local[4] myApp/study-scala.jar
执行结果为a:60,b:29
时间: 2024-10-22 06:36:17