DataFrame中的行动算子操作2

2022/8/30 23:22:48

本文主要是介绍DataFrame中的行动算子操作2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

## 修改hdfs-site.xml
<property> 
  <name>hive.metastore.warehouse.dir</name> 
  <value>hdfs://node1:9000/user/hive/warehouse</value>
  <description>location of default database for the warehouse</description>
</property>

## 保存至table表格中
val sparkConf = new SparkConf().setAppName("demo03").setMaster("local[*]")
val ss = SparkSession.builder().config(sparkConf).getOrCreate()

import ss.implicits._
val seq: Seq[(String, Int)] = Array(
  ("zs", 20),
  ("ls", 21),
  ("ww", 22),
  ("ml", 23),
  ("zb", 24),
  ("wb", 20)
)
val dataFrame: DataFrame = seq.toDF("name", "age")
dataFrame.createOrReplaceTempView("student")
val dataFrame1 = ss.sql("select * from student where age>22")
dataFrame1.show()

/**
 * 将数据写出到spark.sql.warehouse.dir 路径下
 */
dataFrame1.write.mode(SaveMode.Overwrite).saveAsTable("stud")
val frame = ss.sql("select * from stud")
frame.show()
val frame1 = ss.sql("show tables")
frame1.show()

/*
    将数据以结构化文件写出 需要跟一个目录
*/
dataFrame1.write.mode(SaveMode.Append).csv("hdfs://node1:9000/sparksql/a.csv")
dataFrame1.write.mode(SaveMode.Append).json("hdfs://node1:9000/sparksql/json")
dataFrame1.write.mode(SaveMode.Append).parquet("hdfs://node1:9000/sparksql/parquet")
dataFrame1.write.mode(SaveMode.Append).orc("hdfs://node1:9000/sparksql/orc")
val properties = new Properties()
properties.setProperty("user", "root")
properties.setProperty("password", "Jsq123456...")
dataFrame1.write.mode(SaveMode.Append).jdbc("jdbc:mysql://node1:3306/project?serverTimezone=UTC", "student_info", properties)

SaveMode介绍

SaveMode 解释
SaveMode.ErrorIfExists(default) 如果文件存在,则报错
SaveMode.Append 追加
SaveMode.Overwrite 覆盖写
SaveMode.Ignore 数据存在,则忽略


这篇关于DataFrame中的行动算子操作2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程