row.schema 的架构 returns 为空的行
Row with schema returns null for row.schema
我知道您会期望为此编写代码,但代码看起来不错,但我无法理解哪里出了问题。我会解释这个问题,让我们看看你是否能告诉我我是否遗漏了一些明显的东西。
我为 Row
编写了一个隐式 class,一个使用 row.schema
获取模式的函数。有趣的是 row.schema
总是 returns null
在隐含的 class 行对象是否有模式,而相同的 returns 实际模式,在包含单元测试的 class 中(我使用 FlatSpec with Matchers
进行单元测试)。这就是单元测试中行的构造方式。
val schema = StructType(
StructField("col1", StringType) ::
StructField("col2", DoubleType) ::
StructField("col3", IntegerType) ::
Nil)
val values = Array("value1", 1.0, 1)
val row: Row = new GenericRowWithSchema(values, schema)
我检查了 Spark 代码库中的源代码,这就是 schema
函数的实现方式。
def schema: StructType = null
这让我更加困惑。有什么指点吗?
编辑: 我早该知道没有任何代码没有真正帮助。所以这是代码。检查 getAsOpt[T](i)
(here) and getAsOpt[T](fieldName)
(here) functions in this code for Row
and the corresponding tests here.
下面是那些失败的,失败的,
[info] - getAsOpt[T]() can get values using field names. *** FAILED *** (8 milliseconds)
[info] None was not equal to Some("value1") (RowTest.scala:85)
...
[info] - getAsOpt[T] retrieves an Optional value if the fieldName exists else returns None *** FAILED *** (1 millisecond)
[info] None was not equal to Some("value1") (RowTest.scala:91)
...
[info] - getAsOpt[T] retrieves an Optional value if class cast is successful else returns None *** FAILED *** (0 milliseconds)
[info] None was not equal to Some("value1") (RowTest.scala:97)
我不太确定这里的问题到底是什么,所以我将只解决第二部分。 schema
在GenericRowWithSchema
中定义is overridden如下:
class GenericRowWithSchema(values: Array[Any], override val schema: StructType)
为了说明正在发生的事情,请考虑以下示例
trait Foobar {
def foo: Integer = null
}
class Foo(override val foo: Integer) extends Foobar
class Bar extends Foobar
new Foo(1).foo != null
// Boolean = true
new Bar().foo == null
// Boolean = true
我知道您会期望为此编写代码,但代码看起来不错,但我无法理解哪里出了问题。我会解释这个问题,让我们看看你是否能告诉我我是否遗漏了一些明显的东西。
我为 Row
编写了一个隐式 class,一个使用 row.schema
获取模式的函数。有趣的是 row.schema
总是 returns null
在隐含的 class 行对象是否有模式,而相同的 returns 实际模式,在包含单元测试的 class 中(我使用 FlatSpec with Matchers
进行单元测试)。这就是单元测试中行的构造方式。
val schema = StructType(
StructField("col1", StringType) ::
StructField("col2", DoubleType) ::
StructField("col3", IntegerType) ::
Nil)
val values = Array("value1", 1.0, 1)
val row: Row = new GenericRowWithSchema(values, schema)
我检查了 Spark 代码库中的源代码,这就是 schema
函数的实现方式。
def schema: StructType = null
这让我更加困惑。有什么指点吗?
编辑: 我早该知道没有任何代码没有真正帮助。所以这是代码。检查 getAsOpt[T](i)
(here) and getAsOpt[T](fieldName)
(here) functions in this code for Row
and the corresponding tests here.
下面是那些失败的,失败的,
[info] - getAsOpt[T]() can get values using field names. *** FAILED *** (8 milliseconds)
[info] None was not equal to Some("value1") (RowTest.scala:85)
...
[info] - getAsOpt[T] retrieves an Optional value if the fieldName exists else returns None *** FAILED *** (1 millisecond)
[info] None was not equal to Some("value1") (RowTest.scala:91)
...
[info] - getAsOpt[T] retrieves an Optional value if class cast is successful else returns None *** FAILED *** (0 milliseconds)
[info] None was not equal to Some("value1") (RowTest.scala:97)
我不太确定这里的问题到底是什么,所以我将只解决第二部分。 schema
在GenericRowWithSchema
中定义is overridden如下:
class GenericRowWithSchema(values: Array[Any], override val schema: StructType)
为了说明正在发生的事情,请考虑以下示例
trait Foobar {
def foo: Integer = null
}
class Foo(override val foo: Integer) extends Foobar
class Bar extends Foobar
new Foo(1).foo != null
// Boolean = true
new Bar().foo == null
// Boolean = true