当前位置:   article > 正文

spark学习记录(十一、Spark on Hive配置)_spark 环境配置 hive.server2.proxy.user

spark 环境配置 hive.server2.proxy.user

添加依赖

  1. <dependency>
  2. <groupId>org.apache.spark</groupId>
  3. <artifactId>spark-hive_2.12</artifactId>
  4. <version>2.4.0</version>
  5. <scope>provided</scope>
  6. </dependency>

1.1hadoop1启动hive metastore服务

在hive/bin目录下

hive --service metastore &

1.2修改hive客户端主机上 hive/conf目录下的hive-site.xml 添加

  1. <configuration>
  2. <property>
  3. <name>hive.metastore.uris</name>
  4. <value>thrift://hadoop1:9083</value>
  5. </property>
  6. </configuration>

1.3 将hive客户端的主机上hive/conf目录下的hive-site.xml拷到spark客户端spark/conf目录下 

1.4修改spark客户端的hive-site.xml,只保留

  1. <configuration>
  2. <property>
  3. <name>hive.metastore.uris</name>
  4. <value>thrift://hadoop1:9083</value>
  5. </property>
  6. </configuration>

1.5启动spark

在spark/sbin目录下

./start-master.sh

在spark客户端的spark/bin目录下

./spark-shell --master spark://hadoop1:7077,hadoop2:7077

1.6编写java代码

  1. public class JavaExample {
  2. public static void main(String[] args) {
  3. SparkConf conf = new SparkConf();
  4. conf.setAppName("hive");
  5. JavaSparkContext sc = new JavaSparkContext(conf);
  6. // HiveContext是SQLContext子类
  7. HiveContext hiveContext = new HiveContext(sc);
  8. hiveContext.sql("USE spark");
  9. hiveContext.sql("DROP TABLE IF EXISTS student_infos");
  10. // 在hive中创建student_infos表
  11. hiveContext.sql("CREATE TABLE IF NOT EXISTS student_infos (name STRING,age INT) row format delimited " +
  12. "fields terminated by '\t'");
  13. hiveContext.sql("load data local inpath '/usr/local/student_infos' into table student_infos");
  14. /**
  15. * 查询生成的DataFrame
  16. */
  17. Dataset<Row> siDf = hiveContext.sql("SELECT * from student_infos");
  18. siDf.registerTempTable("students");
  19. siDf.show();
  20. /**
  21. * 将结果保存到hive表good_student_infos
  22. */
  23. hiveContext.sql("DROP TABLE IF EXISTS good_student_infos");
  24. siDf.write().mode(SaveMode.Overwrite).saveAsTable("good_student_infos");
  25. Dataset<Row> table = hiveContext.table("good_student_infos");
  26. Row[] collects = table.collect();
  27. for (Row collect : collects) {
  28. System.out.println(collect);
  29. }
  30. sc.stop();
  31. }
  32. }

1.7在bin目录下执行命令

./spark-submit --master spark://hadoop1:7077,hadoop2:7077 /usr/local/JavaExample.jar 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/987927
推荐阅读
相关标签
  

闽ICP备14008679号