有没有更好的方法将ResultSet从SQL查询转换为Scala中的Array/List?
下面显示的天真的方式不是Scala.我想知道如何改进它.
val rs = pstmt.executeQuery()
var nids = List[String]()
while (rs.next()) {
nids = nids :+ rs.getString(1)
}
rs.close()
Run Code Online (Sandbox Code Playgroud) Kryo通过高效的序列化方法帮助提高Spark应用程序的性能.
我想知道,如果Kryo会在SparkSQL的情况下提供帮助,我应该如何使用它.
在SparkSQL应用程序中,我们会做很多基于列的操作df.select($"c1", $"c2"),并且DataFrame Row的模式不是很静态.
不确定如何为用例注册一个或多个序列化程序类.
例如:
case class Info(name: String, address: String)
...
val df = spark.sparkContext.textFile(args(0))
.map(_.split(','))
.filter(_.length >= 2)
.map {e => Info(e(0), e(1))}
.toDF
df.select($"name") ... // followed by subsequent analysis
df.select($"address") ... // followed by subsequent analysis
Run Code Online (Sandbox Code Playgroud)
我认为为每个案例定义案例类并不是一个好主意select.
或者它帮助,如果我注册Info像registerKryoClasses(Array(classOf[Info]))
我有一个隐含的方法:
def f(x: String)(implicit dispatcher: ExecutionContextExecutor, mat: ActorMaterializer) = ???
Run Code Online (Sandbox Code Playgroud)
我想创建一个辅助方法,如:
def g1(y: String) = f("uri1" + y)
def g2(y: String) = f("uri2" + y)
Run Code Online (Sandbox Code Playgroud)
当然,no implicits found for parameter ex: ExecutionContext对于方法,这不能编译g。
我不想implicits在g.
那么,这个案例的惯用解决方案是什么?
我想在完成后进行一些清理(如关闭数据库连接)Future。
目前我是这样实现的:
Future { ... } onComplete {
case Success(v) =>
// ...
conn.close()
case Failure(ex) =>
// ...
conn.close()
}
Run Code Online (Sandbox Code Playgroud)
有重复的代码,也很乏味。
对此有什么最佳实践吗?
我是 SBT 新手,我喜欢在 SBT 控制台中调试 scala 源代码。但控制台总是像<console>:65: warning: Unused import每个命令行一样重复警告。真烦人。
如何在 SBT 控制台中禁用此无用警告,同时在 sbt 编译中保留此检查?
我想我应该添加build.sbt类似的内容scalacOptions in console := ???,但不确定它到底应该是什么。
[更新]
我还没有完成关于 sbt 的教程,所以我build.sbt从一些示例项目中复制了。我添加了scalacOptions in (Compile, console) ...,但是还是不行。
scalacOptions in Compile ++= Seq("-deprecation", "-feature", "-unchecked", "-Xlog-reflective-calls", "-Xlint"),
scalacOptions in (Compile, console) ~= { _.filterNot(Set("-Ywarn-unused-import", "-Ywarn-unused:imports")) },
javacOptions in Compile ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"),
javaOptions in run ++= Seq("-Xms256m", "-Xmx2048m", "-Djava.library.path=./target/native"),
libraryDependencies ++= Seq(
Run Code Online (Sandbox Code Playgroud) 我有一些返回Futures 的函数。调用者通过 注册回调onComplete。
def makeHttpRequest(): Future[T] = ???
makeHttpRequest().onComplete {
case Success(v) => ???
case Failure(ex) => ???
}
Run Code Online (Sandbox Code Playgroud)
现在我想对这些函数(或函数调用)启用重试。有没有关于如何实现这一目标的建议?