当前位置: 首页 > article >正文

Spark---Spark on Hive

1、Spark On Hive的配置

1)、在Spark客户端配置Hive On Spark

在Spark客户端安装包下spark-2.3.1/conf中创建文件hive-site.xml:

配置hive的metastore路径

<configuration>
   <property>
        <name>hive.metastore.uris</name>
        <value>thrift://mynode1:9083</value>
   </property>
</configuration>

2)、启动Hive的metastore服务

hive --service metastore

3)、启动zookeeper集群,启动HDFS集群

4)、启动SparkShell读取Hive中的表总数,对比hive中查询同一表查询总数测试时间

./spark-shell 
--master spark://node1:7077,node2:7077 
 --executor-cores 1 
--executor-memory 1g 
--total-executor-cores 1
import org.apache.spark.sql.hive.HiveContext
val hc = new HiveContext(sc)
hc.sql("show databases").show
hc.sql("user default").show
hc.sql("select count(*) from jizhan").show
  • 注意:

如果使用Spark on Hive  查询数据时,出现错误:

找不到HDFS集群路径,要在客户端机器conf/spark-env.sh中设置HDFS的路径:

2、读取Hive中的数据加载成DataFrame

  • 在Spark1.6版本中HiveContext是SQLContext的子类,连接Hive使用HiveContext。

在Spark2.0+版本中之后,建议使用SparkSession对象,读取Hive中的数据需要开启Hive支持。

  • 由于本地没有Hive环境,要提交到集群运行,提交命令:
./spark-submit 
--master spark://node1:7077,node2:7077 
--executor-cores 1 
--executor-memory 2G 
--total-executor-cores 1
--class com.lw.sparksql.dataframe.CreateDFFromHive 
/root/test/HiveTest.jar

java:

SparkConf conf = new SparkConf();
conf.setAppName("hive");
JavaSparkContext sc = new JavaSparkContext(conf);
//HiveContext是SQLContext的子类。
HiveContext hiveContext = new HiveContext(sc);
hiveContext.sql("USE spark");
hiveContext.sql("DROP TABLE IF EXISTS student_infos");
//在hive中创建student_infos表
hiveContext.sql("CREATE TABLE IF NOT EXISTS student_infos (name STRING,age INT) row format delimited fields terminated by '\t' ");
hiveContext.sql("load data local inpath '/root/test/student_infos' into table student_infos");

hiveContext.sql("DROP TABLE IF EXISTS student_scores"); 
hiveContext.sql("CREATE TABLE IF NOT EXISTS student_scores (name STRING, score INT) row format delimited fields terminated by '\t'");  
hiveContext.sql("LOAD DATA "
+ "LOCAL INPATH '/root/test/student_scores'"
+ "INTO TABLE student_scores");
/**
 * 查询表生成DataFrame
 */
DataFrame goodStudentsDF = hiveContext.sql("SELECT si.name, si.age, ss.score "
+ "FROM student_infos si "
+ "JOIN student_scores ss "
+ "ON si.name=ss.name "
+ "WHERE ss.score>=80");

hiveContext.sql("DROP TABLE IF EXISTS good_student_infos");

goodStudentsDF.registerTempTable("goodstudent");
DataFrame result = hiveContext.sql("select * from goodstudent");
result.show();

/**
 * 将结果保存到hive表 good_student_infos
 */
goodStudentsDF.write().mode(SaveMode.Overwrite).saveAsTable("good_student_infos");

Row[] goodStudentRows = hiveContext.table("good_student_infos").collect();  
for(Row goodStudentRow : goodStudentRows) {
	System.out.println(goodStudentRow);  
}
sc.stop();

scala:

1.val spark = SparkSession.builder().appName("CreateDataFrameFromHive").enableHiveSupport().getOrCreate()
2.spark.sql("use spark")
3.spark.sql("drop table if exists student_infos")
4.spark.sql("create table if not exists student_infos (name string,age int) row format delimited fields terminated by '\t'")
5.spark.sql("load data local inpath '/root/test/student_infos' into table student_infos")
6.
7.spark.sql("drop table if exists student_scores")
8.spark.sql("create table if not exists student_scores (name string,score int) row format delimited fields terminated by '\t'")
9.spark.sql("load data local inpath '/root/test/student_scores' into table student_scores")
10.// val frame: DataFrame = spark.table("student_infos")
11.// frame.show(100)
12.
13.val df = spark.sql("select si.name,si.age,ss.score from student_infos si,student_scores ss where si.name = ss.name")
14.df.show(100)
15.spark.sql("drop table if exists good_student_infos")
16./**
17.* 将结果写入到hive表中
18.*/
19.df.write.mode(SaveMode.Overwrite).saveAsTable("good_student_infos")


http://www.kler.cn/news/162135.html

相关文章:

  • 利用proteus实现串口助手和arduino Mega 2560的串口通信
  • Linux 常用命令汇总
  • Java网络编程 *TCP与UDP协议*
  • 使用Caliper对Fabric地basic链码进行性能测试
  • 【私藏】国内最全的电商API数据接口分享各种业务场景调用API代理的API接口教程
  • 查看Linux的Ubuntu的版本
  • pytorch 模型量化quantization
  • JAVA后端自学技能实操合集
  • Qt之基于QCustomPlot绘制直方图(Histogram),叠加正态分布曲线
  • vmware安装centos7总结
  • VSCODE 运行C程序缓慢解决方法之一
  • Ubuntu22.04安装Mariadb
  • C语言printf的输出格式大全及颜色字体打印
  • 微信小程序中block和View组件的使用区别
  • AI发展下服务器的选择非常重要
  • mysql 链接超时的几个参数详解
  • 嵌入式总线技术学习(文章链接汇总)
  • C语言——指针(五)
  • 【C/PTA —— 15.结构体2(课内实践)】
  • 3D材质编辑:制作被火烧的木头
  • ERP数据仓库模型
  • 数学建模-二氧化碳排放及时空分布测度
  • Ubuntu安装nvidia GPU显卡驱动教程
  • SAP数据一键拉取!利用零代码ETL工具快速实现数据同步
  • 【C/PTA —— 15.结构体2(课外实践)】
  • CPU密集型和IO密集型与CPU内核之间的关系
  • Fabric 画布缩放、拖动、初始化大小
  • 人工智能_机器学习059_非线性核函数_poly核函数_rbf核函数_以及linear核函数效果对比---人工智能工作笔记0099
  • Docker快速入门(编译源码辅助技)
  • AWS攻略——使用中转网关(Transit Gateway)连接同区域(Region)VPC