spark-core (scala版本)

1.spark-RDD

RDD创建

    val conf : SparkConf = new SparkConf().setMaster("local[*]").setAppName("wordcount")
    //创建上下文对象
    val sc = new SparkContext(conf)

    //内存中创建RDD,底层实现是parallelize
    val arrayRDD : RDD[Int]= sc.makeRDD(Array(1,2,3))
    //内存中创建parallelize
    val listRdd:RDD[Int] = sc.parallelize(Array(1,2,3,4))

    //外部存储中创建
    val fileRDD : RDD [String] = sc.textFile("in")

    listRdd.collect().foreach(println)

RDD的读取和存储

val fileRDD : RDD [String] = sc.textFile("in")

fileRDD.saveAsTextFile("output")

driver和excutor

Driver :创建spark上下文对象的应用程序为Driver
Executor:接受任务执行任务

算子在Executor里执行
Driver里执行上部分的所有代码

发送任务
发送任务
Driver
Executor

网络中传输的是序列化的字符或字符串或数字。

2.RDD算子

map算子

map算子作为最通用的算子对RDD内的元素进行处理

    val sparkConf = new SparkConf().setAppName("map").setMaster("local[2]")
    val sc = new SparkContext(sparkConf)
    val number = Array(1,2,3,4,5)
    val numberRDD = sc.parallelize(number)
    val multipleRdd = numberRDD.map(num => num *2)
    multipleRdd.foreach(num => println(num))

reduce 算子

reduce为action算子,对RDD内元素做处理

  def reduce(): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName("reduce")
    val sc = new SparkContext(conf)
    val number = Array(1, 2, 3, 4, 5, 5, 6, 7, 8)
    val numberRDD = sc.parallelize(number)
    val sum = numberRDD.reduce(_ + _)
    println(sum)
    sc.stop()
  }

flatMap算子

需求给定单词列表[“Hello”,“World”],你想要返回列表[“H”,“e”,“l”, “o”,“W”,“r”,“d”]
这时候使用flatmap比较合适,它是一对多或多对多的处理

    val conf = new SparkConf().setAppName("flatMap").setMaster("local[*]")
    val sc = new SparkContext(conf)
    val lineArray = Array("hello you", "hello me","hello world")
    val lineRDD =sc.parallelize(lineArray)
    lineRDD.foreach(line => println(line))
    val words = lineRDD.flatMap( line => line.split(" "))
    words.foreach(line => println(line))

在这里插入图片描述

groupByKey算子

对ky算子进行group处理

  val conf = new SparkConf().setMaster("local[*]").setAppName("groupByKey")
    val sc = new SparkContext(conf)
    val socre = Array(Tuple2("class1",80),Tuple2("class2",70),Tuple2("class2",20),Tuple2("class2",90),Tuple2("class1",80))
    val scoreRDD = sc.parallelize(socre)
    scoreRDD.groupByKey().foreach(a => println(a._2,a._1))
//      .foreach(score => { println(score._1);score._2.foreach(singlescore => println(singlescore))})
//    println("===============")
    sc.stop()

reduceByKey算子

对ky算子进行reduce处理(对相同Key值的rdd进行value相加)

    val conf = new SparkConf().setAppName("reduceByKey").setMaster("local[*]")
    val sc = new SparkContext(conf)
    val score = Array(Tuple2("class1",80),Tuple2("class2",70),Tuple2("class2",20),Tuple2("class2",90),Tuple2("class1",80))
    val scoreRDD = sc.parallelize(score)
    scoreRDD.reduceByKey(_+_).foreach(num => println(num._1+ " : " + num._2) )
    sc.stop()

sortByKey

对ky算子进行排序处理

    val conf = new SparkConf().setMaster("local").setAppName("sortByKey")
    val sc = new SparkContext(conf)
    val score = Array(Tuple2(80,"class1"),Tuple2(70,"class3"),Tuple2(20,"class4"),Tuple2(90,"class5"),Tuple2(80,"class6"))
    val scoreRDD = sc.parallelize(score)
    scoreRDD.sortByKey().foreach(num => println(num._1 + " : " + num._2))
    sc.stop()

join算子

对ky算子进行join操作

    val conf = new SparkConf().setAppName("join").setMaster("local")
    val sc = new SparkContext(conf)
    val score = Array(Tuple2(1,80),Tuple2(1,70),Tuple2(2,30),Tuple2(3,50))
    val student = Array(Tuple2(1,"xiaohua"),Tuple2(2,"xiaoming"),Tuple2(3,"xiaochen"))
    val scoreRDD = sc.parallelize(score)
    val studentRDD = sc.parallelize(student)
    scoreRDD.join(studentRDD).foreach(stu => println("id :"+  stu._1 + " , name: " + stu._2._2 + " , score : " + stu._2._1))

filter 算子

filter算子用于过滤RDD内的元素
案例:取偶数

   val conf = new SparkConf()
      .setMaster("local")
      .setAppName("filter")
    val sc = new SparkContext(conf)
    val number = Array(1,2,3,4,5,6,7,8,9)
    val numberRDD = sc.parallelize(number)
    val eventNumberRDD = numberRDD.filter(num => num % 2 ==0)
    eventNumberRDD.foreach(num =>println(num))

mapPartitionsWithIndex(func)算子

该算子主要用于查询分区的名称

    val arrayRDD : RDD[Int]= sc.makeRDD(1 to 10 )

    val tupleIndexRDD : RDD[(Int,String)] = arrayRDD.mapPartitionsWithIndex{
      case(num,datas)=>{
        datas.map((_,"分区号:"+num))
      }
    }
    tupleIndexRDD.foreach(println)

输出结果

(4,,分区号:2)
(5,,分区号:2)
(1,,分区号:0)
(6,,分区号:3)
(7,,分区号:4)
(8,,分区号:4)
(2,,分区号:1)
(3,,分区号:1)
(9,,分区号:5)
(10,,分区号:5)

glom算子

将每一个分区形成一个数组,形成新的RDD类型时RDD[Array[T]]

val arrayRDD : RDD[Int]= sc.makeRDD(List(1,2,3,4,5,6,7,8),4 )
    val glomRDD:RDD[Array[Int]] = arrayRDD.glom()
      glomRDD.collect().foreach(array=>{
      println(array.mkString(","))
    })
  }

在这里插入图片描述

take算子

去RDD前几元素

    val conf = new SparkConf().setMaster("local").setAppName("take
    val sc = new SparkContext(conf)
    val number = Array(1,2,3,4,5,5,6,7,8)
    val numberRDD = sc.parallelize(number)
    val top = numberRDD.take(3)
    for(num <- top){
    println(num)}
    sc.stop()

takeOrdered算子

排序后取RDD的前N位
take是直接取RDD的前N位

broadcast变量

广播主要用于提高效率,将一个参数传入一组RDD内

    val conf = new SparkConf().setAppName("broadcast").setMaster("local")
    val sc = new SparkContext(conf)
    val factor = 3
    val factorBroadcast = sc.broadcast(factor)
    val number = Array(1,2,3,4,5)
    val numberRDD = sc.parallelize(number)
    numberRDD.map(num => num *factorBroadcast.value).foreach(num => println(num))
    sc.stop(

3.RDD操作MYSQL

查询操作

object spark_scala_test_delete {
  def main(args: Array[String]): Unit = {
    val conf : SparkConf = new SparkConf().setMaster("local[*]").setAppName("wordcount")
    //创建上下文对象
    val sc = new SparkContext(conf)

    val driver = "com.mysql.jdbc.Driver"
    val url = "jdbc:mysql://localhost:3306/test"
    val userName = "root"
    val password = "root"

    val sql = "select CNO from course where tno >= ? and tno <= ?"
    val jdbcRDD = new JdbcRDD(
      sc,()=>{
        Class.forName(driver)
        java.sql.DriverManager.getConnection(url,userName,password)
      },
      sql,
      0,1000,2,
      (rs)=>{
        println(rs.getString(1))
      }

    )
    jdbcRDD.collect()
  }
}

问好代表上下限,2为分区数

插入操作

val dataRDD = sc.makeRDD(List(("1-111","摩登教育","999"),("2-222","摩登家庭","888")))
    dataRDD.foreach {
      case(a,b,c)=> {
        Class.forName(driver)
        val connection = java.sql.DriverManager.getConnection(url, userName, password)
        val sql = "insert into course (CNO,CNAME,TNO) value (?,?,?)"
        val statement = connection.prepareStatement(sql)
        statement.setString(1,a)
        statement.setString(2,b)
        statement.setString(3,c)
        statement.executeUpdate()
        statement.close()
        connection.close()
      }
    }

上图为RDD的遍历
改为foreachPartition
datas是集合中的数据循环遍历
不涉及到网络数据传递
效率高于上图,以partition为单位
若有两个分区,以下逻辑只走两边
连接的对象只有两个,会少于之前的连接对象,提高效率
缺点:OOM
分区循环完毕,继续加载数据会导致内存溢出

    dataRDD.foreachPartition(datas=> {
      Class.forName(driver)
      val connection = java.sql.DriverManager.getConnection(url, userName, password)
      datas.foreach {
        case (a, b, c) => {
          val sql = "insert into course (CNO,CNAME,TNO) value (?,?,?)"
          val statement = connection.prepareStatement(sql)
          statement.setString(1, a)
          statement.setString(2, b)
          statement.setString(3, c)
          statement.executeUpdate()
          statement.close()
        }
      }
      connection.close()
    }
    )

4.Spark-Core IDE开发

pom.xml配置

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.8</source>
                <target>1.8</target>
                <showWarnings>true</showWarnings>
            </configuration>
        </plugin>

        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.1.6</version>
            <configuration>
                <scalaVersion>2.11.8</scalaVersion>
            </configuration>
            <executions>
                <execution>
                    <id>scala-compile</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>cn.strong.leke.bigdata.app.attendance.TeacherAttandanceStatApp</mainClass>
                            </transformer>
                                            </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Maven Project 里的package 打包

连接sparkContext

SparkConf conf = new SparkConf().setAppName("conf_test").setMaster("local[*]");
        JavaSparkContext sc = new JavaSparkContext(conf);

wordcount案例

val conf = new SparkConf().setMaster("local").setAppName("wordcount")
val sc = new SparkContext(conf)
val lines = sc.textFile("file:///data/people.txt")
val words = lines.flatMap(line => line.split(" "))
//    words.foreach(num => println(num))
val pairs = words.map(num => (num,1))
val wordCount = pairs.reduceByKey(_+_)
//    wordCount.foreach(num => println(num))
val countworks = wordCount.map(num => (num._2,num._1))
val sortCount = countworks.sortByKey(false)
val sortedCount = sortCount.map(num=> (num._2,num._1))
sortedCount.foreach(num => println(num))
sc.stop()

spark IDE 上传后运行命令

bin/spark-submit \
--class com.csz.bigdata.spar
k.spark_wordcount \
/opt/jarPackage/spark-bigdata-1.0-shaded.jar 

5.spark standalone 模式

安装使用

1.进入conf,修改配置文件

cd /opt/module/spark/conf

mv slaves.template slaves
mv spark-env.sh.template spark-env.sh

2.修改slaves

master
slave1
slave2

3.添加配置

vim spark-env.sh

SPARK_MASTER_HOST=master
SPARK_MASTER_PORT=7077

配置sbin下的spark-config.sh
添加

export JAVA_HOME=/opt/module/jdk

4.分发执行

scp -r /opt/module/spark root@slave1:/opt/module/

scp -r /opt/module/spark root@slave2:/opt/module/

5.启动

sbin/start-all.sh
jps

6.官方求PI案例

bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://master:7077 \
--executor-memory 1G \
--total-executor-cores 2 \
./examples/jars/spark-examples_2.11-2.4.4.jar \
100

java io

java IO
输入,输出
字节(rar,zip,dat,png,jpeg),字符(txt)

//文件输入流
InputStream in = new FileInputStream("xxxxx")
//缓冲流
InputStream bufferIn = new BufferedInputStream( new FileInputStream("xxxx"))
//使用字符流读取一行数据
Reader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"))
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值