随着 spark 使用深入,越来越感觉用 java 是不行的了,社区里大家都用 scala 讨论,用 java 就像一个语言不通的人一样,换呗,先搭环境,再学语法,都不是什么难点,就是花时间,环境最是烦人,耗时耗力还不讨好,语法倒好,以我十八般武艺样样不精通的经验来说,就是个类似 js 的函数式,不难
先看环境问题,scala eclipse sbt 应用程序开发,From zero to a Scala SBT project with Eclipse / Intellij IDEA support,算是比较手把手的文档
这里有提到一个文件夹生成不出来的问题,算了,也就是自己动手建立一个文件夹的事情,Creating empty eclipse project using sbt
这里有两个插件,sbt/sbt-assembly,https://github.com/typesafehub/sbteclipse
至此能够打出一个 fat jar 包来
======================================================
然后就是琢磨着怎么换 repo 源的问题了,这年代,啥语言都要搞一个自己的包管理,也是醉了
修改sbt和maven镜像源,这里给出了详细的配置单
修改sbt镜像源
以使用oschina镜像源为例,在~/.sbt/下添加一个repositories文件,里面内容如下:
[repositories]
local
osc: http://maven.oschina.net/content/groups/public/
typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sonatype-oss-releases
maven-central
sonatype-oss-snapshots
这里,问候sbt一千遍之:添加全局镜像库,提到了注意事项:
注意:
要放在`~/.sbt`目录下,而不是`~/.sbt/0.13.x`这样的子目录下
并且问候了 sbt,看来想问候的不止我一个人
这里,加速 SBT 下载依赖库的速度,进一步说了不要配太多
因为, 下载过程中 SBT 会串行的 “询问” 列表中所有的远程仓库, 无论是否找得到.
当依赖树越大, 整个下载的过程就更漫长. 若再遇到响应慢的仓库, 情况恶化的令人发指.
推荐列表策略是:
本地仓库
国内(或内网)镜像仓库
国外官方仓库, 通常 # 注释掉, 待上面不管用时, 去掉 # 再做尝试
另外还有一个兼容的问题
兼容 Ivy 路径布局
大多数中心仓库(repository)是 Maven 的路径布局, 这就导致 SBT 的插件和部分 Ivy 依赖无法从其下载.
=============================================
2016-4-20 10:55:58
配置了私有仓库还是要去下墙外的,看这里,SBT免翻墙手册,SBT连接私有仓库 ,再问候一遍 sbt
Sbt使用笔记,这里有一篇中文文档定义说明
============================
2016-4-20 12:21:52 至于怎么在 scala 打日志的问题,http://stackoverflow.com/questions/978252/logging-in-scala,这里说了用 sfl4j,http://alvinalexander.com/scala/how-to-use-java-style-logging-slf4j-scala,这里说了怎么引入引用,但是引进来发现还是不行,看到这里,http://alvinalexander.com/scala/how-to-log-output-file-grizzled-slf4j-scala-simplelogger.properties,说用 simple log 配置文件,想起来 multi bind 的问题,于是去掉了一个 simple log 的依赖,就可以了
============================
2016-4-20 13:10:28,再来说说 scala 中为啥用 object 不用 class,看这里,http://stackoverflow.com/questions/1755345/difference-between-object-and-class-in-scala,说的算比较好了
Briefly
You can think of the object keyword as creating a singleton object of a class that is defined implicitly.Not So Briefly
Consider the exampleobject A extends B with C {
def f(x: Any): Any = ???
}
From the Scala view, this declarationdeclares an anonymous (inaccessible) class that extends both B and C, and
creates a single instance of this class named A.
This means A can be passed to functions expecting objects of type B or C, and method f can be called using A.f(…).Additional Features of object
There exist some special methods that can be defined for objects:def apply(…) enables the usual method name-less syntax of A(…)
def unapply(…) allows to create custom pattern matching extractors
if accompanying a class of the same name, the object assumes a special role when resolving implicit parameters
其实说白了还是为了函数式的编程模式来考虑的
===================
2016-4-20 17:55:48 当你遇到报错 org.apache.spark#spark-mllib_2.10_2.11;1.3.0: not found 的时候,尝试一下 “org.apache.spark” % “spark-mllib_2.10″ % “1.1.0” 记得先拜拜神
===================================
2016-4-20 18:45:19 经过无数遍像傻逼一样的试错,终于被我试出来一个能用 mllib 的:
scalaVersion := “2.10.6”
“org.apache.spark” %% “spark-core” % “1.5.0”,
“org.apache.spark” % “spark-mllib_2.10″ % “1.3.0”,
再次向 sbt 致以诚挚的问候
==================================
2016-4-20 18:52:23 http://stackoverflow.com/questions/2742719/how-do-i-break-out-of-a-loop-in-scala,scala 中是没有 break 的,spark 是不允许带 return 的,真是阳春白雪,行,你牛逼
===========================
2016-4-20 20:15:15 打的包多了,assembly 的时候就开始抽风,报冲突,看这里,https://github.com/sbt/sbt-assembly,说要我一个个去 extra,有病啊,吃饱撑的?http://stackoverflow.com/questions/25144484/sbt-assembly-deduplication-found-error,看这里,简单粗暴,直接 scope 为 provided