01-Read&Write

Reader

Read from CSV files

spark.read.csv也可以读取csv文件,而且更常用。

Read from CSV with DataFrameReader’s csv method and the following options:
Tab separator, use first line as header, infer schema

file_csv = "/mnt/training/ecommerce/users/users-500k.csv"
df = (spark.read
  .option("sep", "\t") # 指定分割符为\t
  .option("header", True) # 首行是表头
  .option("inferSchema", True) # 自行判断schema/字段类型
  .csv(file_csv ) # 文件路径
  )
df.printSchema() # 查看一下数据schema

在这里插入图片描述

有时候程序自行判断的schema可能不对,这种情况下我们也可以自行指定。指定schema也有2种方式,分别是StructTypeDDLSchema

StructType
from pyspark.sql.types import DoubleType, IntegerType, StringType, StructField, StructType, TimestampType
userDefinedSchema = StructType([
  StructField("user_id", StringType(), True),
  StructField("user_first_touch_timestamp", LongType(), True),
  StructField("email", StringType(), True)
])

df = (spark.read
  .option("sep", "\t") # 指定分割符为\t
  .option("header", True) # 首行是表头
  .schema(userDefinedSchema) # 使用上面定义的schema
  .csv(file_csv ) # 文件路径
  )

DDLSchema
DDLSchema = "user_id string, user_first_touch_timestamp long, email string"

df = (spark.read
  .option("sep", "\t")
  .option("header", True)
  .schema(DDLSchema)
  .csv(file_csv)
  )

schema信息的获取可以通过两种方式,一是通过df.schema直接获取,该方法生成的schemaStructField类型的。还有一种方式是生成DDLSchema类型,该方式则需要使用scala获取,python没有相应方法。

spark.read.parquet("/mnt/training/ecommerce/events/events.parquet").schema.toDDL

在这里插入图片描述

Read from JSON files

spark.read.json也可以读取json文件,而且更常用。

file_json = "/mnt/training/ecommerce/events/events-500k.json"

df = (spark.read
  .option("inferSchema", True)
  .json(file_json)
  )

df.printSchema()

在这里插入图片描述

这里可以看到json文件有时候是有层级关系的,那么如果指定schema的时候,也要设置相应的层级关系。如上例中,

  • ecommerce

    purchaseRevenus

    total_item_quantity

    unique_items

  • geo

    city

    stats

  • items 里面是个array

    coupon

    item_id

    item_name

    item_revenue_in_usd

    price_in_usd

    quantity

userDefinedSchema = StructType([
  StructField("device", StringType(), True),
  StructField("ecommerce", StructType([
                          StructField("purchaseRevenue", DoubleType(), True),
                          StructField("total_item_quantity", LongType(), True),
                          StructField("unique_items", LongType(), True)
                          ])
              , True),
  StructField("event_name", StringType(), True),
  StructField("event_previous_timestamp", LongType(), True),
  StructField("event_timestamp", LongType(), True),
  StructField("geo", StructType([
                      StructField("city", StringType(), True),
                      StructField("state", StringType(), True)
                    ])
              , True),
  StructField("items", ArrayType(
                      StructType([
                        StructField("coupon", StringType(), True),
                        StructField("item_id", StringType(), True),
                        StructField("item_name", StringType(), True),
                        StructField("item_revenue_in_usd", DoubleType(), True),
                        StructField("price_in_usd", DoubleType(), True),
                        StructField("quantity", LongType(), True)
                      ])
                    )
              , True),
  StructField("traffic_source", StringType(), True),
  StructField("user_first_touch_timestamp", LongType(), True),
  StructField("user_id", StringType(), True)
])

当然也可以用DDLSchema进行指定。

Writer

Write DataFrame to file

通常我们会将数据以parquet格式写出,当然csv格式也是支持的。

parquet格式写出

outputfile_path = f'/mnt/dbwarehouse/files/{filename}.parquet'

df\
.write\
.option("comparession","snappy")\ # 指定压缩方式
.mode("overwrite")\ # 写入方式
.parquet(outputfile_path) # 指定路径

其实还有更简短的写法

df.write.parquet(outputfile_path,mode='overwrite')

csv格式写出

outputfile_path = f'/mnt/dbwarehouse/files/{filename}.csv'

df\
.write\
.mode("overwrite")\ # 写入方式
.csv(outputfile_path) # 指定路径


其实还有更简短的写法

df.write.csv(outputfile_path,mode='overwrite')
Write DataFrame to table

使用saveAsTable将DataFrame保存为table

tb_name = 'users'
df.write.mode("overwrite").saveAsTable(tb_name)
Write DataFrame to delta table

通过指定format为deltasave方法,将DataFrame保存为delta table

delta_tb_path = '/mnt/dbwarehouse/delat/users'
df.write.format('delat').mode('overwrite').save(delta_tb_path)

这篇文档讲述的是最为基础的读写方式,在实际的工作场景中,当遇到数据写入时,我们通常是先建表,在建表的时候指定数据的类型,存储路径(路径通常在建库时指定),如parquet,textfile,delta,将DataFrame 注册成临时视图createOrReplaceTempView后,再往表里写入。如

create database if not exists demo location "/mnt/dbwarehouse/demo";
create table if not exists demo.first_table(
id int,
value string
) using delta

df.createOrReplaceTempView('df')
spark.sql('insert overwrite demo.first_table select * from df')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统根据B/S,即所谓的电脑浏览器/网络服务器方式,运用Java技术性,挑选MySQL作为后台系统。系统主要包含对客服聊天管理、字典表管理、公告信息管理、金融工具管理、金融工具收藏管理、金融工具银行卡管理、借款管理、理财产品管理、理财产品收藏管理、理财产品银行卡管理、理财银行卡信息管理、银行卡管理、存款管理、银行卡记录管理、取款管理、转账管理、用户管理、员工管理等功能模块。 文中重点介绍了银行管理的专业技术发展背景和发展状况,随后遵照软件传统式研发流程,最先挑选适用思维和语言软件开发平台,依据需求分析报告模块和设计数据库结构,再根据系统功能模块的设计制作系统功能模块图、流程表和E-R图。随后设计架构以及编写代码,并实现系统能模块。最终基本完成系统检测和功能测试。结果显示,该系统能够实现所需要的作用,工作状态没有明显缺陷。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。进入银行卡列表,管理员可以进行查看列表、模糊搜索以及相关维护等操作。用户进入系统可以查看公告和模糊搜索公告信息、也可以进行公告维护操作。理财产品管理页面,管理员可以进行查看列表、模糊搜索以及相关维护等操作。产品类型管理页面,此页面提供给管理员的功能有:新增产品类型,修改产品类型,删除产品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值