Spark整合Hive

spark-sql 寫代碼方式
1、idea裏面將代碼編寫好打包上傳到集群中運行,上線使用
spark-submit提交

2、spark shell  (repl) 裏面使用sqlContext     測試使用,簡單任務使用
    spark-shell --master yarn-client
    不能使用yarn-cluster 		

3、spark-sql    
spark-sql --master yarn-client   不能使用yarn-cluster 
    可以整合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、 將hive中的mysql 驅動包複製到saprk jars目錄下
         cp mysql-connector-java-5.1.49.jar /usr/local/soft/spark-2.4.5/jars/

         整合好之後在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 ‘/spark/data/stu/input/’;

create table score
(
student_id string,
cource_id string,
sco int
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘,’
STORED AS textfile
location ‘/data/score/’;

Tags: