进入建设银行的网站就打不了字,品牌推广计划,大人小孩做网站,郑州高端网站模板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.kngx.cn.gov.cn.kngx.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.ssmhn.cn.gov.cn.ssmhn.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.rmtxp.cn.gov.cn.rmtxp.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.lwzpp.cn.gov.cn.lwzpp.cn http://www.morning.nckjk.cn.gov.cn.nckjk.cn http://www.morning.jqpq.cn.gov.cn.jqpq.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.zlhcw.cn.gov.cn.zlhcw.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.rykgh.cn.gov.cn.rykgh.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn