创建输入输出路径,准备数据
执行提交命令(其他的参数可以不指定,内存,核数,数量)
2、spark-sql写代码的方式(客户端)spark-submit --class com.shujia.demo2spark_sql.Demo06clazz --master
yarn-client jar包
(1)idea里面将代码编写好打包上传到集群中运行,上线使用
spark-submit提交
(2)spark shell (repl) 里面使用sqlContext 测试使用,简单任务使用(写代码)
spark-shell --master yarn-client
(不能使用yarn-cluster)
资源会一直占用,知道退出
代码不能换行,需要写在一行,Dataframe也需要导入执行
import org.apache.spark.sql.{SaveMode, SparkSession}
val stuDF = spark.read.format("csv").schema("id String,name String,age String,gender String,clazz String").load("/data/students.txt")
stuDF.groupBy($"clazz").count().show() //也可以直接写在HDFS文件上
(3)spark-sql (写sql)
spark-sql --master yarn-client 不能使用yarn-cluster
可以创建表等使用sql语法,数据来源于HDFS,但是退出后产生一个meta和derby文件(derby是一个文件数据库),换个目录进去数据就会消失
create table student ( id string, name string, age int, gender string, clazz string ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS textfile location '/data/students.txt';3、spark-sql 整合hive 使用hive的元数据
(1)在hive的hive-site.xml修改一行配置,增加了这一行配置之后,以后在使用hive之前都需要先启动元数据服务
hive.metastore.uris thrift://master:9083
(2)将hive-site.xml 复制到spark conf目录下
cp hive-site.xml /usr/local/soft/spark-2.4.5/conf/
(3)启动hive元数据服务
hive --service metastore 或 nohup hive --service metastore >> metastore.log 2>&1 & //后台启动
(4)将mysql 驱动包复制到saprk jars目录下
cp mysql-connector-java-5.1.49.jar /usr/local/soft/spark-2.4.5/jars/
4、启动整合好之后在spark-sql 里面就可以使用hive的表了
spark-sql --master yarn-client --conf spark.sql.shuffle.partitions=2 (参数设置并行度)
不能使用cluster模式
在spark-sql中设置运行参数 set spark.sql.shuffle.partitions=2;
create table student ( id string, name string, age int, gender string, clazz string ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS textfile location '/data/students.txt'; create table score ( student_id string, cource_id string, sco int ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS textfile location '/data/score/'; 1、统计每个科目排名前十的学生 分组取topN



