SparkのRow

7456 ワード

hklの曰わく、公式のAPIドキュメントを直接手に入れて、英語が読めなくても大丈夫で、その実例を見ればいい.
http://spark.apache.org/docs/1.3.1/api/scala/index.html#org.apache.spark.sql.Row
注意:import org.apache.spark.sql. まずこのパッケージをインポートします.
Represents one row of output from a relational operator. Allows both generic access by ordinal, which will incur boxing overhead for primitives, as well as native primitive access.
It is invalid to use the native primitive interface to retrieve a value that is null, instead a user must check  isNullAt  before attempting to retrieve a value that might be null.
To create a new Row, use RowFactory.create() in Java or Row.apply() in Scala.
A Row object can be constructed by providing field values. Example:
import org.apache.spark.sql._

// Create a Row from values.
Row(value1, value2, value3, ...)
// Create a Row from a Seq of values.
Row.fromSeq(Seq(value1, value2, ...))

A value of a row can be accessed through both generic access by ordinal, which will incur boxing overhead for primitives, as well as native primitive access. An example of generic access by ordinal:
import org.apache.spark.sql._

val row = Row(1, true, "a string", null)
// row: Row = [1,true,a string,null]
val firstValue = row(0)
// firstValue: Any = 1
val fourthValue = row(3)
// fourthValue: Any = null

For native primitive access, it is invalid to use the native primitive interface to retrieve a value that is null, instead a user must check  isNullAt  before attempting to retrieve a value that might be null. An example of native primitive access:
// using the row from the previous example.
val firstValue = row.getInt(0)
// firstValue: Int = 1
val isNull = row.isNullAt(3)
// isNull: Boolean = true

In Scala, fields in a Row object can be extracted in a pattern match. Example:
import org.apache.spark.sql._

val pairs = sql("SELECT key, value FROM src").rdd.map {
  case Row(key: Int, value: String) =>
    key -> value
}

本当に読みたいなら、そのまま翻訳プラグインを使いましょう.
これはすべてその具体的なAPIです.
もう一つ注意しなければならないのは彼のAPIです     isNullAt()このインデックスの1番目の要素は 0  .
Spark 之 Row_第1张图片
sizeメソッドはrow内の要素の個数を表示します.
rowのapiは豊富で、自分が必要なら文章の上部のリンクを見てみましょう.