记事本内容:

打印结构:

方法1:
object SparkSessionTest { case class Person(name:String,age:Int) def main(args: Array[String]): Unit = { val sparkSession=SparkSession.builder().appName("SparkSessionTest")
.master("local[*]")
.getOrCreate()
val sparkContext=sparkSession.sparkContext
val rdd=sparkContext.textFile("D:\\temp\\person.txt")
val rowRdd=rdd.map(_.split(" ")).map(row=>Person(row(0),row(1).toInt)) import sparkSession.implicits._
rowRdd.toDF sparkSession.stop()
} }
方法2:
val sparkContext=sparkSession.sparkContext
val rdd=sparkContext.textFile("D:\\temp\\person.txt")
val schemaFiled="name,age"
val schemaString=schemaFiled.split(",")
val schema =StructType(
List(
StructField(schemaString(0),StringType,nullable = true),
StructField(schemaString(1),IntegerType,nullable = true)
)
)
val rowRdd= rdd.map(_.split("")).map(p=>Row(p(0),p(1).toInt))
val df=sparkSession.createDataFrame(rowRdd,schema)
df.show()

  结果展示:

parquet的优势

支持列存储+嵌套数据格式+适配多个计算框架

节省表扫描时间和反序列的时间

压缩技术稳定出色,节省存储空间

Spark操作 Parquet文件比操作CSV等普通文件的速度更快

加载数据:sparkSession.read.parquet(“/nginx/20200110.parquet”)

写入数据:df.write.mode(SaveMode.Overwrite).parquet(“/path/to”)

分区文件

加载批量数据:

Df.show()//只显示前20条数据
Df.show(3)//只显示前3条数据
df.show( false)//每列可以显示多于20个字符
dt show(3, false)

 

Df.select (“字段1”,”字段2”).show()
Df.select(col(“”) as(“别名1”),col(“字段2”)+1).show()

  

df.first()//获取第一行数据,返回RoW
df.head( 3)//获取前3行数据,返回 Array Row]
df.take (3)//获取前3行数据,返回 Array[Row]
df.takeaslist(3//获取前3行数据,返回List[Row]
df.limit(3).show()//返回新的 Data Frame,不是 Action操作

  

Df.where(“age>21”).show()
Df.filter(“age>21”).show()
Df.where(col(“age”)>21).show()
Ds.where($”age”>21).show()

  

Df.where(“age=21”).show
Df.where(col(“age”)===21).show
Df.where(col(“age”)=!=21).show

  

Val  ageFilter_1 =col(“age”)>21
Val agefilter_2=col(“age”)<25
Val ageFilter_3=agefilter_1.or(ageFilter_2)
Df.where(col(“name”)===”jack”).where(ageFilter_3)

  

Val ageFilter_1 =col(“age”)>21
Val ageFilter_2=col(“age”)<25
Val ageFilter_3 =ageFilter_1.ll(ageFilter_2)
Df.where(col(“name”)===”jack”)
.where(ageFitler_3)
.show
//按照身份统计人数
Df.groupBy(col(“province”))
.count
.show

按照城市,手机运营商分组统计人数并按人数排序

//方法1
Df.groupby(col(“city”),col(“”op_phone“”))
.count
.withColumnRenamed(“count”,”num”)
.orderBy(col(“num”).desc)
.show
//方法2
Ds.groupBy($”city”,$”op_phone”)
.count
.withColumnRenamed(“count”,”num”)
.sort($”num”.desc)
.show

  按年统计注册用户最高的积分,以及平均积分

Df.groupBy(year(col(“add_time”)))
.agg(max(col(“total_mark”).as(“max_mark”)),
Avg(col(“total_mark”).as(“avg_mark”))
)
.show

 

sparkSessiontest的更多相关文章

  1. RDD&Dataset&DataFrame

    Dataset创建 object DatasetCreation { def main(args: Array[String]): Unit = { val spark = SparkSession ...

随机推荐

  1. 群辉DS418play体验+经验分享

    群辉DS418play体验+经验分享     群辉DS418play体验+经验分享   购买初衷 近期百度网盘到期,我又需要重复下载很多资源(游戏.电影.毛片),下载没速度&下完没空间怎么办? ...

  2. jvm 性能调优工具之 jstat 命令详解

    Jstat名称:Java Virtual Machine statistics monitoring tool 官方文档:https://docs.oracle.com/javase/1.5.0/do ...

  3. tomcat启动时间5分钟左右org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [342,445] milliseconds.

    org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance ...

  4. c# winform 访问WebServices (通过Http方式)

    第一步.编写WebServices服务方法 [WebMethod] public void PostJson(string str, string bb) { Dictionary<string ...

  5. F-NAScan:一款网络资产扫描工具

    此脚本的大概流程为: ICMP存活探测-->端口开放探测-->端口指纹服务识别-->提取快照(若为WEB)-->生成结果报表 用法 python NAScan.py -h 10 ...

  6. 单调栈-Maximum Width Ramp

    2020-01-23 19:39:26 问题描述: 问题求解: public int maxWidthRamp(int[] A) { Stack<Integer> stack = new ...

  7. 凸包问题 Graham Scan

    2020-01-09 15:14:21 凸包问题是计算几何的核心问题,并且凸包问题的研究已经持续了好多年,这中间涌现出了一大批优秀的算法. 凸包问题的最优解法是Graham Scan算法,该算法可以保 ...

  8. Contest 141

    2019-06-16 14:35:52 1089. Duplicate Zeros - Easy 问题描述: 问题求解: 很显然的可以使用O(n), O(n)的解法.主要问题在于如何在O(1)空间复杂 ...

  9. [Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted

    字面意思大概就是: [干预]忽略尝试取消带有cancelable = false的touchmove事件的尝试,例如,因为滚动正在进行并且无法中断. 解决方法: 1.添加样式更改 将滑动报错的标签样式 ...

  10. Python如何用virtualenv搭建虚拟环境

    虚拟环境的搭建 优点 1.使不同应用开发环境相互独立 2.环境升级不影响其他应用,也不会影响全局的python环境 3.防止出现包管理混乱及包版本冲突 windows 安装 # 建议使用pip3安装到 ...