spark程序调试记录(未完待续)

本文记录了Spark编程过程中的关键知识点,包括环境搭建、重要网站、引用问题、SQL函数概览、DataFrame操作、异常处理、内存管理、数据保存、Spark Web UI使用等。还涉及Spark与数据库交互、内存分配策略、Spark OOM问题解决、连接MongoDB及Spark配置选项等实战经验。
摘要由CSDN通过智能技术生成

 

目录

前序

一、环境搭建

二、两个网站

网站1、https://search.maven.org

 网站2.maven依赖查询网站

三、引用出错 

四、org.apache.spark.sql.functions汇总

五、spark datafram常用操作和用法

Action 操作

dataframe的基本操作

集成查询

Join操作

spark 将DataFrame所有的列类型改为double

更改列名

六、如何去掉编码过程中数值外面的some(加get可以) 

一.异常信息捕获

二.spark-shell

三.代码提交.master("local")对应的模式。

1. 运行OK的一个提交命令

2.master("local")的配置

四.Spark OOM:java heap space,OOM:GC overhead limit exceeded解决方法

五.spark链接MongD数据库 

六.spark 操作数据库

七.datafram保存

1.保存为CSV文件

 八 .Required executor memory (18432+1843 MB) is above the max threshold (11520 MB) of this cluster

九、SPARK 中 DriverMemory和ExecutorMemory内存大小的计算和合理分配

内存分配实例计算

相关案例1.

 相关案例2.local mode模式下executor的设置

sparkwebUI讲解

Spark-shell调试加入外部包

oozie的使用

Scala Json 转化 

java.lang.NumberFormatException: For input string: "null"


前序

最近闲来无事,记录一下之前学习和使用spark过程中的一些细节,一则用于记录,二则也为他人提供一些参考。由于相隔时间比较长,因此有些内容会参考之前保存的笔记或者网上的相关参考。内容将会持续更新

一、环境搭建

略,见软件安装文章

二、两个网站

网站1、https://search.maven.org

可以用于查找使用的包的版本,以及包对应的maven配置。

 

 

 

 网站2.maven依赖查询网站

https://mvnrepository.com/,可以查询依赖包所需要的版本(版本配置不对,同样会运行失败)

三、引用出错 

import org.json.JSONException;

如上代码编译报错

直接在如下地址搜索org.json.,然后选择用的人数最多的即可

四、org.apache.spark.sql.functions汇总

scala> var data = spark.read.format("csv").option("header",true).load("file:///E:/liyanyan/data/test.csv")

scala> data.printSchema
root
|-- id: string (nullable = true)
|-- name: string (nullable = true)
|-- age: string (nullable = true)
|-- comment: string (nullable = true)
|-- date: string (nullable = true)



scala> data.select(split(data.col("comment")," ")(1).alias("t"),substring(data("
date"),0,8)).withColumn("date1",date_format(current_date(),"yyyyMMdd")).show
+---+---------------------+--------+
| t|substring(date, 0, 8)| date1|
+---+---------------------+--------+
|bbb| 20180102|20180828|
+---+---------------------+--------+

scala> data.select(data("date")).withColumn("date1",from_unixtime(unix_timestamp
(),"yyyyMMdd")).show
+--------------+--------+
| date| date1|
+--------------+--------+
|20180102020325|20180828|
+--------------+--------+

scala> data.select(substring(data("date"),0,8)).withColumn("date1",date_format(c
urrent_date(),"yyyyMMdd")).show
+---------------------+--------+
|substring(date, 0, 8)| date1|
+---------------------+--------+
| 20180102|20180828|
+---------------------+--------+



scala> data.select(regexp_extract(data("comment"),"(\\w+)\\s+(\\w+)",2)).show
+-----------------------------------------+
|regexp_extract(comment, (\w+)\s+(\w+), 2)|
+-----------------------------------------+
| bbb|
+-----------------------------------------+

聚合函数
approx_count_distinct
count_distinct近似值

avg
平均值

collect_list
聚合指定字段的值到list

collect_set
聚合指定字段的值到set

corr
计算两列的Pearson相关系数

count
计数

countDistinct
去重计数 SQL中用法
select count(distinct class)

covar_pop
总体协方差(population covariance)

covar_samp
样本协方差(sample covariance)

first
分组第一个元素

last
分组最后一个元素

grouping


grouping_id


kurtosis
 计算峰态(kurtosis)值

skewness
 计算偏度(skewness)

max
最大值

min
最小值

mean
平均值

stddev
 即stddev_samp

stddev_samp
 样本标准偏差(sample standard deviation)

stddev_pop
总体标准偏差(population standard deviation)

sum
求和

sumDistinct
非重复值求和 SQL中用法
select sum(distinct class)

var_pop
总体方差(population variance)

var_samp
样本无偏方差(unbiased variance)

variance
即var_samp

集合函数
array_contains(column,value)
检查array类型字段是否包含指定元素

explode
 展开array或map为多行

explode_outer
同explode,但当array或map为空或null时,会展开为null。

posexplode
同explode,带位置索引。

posexplode_outer
同explode_outer,带位置索引。

from_json
解析JSON字符串为StructType or ArrayType,有多种参数形式,详见文档。

to_json
转为json字符串,支持StructType, ArrayType of StructTypes, a MapType or ArrayType of MapTypes。

get_json_object(column,path)
获取指定json路径的json对象字符串。
select get_json_object('{"a"1,"b":2}','$.a');
[JSON Path介绍](http://blog.csdn.net/koflance/article/details/63262484)

json_tuple(column,fields)
获取json中指定字段值。select json_tuple('{"a":1,"b":2}','a','b');

map_keys
返回map的键组成的array

map_values
返回map的值组成的array

size
array or map的长度

sort_array(e: Column, asc: Boolean)
将array中元素排序(自然排序),默认asc。

时间函数
add_months(startDate: Column, numMonths: Int)
指定日期添加n月

date_add(start: Column, days: Int)
指定日期之后n天 e.g. select date_add('2018-01-01',3)

date_sub(start: Column, days: Int)
指定日期之前n天

datediff(end: Column, start: Column)
两日期间隔天数

current_date()
当前日期

current_timestamp()
当前时间戳,TimestampType类型

date_format(dateExpr: Column, format: String)
日期格式化

dayofmonth(e: Column)
日期在一月中的天数,支持 date/timestamp/string

dayofyear(e: Column)
日期在一年中的天数, 支持 date/timestamp/string

weekofyear(e: Column)
日期在一年中的周数, 支持 date/timestamp/string

from_unixtime(ut: Column, f: String)
时间戳转字符串格式

from_utc_timestamp(ts: Column, tz: String)
时间戳转指定时区时间戳

to_utc_timestamp(ts: Column, tz: String)
指定时区时间戳转UTF时间戳

hour(e: Column)
提取小时值

minute(e: Column)
提取分钟值

month(e: Column)
提取月份值

quarter(e: Column)
提取季度

second(e: Column)
提取秒

year(e: Column):提取年

last_day(e: Column)
指定日期的月末日期

months_between(date1: Column, date2: Column)
计算两日期差几个月

next_day(date: Column, dayOfWeek: String)
计算指定日期之后的下一个周一、二...,dayOfWeek区分大小写,只接受 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"。

to_date(e: Column)
字段类型转为DateType

trunc(date: Column, format: String)
日期截断

unix_timestamp(s: Column, p: String)
指定格式的时间字符串转时间戳

unix_timestamp(s: Column)
同上,默认格式为 yyyy-MM-dd HH:mm:ss

unix_timestamp():当前时间戳(秒),底层实现为unix_timestamp(current_timestamp(), yyyy-MM-dd HH:mm:ss)

window(timeColumn: Column, windowDuration: String, slideDuration: String, startTime: String)
时间窗口函数,将指定时间(TimestampType)划分到窗口

数学函数
cos,sin,tan
计算角度的余弦,正弦。。。

sinh,tanh,cosh
计算双曲正弦,正切,。。

acos,asin,atan,atan2
计算余弦/正弦值对应的角度

bin
将long类型转为对应二进制数值的字符串For example, bin("12") returns "1100".

bround
舍入,使用Decimal的HALF_EVEN模式,v>0.5向上舍入,v< 0.5向下舍入,v0.5向最近的偶数舍入。

round(e: Column, scale: Int)
HALF_UP模式舍入到scale为小数点。v>=0.5向上舍入,v< 0.5向下舍入,即四舍五入。

ceil
向上舍入

floor
向下舍入

cbrt
Computes the cube-root of the given value.

conv(num:Column, fromBase: Int, toBase: Int)
 转换数值(字符串)的进制

log(base: Double, a: Column):$log_{base}(a)$

log(a: Column):$log_e(a)$

log10(a: Column):$log_{10}(a)$

log2(a: Column):$log_{2}(a)$

log1p(a: Column):$log_{e}(a+1)$

pmod(dividend: Column, divisor: Column):Returns the positive value of dividend mod divisor.

pow(l: Double, r: Column):$r^l$ 注意r是列

pow(l: Column, r: Double):$r^l$ 注意l是列

pow(l: Column, r: Column):$r^l$ 注意r,l都是列

radians(e: Column):角度转弧度

rint(e: Column):Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

shiftLeft(e: Column, numBits: Int):向左位移

shiftRight(e: Column, numBits: Int):向右位移

shiftRightUnsigned(e: Column, numBits: Int):向右位移(无符号位)

signum(e: Column):返回数值正负符号

sqrt(e: Column):平方根

hex(column: Column):转十六进制

unhex(column: Column):逆转十六进制

混杂(misc)函数
crc32(e: Column):计算CRC32,返回bigint

hash(cols: Column*):计算 hash code,返回int

md5(e: Column):计算MD5摘要,返回32位,16进制字符串

sha1(e: Column):计算SHA-1摘要,返回40位,16进制字符串

sha2(e: Column, numBits: Int):计算SHA-1摘要,返回numBits位,16进制字符串。numBits支持224, 256, 384, or 512.

其他非聚合函数
abs(e: Column)
绝对值

array(cols: Column*)
多列合并为array,cols必须为同类型

map(cols: Column*):
将多列组织为map,输入列必须为(key,value)形式,各列的key/value分别为同一类型。

bitwiseNOT(e: Column):
Computes bitwise NOT.

broadcast[T](df: Dataset[T]): Dataset[T]:
将df变量广播,用于实现broadcast join。如left.join(broadcast(right), "joinKey")

coalesce(e: Column*):
返回第一个非空值

col(colName: String):
返回colName对应的Column

column(colName: String):
col函数的别名

expr(expr: String):
解析expr表达式,将返回值存于Column,并返回这个Column。

greatest(exprs: Column*):
返回多列中的最大值,跳过Null

least(exprs: Column*):
返回多列中的最小值,跳过Null

input_file_name():返
回当前任务的文件名 ??

isnan(e: Column):
检查是否NaN(非数值)

isnull(e: Column):
检查是否为Null

lit(literal: Any):
将字面量(literal)创建一个Column

typedLit[T](literal: T)(implicit arg0: scala.reflect.api.JavaUniverse.TypeTag[T]):
将字面量(literal)创建一个Column,literal支持 scala types e.g.: List, Seq and Map.

monotonically_increasing_id():
返回单调递增唯一ID,但不同分区的ID不连续。ID为64位整型。

nanvl(col1: Column, col2: Column):
col1为NaN则返回col2

negate(e: Column):
负数,同df.select( -df("amount") )

not(e: Column):
取反,同df.filter( !df("isActive") )

rand():
随机数[0.0, 1.0]

rand(seed: Long):
随机数[0.0, 1.0],使用seed种子

ran
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值