spark sql 综合实例

本文是一篇关于Spark SQL的综合实例,通过将Oracle数据库的例子转化为Spark SQL操作,讲解了如何处理四张表(Teacher, Student, Score, Course)的数据,包括创建schema、注册表和时间处理等步骤。" 116492143,10457398,"Redis数据类型详解:String, List, Hash, Set与Sorted Set

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该文主要展示的是spark sql 例子

(内容是找了份oracle的例子,翻译成spark sql的)

1、需要准备好四张表,既四个文本文件逗号分隔

2、为这四张表创建好schema,并注册成表

3、时间处理有小部分改动

准备的四张表

 

image.png

 

image.png

 

image.png

 

image.png

 

四张表中的数据

Teacher

884,zhagnxueyou,female,1980/2/10,Assistant professor,department of computer
856,liudehua,female,1980/12/10,professor,department of electronic engineering
825,liming,female,1980/1/10,advandced professor,department of computer
831,zhourunfa,female,1981/12/10,jonior professor,department of electronic engineering

Student

108,ceng hua,male,1977/9/1,95033
105,zhangsan,male,1975/10/22,95031
107,wangli,female,1976/1/8,95033
101,lili,male,1978/5/14,95033
109,wangfang,female,1979/5/10,95031
103,lijun,male,1974/5/21,95031

Score

103,3-245,86
105,3-245,75
109,3-245,68
103,3-105,92
105,3-105,88
109,3-105,76
101,3-105,64
107,3-105,91
108,3-105,78
101,6-166,85
107,6-166,79
108,6-166,81

Course

3-105,introduction to computer,825
3-245,the operating system,804
6-166,the digital circuit,856
9-888,higher math,831

maven

 <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.11</artifactId>
      <version>2.3.1</version>
    </dependency>
       <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.11</artifactId>
      <version>2.3.1</version>
    </dependency>

例子代码,粘贴可用,注意注释掉不需要的地方即可


 
import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.sql.types._
 
import scala.collection.mutable
import java.text.SimpleDateFormat
 
/**
  * 2017/05/26
  * CDP
  */
 
object SparkSqlExample1 {
 
   def main(args: Array[String]): Unit = {
 
    /** ***************************************************************************************************************
      * sparksession
      */
    val spark = SparkSession
      .builder()
      .master("local")
      .appName("test")
      .config("spark.sql.shuffle.partitions", "5")
      .getOrCreate()
 
    /** ***************************************************************************************************************
      * 表结构
      */
    val StudentSchema: StructType = StructType(mutable.ArraySeq(  //学生表
      StructField("Sno", StringType, nullable = false),           /
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值