This section describes how to store Cassandra rows in Scala tuples or objects of your own classes.
Instead of mapping your Cassandra rows to objects of the CassandraRow
class, you can directly
unwrap column values into tuples of desired type.
sc.cassandraTable[(String, Int)]("test", "words").select("word", "count").toArray
// Array((bar,20), (foo,10))
sc.cassandraTable[(Int, String)]("test", "words").select("count", "word").toArray
// Array((20,bar), (10,foo))
Define a case class with properties named the same as the Cassandra columns.
For multi-word column identifiers, separate each word by an underscore in Cassandra,
and use the camel case convention on the Scala side. Then provide the explicit class name
when invoking cassandraTable
:
case class WordCount(word: String, count: Int)
sc.cassandraTable[WordCount]("test", "words").toArray
// Array(WordCount(bar,20), WordCount(foo,10))
The column-property naming convention is:
Cassandra column name | Scala property name |
---|---|
count |
count |
column_1 |
column1 |
user_name |
userName |
Using the same property names as columns also works:
Cassandra column name | Scala property name |
---|---|
COUNT |
COUNT |
column_1 |
column_1 |
user_name |
user_name |
The class doesn't necessarily need to be a case class. The only requirements are:
Serializable
Property values might be also set by Scala-style setters. The following class is also compatible:
class WordCount extends Serializable {
var word: String = ""
var count: Int = 0
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。