【发布时间】:2015-03-18 14:54:05
【问题描述】:
在 Spark SQL 中,我们有 Row 对象,其中包含组成一行的记录列表(想想 Seq[Any])。 Row 具有序号访问器,例如 .getInt(0) 或 getString(2)。
说序号 0 = ID,序号 1 = 名称。很难记住序数是什么,使代码混乱。
比如说我有以下代码
def doStuff(row: Row) = {
//extract some items from the row into a tuple;
(row.getInt(0), row.getString(1)) //tuple of ID, Name
}
问题变成了如何为 Row 对象中的这些字段创建别名?
我在想我可以创建采用隐式 Row 对象的方法;
def id(implicit row: Row) = row.getInt(0)
def name(implicit row: Row) = row.getString(1)
然后我可以将上面的内容重写为;
def doStuff(implicit row: Row) = {
//extract some items from the row into a tuple;
(id, name) //tuple of ID, Name
}
有更好/更整洁的方法吗?
【问题讨论】:
标签: scala functional-programming apache-spark implicit