spark scala typesafe config 安全迭代特定列名的值
spark scala typesafe config safe iterate over value of a specific column name
我在 Whosebug 上找到了类似的 post。但是,我无法解决我的问题 所以,这就是我写这个 post.
的原因
瞄准
目的是在加载 SQL table(我使用 SQL 服务器)时执行列投影 [projection = filter columns]。
根据 scala 食谱,这是 [使用数组] 过滤列的方法:
sqlContext.read.jdbc(url,"person",Array("gender='M'"),prop)
但是,我不想在我的 Scala 代码中硬编码 Array("col1", "col2", ...) 这就是为什么我使用类型安全的配置文件(见下文) .
配置文件
dataset {
type = sql
sql{
url = "jdbc://host:port:user:name:password"
tablename = "ClientShampooBusinesLimited"
driver = "driver"
other = "i have a lot of other single string elements in the config file..."
columnList = [
{
colname = "id"
colAlias = "identifient"
}
{
colname = "name"
colAlias = "nom client"
}
{
colname = "age"
colAlias = "âge client"
}
]
}
}
让我们关注 'columnList':SQL 列的名称与 'colname' 完全对应。 'colAlias'是我后面要用到的字段。
data.scala 文件
lazy val columnList = configFromFile.getList("dataset.sql.columnList")
lazy val dbUrl = configFromFile.getList("dataset.sql.url")
lazy val DbTableName= configFromFile.getList("dataset.sql.tablename")
lazy val DriverName= configFromFile.getList("dataset.sql.driver")
configFromFile是我自己在另一个自定义class中创建的。但这不要紧。 columnList 的类型是 "ConfigList" 这个类型来自 typesafe.
主文件
def loadDataSQL(): DataFrame = {
val url = datasetConfig.dbUrl
val dbTablename = datasetConfig.DbTableName
val dbDriver = datasetConfig.DriverName
val columns = // I need help to solve this
/* EDIT 2 march 2017
This code should not be used. Have a look at the accepted answer.
*/
sparkSession.read.format("jdbc").options(
Map("url" -> url,
"dbtable" -> dbTablename,
"predicates" -> columns,
"driver" -> dbDriver))
.load()
}
所以我所有的问题都是提取 'colnames' 值,以便将它们放入 suitable 数组中。谁能帮我写出 'val columns' 的正确操作数?
谢谢
如果您正在寻找一种将 colname
值列表读入 Scala 数组的方法 - 我认为可以这样做:
import scala.collection.JavaConverters._
val columnList = configFromFile.getConfigList("dataset.sql.columnList")
val colNames: Array[String] = columnList.asScala.map(_.getString("colname")).toArray
使用提供的文件,这将导致 Array(id, name, age)
编辑:
至于你的实际目标,我实际上不知道任何名为 predication
的选项(我也无法在源代码中找到证据,使用 Spark 2.0.2)。
JDBC 数据源根据在使用的查询中选择的实际列执行 "projection pushdown"。换句话说 - 只有 selected 列将从数据库中读取,因此您可以在创建 DF 后立即在 select
中使用 colNames
数组,例如:
import org.apache.spark.sql.functions._
sparkSession.read
.format("jdbc")
.options(Map("url" -> url, "dbtable" -> dbTablename, "driver" -> dbDriver))
.load()
.select(colNames.map(col): _*) // selecting only desired columns
我在 Whosebug 上找到了类似的 post。但是,我无法解决我的问题 所以,这就是我写这个 post.
的原因瞄准
目的是在加载 SQL table(我使用 SQL 服务器)时执行列投影 [projection = filter columns]。
根据 scala 食谱,这是 [使用数组] 过滤列的方法:
sqlContext.read.jdbc(url,"person",Array("gender='M'"),prop)
但是,我不想在我的 Scala 代码中硬编码 Array("col1", "col2", ...) 这就是为什么我使用类型安全的配置文件(见下文) .
配置文件
dataset {
type = sql
sql{
url = "jdbc://host:port:user:name:password"
tablename = "ClientShampooBusinesLimited"
driver = "driver"
other = "i have a lot of other single string elements in the config file..."
columnList = [
{
colname = "id"
colAlias = "identifient"
}
{
colname = "name"
colAlias = "nom client"
}
{
colname = "age"
colAlias = "âge client"
}
]
}
}
让我们关注 'columnList':SQL 列的名称与 'colname' 完全对应。 'colAlias'是我后面要用到的字段。
data.scala 文件
lazy val columnList = configFromFile.getList("dataset.sql.columnList")
lazy val dbUrl = configFromFile.getList("dataset.sql.url")
lazy val DbTableName= configFromFile.getList("dataset.sql.tablename")
lazy val DriverName= configFromFile.getList("dataset.sql.driver")
configFromFile是我自己在另一个自定义class中创建的。但这不要紧。 columnList 的类型是 "ConfigList" 这个类型来自 typesafe.
主文件
def loadDataSQL(): DataFrame = {
val url = datasetConfig.dbUrl
val dbTablename = datasetConfig.DbTableName
val dbDriver = datasetConfig.DriverName
val columns = // I need help to solve this
/* EDIT 2 march 2017
This code should not be used. Have a look at the accepted answer.
*/
sparkSession.read.format("jdbc").options(
Map("url" -> url,
"dbtable" -> dbTablename,
"predicates" -> columns,
"driver" -> dbDriver))
.load()
}
所以我所有的问题都是提取 'colnames' 值,以便将它们放入 suitable 数组中。谁能帮我写出 'val columns' 的正确操作数?
谢谢
如果您正在寻找一种将 colname
值列表读入 Scala 数组的方法 - 我认为可以这样做:
import scala.collection.JavaConverters._
val columnList = configFromFile.getConfigList("dataset.sql.columnList")
val colNames: Array[String] = columnList.asScala.map(_.getString("colname")).toArray
使用提供的文件,这将导致 Array(id, name, age)
编辑:
至于你的实际目标,我实际上不知道任何名为 predication
的选项(我也无法在源代码中找到证据,使用 Spark 2.0.2)。
JDBC 数据源根据在使用的查询中选择的实际列执行 "projection pushdown"。换句话说 - 只有 selected 列将从数据库中读取,因此您可以在创建 DF 后立即在 select
中使用 colNames
数组,例如:
import org.apache.spark.sql.functions._
sparkSession.read
.format("jdbc")
.options(Map("url" -> url, "dbtable" -> dbTablename, "driver" -> dbDriver))
.load()
.select(colNames.map(col): _*) // selecting only desired columns