将自定义编解码器添加到CassandraConnector

时间:2016-11-01 15:48:54

标签: scala apache-spark cassandra spark-streaming spark-cassandra-connector

有没有办法在实例化CassandraConnector上注册自定义编解码器?

我每次拨打cassandraConnector.withSessionDo

时都会注册我的编解码器
val cassandraConnector = CassandraConnector(ssc.sparkContext.getConf)
...
...
.mapPartitions(partition => {
  cassandraConnector.withSessionDo(session => {
    // register custom codecs once for each partition so it isn't loaded as often for each data point
    if (partition.nonEmpty) {
      session.getCluster.getConfiguration.getCodecRegistry
        .register(new TimestampLongCodec)
        .register(new SummaryStatsBlobCodec)
        .register(new JavaHistogramBlobCodec)
    }

这样做似乎有点像反模式。它也真的堵塞了我们的日志,因为我们有一个每30秒运行一次的火花流服务,并且它用以下内容填充我们的日志:

16/11/01 14:14:44 WARN CodecRegistry: Ignoring codec SummaryStatsBlobCodec [blob <-> SummaryStats] because it collides with previously registered codec SummaryStatsBlobCodec [blob <-> SummaryStats]
16/11/01 14:14:44 WARN CodecRegistry: Ignoring codec JavaHistogramBlobCodec [blob <-> Histogram] because it collides with previously registered codec JavaHistogramBlobCodec [blob <-> Histogram]
16/11/01 14:14:44 WARN CodecRegistry: Ignoring codec TimestampLongCodec [timestamp <-> java.lang.Long] because it collides with previously registered codec TimestampLongCodec [timestamp <-> java.lang.Long]

编辑:

我已经尝试过这样立即注册:

val cassandraConnector = CassandraConnector(ssc.sparkContext.getConf)
cassandraConnector.withClusterDo(cluster => {
  cluster.getConfiguration.getCodecRegistry
    .register(new TimestampLongCodec)
    .register(new SummaryStatsBlobCodec)
    .register(new JavaHistogramBlobCodec)
})

这^在本地工作但是当部署到我们的mesos集群时,它无法找到编解码器。我假设它是因为它只在驱动程序中本地注册它们,并且从不将它们添加到执行程序版本中。

1 个答案:

答案 0 :(得分:6)

更好的方法是覆盖cassandra连接工厂,就像这样

import com.datastax.driver.core.Cluster
import com.datastax.spark.connector.cql.{CassandraConnectionFactory, CassandraConnectorConf, DefaultConnectionFactory}
object MyConnectionFactory extends CassandraConnectionFactory {
  override def createCluster(conf: CassandraConnectorConf): Cluster = {
    val cluster = DefaultConnectionFactory.createCluster(conf)
    cluster.getConfiguration.getCodecRegistry
      .register(new TimestampLongCodec)
      .register(new SummaryStatsBlobCodec)
      .register(new JavaHistogramBlobCodec)
    cluster
  }
}

并将spark.cassandra.connection.factory参数设置为指向类