import org.apache.spark.sql.SparkSession
object SparkSessionExample {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder
.appName("Spark Session Example")
.config("spark.master", "local")
.getOrCreate()
// 你的代码逻辑
spark.stop()
}
}
import org.apache.spark.sql.{SparkSession, DataFrame}
object ReadCSV {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder
.appName("Read CSV Example")
.config("spark.master", "local")
.getOrCreate(([laipuhuo.com])
// 读取CSV文件
val df: DataFrame = spark.read
.option("header", "true")
.csv("path/to/file.csv")
df.show()
spark.stop()
}
}
import org.apache.spark.sql.{SparkSession, DataFrame}
import org.apache.spark.sql.functions._
object DataProcessing {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder
.appName("Data Processing Example")
.config("spark.master", "local")
.getOrCreate()
val df = spark.read
.option("header", "true")
.csv("path/to/file.csv")
// 过滤数据
val filteredDF = df.filter(col("age") > 30)
// 计算平均值
val averageAge = df.groupBy("department").agg(avg("age").as("average_age"))
// 数据展示
filteredDF.show()
averageAge.show()
spark.stop()
}
}
import org.apache.spark.sql.SparkSession
object SparkSQLExample {([laipuhuo.com]
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder
.appName("Spark SQL Example")
.config("spark.master", "local")
.getOrCreate()
val df = spark.read
.option("header", "true")
.csv("path/to/file.csv")
df.createOrReplaceTempView("people")
val sqlDF = spark.sql("SELECT * FROM people WHERE age > 30")
sqlDF.show()
spark.stop([[laipuhuo.com](https://www.laipuhuo.com)]
}
}