前言
我们用IDEA创建Spark项目的时候,默认都是使用SBT作为构建工具的,那么SBT是个啥?
SBT是 Scala 的构建工具,全称是 Simple Build Tool, 类似 Maven 或 Gradle。
SBT 的野心很大,采用Scala编程语言本身编写配置文件,这使得它稍显另类,虽然增强了灵活性,但是对于初学者来说同时也增加了上手难度。
另外由于SBT默认从国外下载依赖,导致第一次构建非常缓慢。
设置全局仓库
默认从国外下载依赖,我们可以设置仓库的地址来加快构建速度。
设置仓库
华为镜像站
https://mirrors.huaweicloud.com/home
推荐
直接在华为镜像站中下载对应的配置文件直接覆盖就行。
如果是Windows系统,则进入CMD执行如下命令:
1 2 3 4 5
| cd ~ mkdir .sbt cd .\.sbt\ echo "">repositories start repositories
|
如果是Mac或Linux系统,则进入Bash执行如下命令:
1 2 3 4
| cd ~ mkdir .sbt cd .sbt vi repositories
|
然后创建 repositories 文件内容如下,并将文件拷贝到 .sbt 目录下,
1 2 3 4
| [repositories] local huaweicloud-ivy: https://mirrors.huaweicloud.com/repository/ivy/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext] huaweicloud-maven: https://mirrors.huaweicloud.com/repository/maven/
|
忽略自身仓库
设置所有项目均使用全局仓库配置,忽略项目自身仓库配置
该参数可以通过 Java System Property 进行设置。在 SBT 中,有三种方法可以设置 Java System Property,可以根据需要自行选择。
方法一:修改SBT配置文件(推荐)
提醒一下, sbt-1.3.0/conf/ 目录下有两个配置文件, sbtconfig.txt 仅适用于 Windows 平台,而 sbtopts 仅适用于 Mac/Linux 平台。
针对 Windows 平台,打开 sbt-1.3.0/conf/sbtconfig.txt 文件,在末尾新增一行,内容如下:
1
| -Dsbt.override.build.repos=true
|
针对 Mac/Linux 平台,打开 sbt-1.3.0/conf/sbtopts 文件,在末尾新增一行,内容如下:
1
| -Dsbt.override.build.repos=true
|
方法二: 设置环境变量
在 Windows 上通过 set 命令进行设置,
1
| set SBT_OPTS="-Dsbt.override.build.repos=true"
|
在 Mac/Linux 上使用 export 命令进行设置,
1
| export SBT_OPTS="-Dsbt.override.build.repos=true"
|
创建项目
修改build.sbt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| name := "SparkDemo01"
version := "1.0-SNAPSHOT"
scalaVersion := "2.12.15"
idePackagePrefix := Some("org.example")
val sparkVersion = "3.1.3"
// 将阿里云仓库做为默认仓库 externalResolvers := List("my repositories" at "https://maven.aliyun.com/nexus/content/groups/public/")
libraryDependencies ++= Seq( "org.apache.spark" %% "spark-core" % sparkVersion, "org.apache.hbase" % "hbase-client" % "2.1.10", "org.apache.hbase" % "hbase-common" % "2.1.10", "org.apache.hbase" % "hbase-server" % "2.1.10", "org.apache.hbase" % "hbase-mapreduce" % "2.1.10", "org.apache.hbase" % "hbase-protocol" % "2.1.10" )
|
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package org.example
import org.apache.hadoop.hbase._ import org.apache.hadoop.hbase.mapreduce.TableInputFormat import org.apache.hadoop.hbase.util.Bytes import org.apache.spark.{SparkConf, SparkContext}
object SparkHBase { def main(args: Array[String]): Unit = { val config = new SparkConf() config .setMaster("local[*]") .setAppName("SparkHBase") val sc = new SparkContext(config)
val conf = HBaseConfiguration.create() conf.set(TableInputFormat.INPUT_TABLE, "student") val stuRDD = sc.newAPIHadoopRDD( conf, classOf[TableInputFormat], classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable], classOf[org.apache.hadoop.hbase.client.Result] ) val count = stuRDD.count() println("Students RDD Count:" + count) stuRDD.cache()
stuRDD.foreach({ case (_, result) => val key = Bytes.toString(result.getRow) val s_name = Bytes.toString(result.getValue("s_name".getBytes, "".getBytes)) val math = Bytes.toString(result.getValue("s_course".getBytes, "math".getBytes)) println("Row key:" + key) println("s_name:" + s_name) println("math:" + math) }) } }
|
如果运行报错
Error running HBaseSpark. Command line is too long.
找到.idea下的workspace.xml
搜索 PropertiesComponent
内部添加
1
| <property name="dynamic.classpath" value="true" />
|
添加后结构如下
1 2 3 4 5 6 7 8 9 10
| <component name="PropertiesComponent"> <property name="dynamic.classpath" value="true" /> <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="WebServerToolWindowFactoryState" value="false" /> <property name="project.structure.last.edited" value="Modules" /> <property name="project.structure.proportion" value="0.0" /> <property name="project.structure.side.proportion" value="0.0" /> <property name="vue.rearranger.settings.migration" value="true" /> </component>
|