ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Spark学习第二步 SparkSql

2021-07-11 10:04:11  阅读:143  来源: 互联网

标签:slf4j ZHAO SparkSql apache org Spark spark 第二步 2007


Spark学习第二步 SparkSql

文章目录

前言

一、是什么?

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、使用步骤

1.配置依赖(Maven)以及文件

使用Maven包管理工具。在pom.xml中添加需要的依赖

<?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>sxnd_scala</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.11</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.11</artifactId>
            <version>2.5.23</version>
        </dependency>


        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.1.1</version>

        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>

        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.16</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
    </dependencies>

</project>

写一个需要读取的文件,分为三个部分,第一是订单号,第二是购买人,第三是购买日期,写入一个txt文件中。

BYSL00000893,ZHAO,2007-8-23
BYSL00000897,ZHAO,2007-8-24
BYSL00000898, ZHAO,2007-8-25
BYSL00000899,ZHAO,2007-8-26
BYSL00000900, ZHAO,2007-8-26
BYSL00000901,ZHAO,2007-8-27
BYSL00000902,ZHAO,2007-8-27
BYSL00000904,ZHAO,2007-8-28
BYSL00000905,ZHAO,2007-8-28
BYSL00000906,ZHAO,2007-8-28
BYSL00000907,ZHAO,2007-8-29
BYSL00000908,ZHAO,2007-8-30
BYSL00000909,ZHAO,2007-9-1
BYSL00000910,ZHAO,2007-9-1
BYSL00000911,ZHAO,2007-8-31
BYSL00000912,ZHAO,2007-9-2
BYSL00000913,ZHAO,2007-9-3
BYSL00000914,ZHAO,2007-9-3

2.读入数据

在创建的maven文件中,找到scala文件夹,在下边创建文件,用来进行数据的读取

package day_03

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession

class SparkSqlTest {

}
case class tbStock(ordernumber:String,localtionid:String,dateid:String) extends Serializable

object Test{
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local[3]").appName("SparkSqlTest").getOrCreate()
    import spark.implicits._
    val sc = spark.sparkContext
    var rdd:RDD[String] = sc.textFile("D:\\SparkSQL\\tbStock.txt")
    val ds = rdd.map(_.split(",")).map(strArr=>{
      tbStock(strArr(0),strArr(1),strArr(2))
    }
    ).toDS
  }
}

问题

在idea中添加依赖之后的下载
在这里插入图片描述

总结

标签:slf4j,ZHAO,SparkSql,apache,org,Spark,spark,第二步,2007
来源: https://blog.csdn.net/weixin_45714844/article/details/118650842

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有