为了提供有关该问题的更多见解,当我们尝试将String写入Cassandra中的整数列时,Cassandra如何在内部进行类型转换或将此String输入理解为整数?
Cassandra模式:
CREATE TABLE keyspace_name.table_name1 (
col1 text,
col2 int,
col3 text,
col4 text PRIMARY KEY (col1)
DataFrame架构:
root
|-- col1: string (nullable = true)
|-- col2: string (nullable = true)
|-- col3: string (nullable = true)
|-- col4: string (nullable = true)
答案 0 :(得分:0)
这是通过为不同的CQL类型注册类型转换器来完成的。例如,在PrimitiveColumnType.scala中为int
类型定义了以下代码:
case object IntType extends PrimitiveColumnType[Int] {
def scalaTypeTag = implicitly[TypeTag[Int]]
def cqlTypeName = "int"
def converterToCassandra =
new TypeConverter.OptionToNullConverter(TypeConverter.forType[java.lang.Integer])
}
此代码使用通用的convert
implementation,从而减轻了将对特定于类型的部分功能convertPF
的特定实现的实际转换。对于能够从Number
或String
转换为整数的IntConverter:
def convertPF = {
case x: Number => x.intValue
case x: String => x.toInt
}
您可以寻找其他实现,例如,CQL date
可以从不同的类型获得-字符串,long,UUID,...