SparkSql

SparkSql

pom.xml

javascript 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spark_sql</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.12</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 该插件用于将 Scala 代码编译成 class 文件 -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <!-- 声明绑定到 maven 的 compile 阶段 -->
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

SparkSQL01_Demo

javascript 复制代码
import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

object SparkSQL01_Demo {
  def main(args:Array[String])={
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("sparkSQL")
    val spark = SparkSession.builder().config(sparkConf).getOrCreate()

    val df = spark.read
      .format("jdbc")
      .option("url", "jdbc:mysql://hadoop102:3306/localstreamdata")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "000000")
      .option("dbtable", "normal_data")
      .load()
    df.show

    spark.close()
  }

}

sparksql写入mysql

提前在mysql中建好表

javascript 复制代码
use localstreamdata;
DESCRIBE normal_data;
CREATE TABLE IF NOT EXISTS gpb2 (
    stream_id varchar(20),
    stream_time datetime,
    stream_user_id bigint(20),
    stream_money int(11),
    stream_consume_type int(11),
    stream_consume_location varchar(50),
    stream_sign_location varchar(50),
    stream_time_date int(11),
    stream_time_minute varchar(20),
    stream_seconds int(11),
    stream_is_new int(3),
    stream_is_normal varchar(20)
);
DESCRIBE gpb2;
alter table gpb2 change stream_consume_location stream_consume_location varchar(100) character set utf8;
alter table gpb2 change stream_sign_location stream_sign_location varchar(100) character set utf8;
javascript 复制代码
import org.apache.spark.SparkConf
import org.apache.spark.sql.{Column, SaveMode, SparkSession}
import org.apache.spark.sql.functions._
import org.apache.spark.sql._

object SparkSQL01_Demo {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("sparkSQL")
    val spark = SparkSession.builder().config(sparkConf).getOrCreate()

    val df = spark.read
      .format("jdbc")
      .option("url", "jdbc:mysql://hadoop102:3306/localstreamdata?characterEncoding=utf8&useSSL=false")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "000000")
      .option("dbtable", "normal_data")
      .load()

    df.show

    import spark.implicits._
    val cleanedDF = df.withColumn("stream_consume_location", your_clean_function(col("stream_consume_location")))

    cleanedDF.write
      .format("jdbc")
      .option("url", "jdbc:mysql://hadoop102:3306/localstreamdata?characterEncoding=utf8&useSSL=false")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "000000")
      .option("dbtable", "gpb2")
      .mode(SaveMode.Append)
      .save()

    spark.close()
  }

  def your_clean_function(str: Column): Column = {
    // 根据需要实现清理或转换逻辑
    // 返回清理后的字符串列
    // 示例代码:
    str
  }
}
javascript 复制代码
import org.apache.spark.SparkConf
import org.apache.spark.sql.{Column, SaveMode, SparkSession}
import org.apache.spark.sql.functions._
import org.apache.spark.sql._

object SparkSQL01_Demo {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("sparkSQL")
    val spark = SparkSession.builder().config(sparkConf).getOrCreate()

    val df = spark.read
      .format("jdbc")
      .option("url", "jdbc:mysql://hadoop102:3306/localstreamdata?characterEncoding=utf8&useSSL=false")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "000000")
      .option("dbtable", "normal_data")
      .load()

    df.show

    import spark.implicits._
    //val cleanedDF = df.withColumn("stream_consume_location", your_clean_function(col("stream_consume_location")))

    df.write
      .format("jdbc")
      .option("url", "jdbc:mysql://hadoop102:3306/localstreamdata?characterEncoding=utf8&useSSL=false")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "000000")
      .option("dbtable", "gpb2")
      .mode(SaveMode.Append)
      .save()

    spark.close()
  }
/*
  def your_clean_function(str: Column): Column = {
    // 根据需要实现清理或转换逻辑
    // 返回清理后的字符串列
    // 示例代码:
    str
  }

 */
}
相关推荐
BD_Marathon11 小时前
Spark:背压机制
大数据·分布式·spark
_waylau11 小时前
Spark 新作《循序渐进 Spark 大数据应用开发》简介
大数据·分布式·spark·应用开发
青云游子12 小时前
pySpark乱码
spark
遥遥晚风点点13 小时前
spark 设置hive.exec.max.dynamic.partition不生效
大数据·hive·spark
Java资深爱好者18 小时前
数据湖与数据仓库的区别
大数据·数据仓库·spark
一个处女座的程序猿1 天前
LLMs之Code:Github Spark的简介、安装和使用方法、案例应用之详细攻略
大数据·spark·github
阿里云大数据AI技术2 天前
Apache Spark & Paimon Meetup · 北京站,助力 LakeHouse 架构生产落地
大数据·架构·spark·apache
天冬忘忧2 天前
Spark 共享变量:广播变量与累加器解析
大数据·python·spark
天冬忘忧2 天前
Spark 中的 RDD 分区的设定规则与高阶函数、Lambda 表达式详解
大数据·分布式·spark
天冬忘忧2 天前
PySpark 数据处理实战:从基础操作到案例分析
大数据·python·spark