网站建设 资质要求,网络 网站建设办公,腾讯云服务器1元,网站是哪个公司做的好处1. 数据源的读取与写入
1.1 数据读取
读文件 read.jsonread.csv csv文件由两个部分组成#xff1a;头部数据#xff08;也就是字段数据#xff09;、行数据。 read.orc 读数据库 read.jdbc(jdbc连接地址,table‘表名’,properties{‘user’用户名,‘password’密码,‘driv…1. 数据源的读取与写入
1.1 数据读取
读文件 read.jsonread.csv csv文件由两个部分组成头部数据也就是字段数据、行数据。 read.orc 读数据库 read.jdbc(jdbc连接地址,table‘表名’,properties{‘user’用户名,‘password’密码,‘driver’‘驱动信息’}) 数据库创建测试数据:
create database itcast charsetutf8;create table itcast.tb_user(id int,name varchar(20),age int,gender varchar(20)
);insert into itcast.tb_user values (1,张三,20,男);表查看 读取数据库数据
# 读取数据源将数据转为DF
from pyspark.sql import SparkSessionss SparkSession.builder.getOrCreate()# read读取数据库数据
# 使用jdbc方法通过jdbc读取数据库数据在读取数据库之前需要现将数据库连接驱动放入spark的jars目录下
#
df ss.read.jdbc(jdbc:mysql://192.168.88.100:3306/itcast,tabletb_user,properties{user:root,password:123456,driver:com.mysql.jdbc.Driver})
df.show()运行结果
1.2 数据写入 因为数据是在df中存储所以使用DataFrame进行数据写入 使用DataFrame的的write方法 写入文件有个模式,覆盖和追加两种方式用mode参数指定 覆盖 overwrite 追加 append 写入文件 write.jsonwrite.csvwrite.orc 写入数据库 write.jdbc(jdbc连接地址,table‘表名’,properties{‘user’用户名,‘password’密码,‘driver’‘驱动信息’},mode‘写入方式’)
# 数据写入
from pyspark.sql import SparkSession,Row
ss SparkSession.builder.getOrCreate()df ss.createDataFrame([Row(id1,name张三,age20),Row(id2,name李四,age20),Row(id3,name王五,age20)],schemaid int,name string,age int
)# 将df数据写入hdfs文件中 modeoverwrite 覆盖写入 append 追加写入
df.write.json(hdfs://node1:8020/data_json,modeoverwrite)# 写入数据库
# create table itcast.tb_stu(
# id int,
# name varchar(20),
# age int
# );
# 在jdbc连接中指定编码字符集为utf-8
df.write.jdbc(jdbc:mysql://192.168.88.100:3306/itcast?characterEncodingutf8,tabletb_stu,modeoverwrite,properties{user:root,password:123456,driver:com.mysql.jdbc.Driver})运行结果
2. 自定义函数 2.1 函数分类
udf 自定义一进一出 udaf 聚合自定义多进一出 udtf 爆炸一进多出
2.2 UDF函数
对每一行数据依次进行计算返回每一行的结果。
#UDF函数
from pyspark.sql import SparkSession,functions as F
from pyspark.sql.types import *ss SparkSession.builder.getOrCreate()#读取文件数据转为df
df ss.read.csv(hdfs://node1:8020/data/students.csv,headerTrue,sep,)df.show()#自定义字符串长度计算函数
def len_func(field):if field is None:return 0else:data len(field)return data
#将自定义的函数注册到spark中使用
len_func ss.udf.register(len_func, len_func,returnTypeIntegerType())#在spark中使用
df2 df.select(id,name,gender,len_func(name))
df2.show()#sql语句中使用
df.createTempView(stu)
df3 ss.sql(select *,len_func(name) from stu)
df3.show()2.3 UDAF函数
多进一出 主要是聚合 使用pandas中的series实现可以读取一列数据存储在pandas的series中进行数据的聚合。
#UDAF函数
from pyspark.sql import SparkSession,functions as F
from pyspark.sql.types import *
import pandas as pdss SparkSession.builder.getOrCreate()#读取文件数据转为df
df ss.read.csv(hdfs://node1:8020/data/students.csv, headerTrue,sep,,schema id int,name string,age int,gender string,cls string)df.show()#对某个字段的整列数据进行计算自定义udaf函数
# 第一步装饰器注册
F.pandas_udf(returnTypeIntegerType())
def sub(field:pd.Series) - int:nfield[0] #取出第一个值作为初始值for i in field[1::]:n-ireturn n
#第二步register方法注册
sub ss.udf.register(sub, sub)df2 df.select(sub(age))
df2.show()
文章转载自: http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.ygztf.cn.gov.cn.ygztf.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.hflrz.cn.gov.cn.hflrz.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.rlbc.cn.gov.cn.rlbc.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.rykn.cn.gov.cn.rykn.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.qymqh.cn.gov.cn.qymqh.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.gfrtg.com.gov.cn.gfrtg.com http://www.morning.cwknc.cn.gov.cn.cwknc.cn