spark sql在Cassandra表之间传输数据

时间:2018-08-20 10:57:21

标签: apache-spark cassandra apache-spark-sql

请在下面找到Cassandra表。

我试图将数据从1个Cassandra表复制到另一个具有相同结构的Cassandra表中。

请帮助我。

CREATE TABLE data2 (
        d_no text,
        d_type text,
        sn_perc int,
        tse_dt timestamp,
        f_lvl text,
        ign_f boolean,
        lk_loc text,
        lk_ts timestamp,
        mi_rem text,
        nr_fst text,
        perm_stat text,
        rec_crt_dt timestamp,
        sr_stat text,
        sor_query text,
        tp_dat text,
        tp_ts timestamp,
        tr_rem text,
        tr_type text,
        PRIMARY KEY (device_serial_no, device_type)
    ) WITH CLUSTERING ORDER BY (device_type ASC)

使用以下数据插入数据

Insert into data2(all column names) values('64FCFCFC','HUM',4,'1970-01-02 05:30:00’ ,’NA’,true,'NA','1970-01-02 05:40:00',’NA’,'NA','NA','1970-02-01 05:30:00','NA','NA','NA','1970-02-03 05:30:00','NA','NA');

注意: 当我尝试像'1970-01-02 05:30:00'这样插入的第4列时间戳,并且在dtaframe中也正确插入了时间戳,但是当从dataframe插入cassandra并使用select * from table时,我看到它是像1970-01-02 00:00:00.000000 + 0000这样插入

类似地,所有时间戳列都在发生。

pom.xml

<dependencies>
       <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.11</artifactId>
    <version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.11</artifactId>
    <version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.datastax.spark/spark-cassandra-connector -->
<dependency>
    <groupId>com.datastax.spark</groupId>
    <artifactId>spark-cassandra-connector_2.11</artifactId>
    <version>2.3.1</version>
</dependency>

我想读取这些值,并使用spark Scala将其写入另一个Cassandra表中。参见下面的代码:

val df2 = spark.read
                       .format("org.apache.spark.sql.cassandra")
                       .option("spark.cassandra.connection.host","hostname")
                       .option("spark.cassandra.connection.port","9042")
                       .option( "spark.cassandra.auth.username","usr")
                       .option("spark.cassandra.auth.password","pas")
                       .option("keyspace","hr")
                       .option("table","data2")
                       .load()
Val df3 =doing some processing on df2.
df3.write
         .format("org.apache.spark.sql.cassandra")
         .mode("append")
         .option("spark.cassandra.connection.host","hostname")
         .option("spark.cassandra.connection.port","9042")
         .option( "spark.cassandra.auth.username","usr")
         .option("spark.cassandra.auth.password","pas")
         .option("spark.cassandra.output.ignoreNulls","true")
         .option("confirm.truncate","true")
         .option("keyspace","hr")
         .option("table","data3")
         .save()

但是当我尝试使用上面的代码插入数据时,我遇到了错误

java.lang.IllegalArgumentException: requirement failed: Invalid row size: 18 instead of 17.
    at scala.Predef$.require(Predef.scala:224)
    at com.datastax.spark.connector.writer.SqlRowWriter.readColumnValues(SqlRowWriter.scala:23)
    at com.datastax.spark.connector.writer.SqlRowWriter.readColumnValues(SqlRowWriter.scala:12)
    at com.datastax.spark.connector.writer.BoundStatementBuilder.bind(BoundStatementBuilder.scala:99)
    at com.datastax.spark.connector.writer.GroupingBatchBuilder.next(GroupingBatchBuilder.scala:106)
    at com.datastax.spark.connector.writer.GroupingBatchBuilder.next(GroupingBatchBuilder.scala:31)
    at scala.collection.Iterator$class.foreach(Iterator.scala:891)
    at com.datastax.spark.connector.writer.GroupingBatchBuilder.foreach(GroupingBatchBuilder.scala:31)
    at com.datastax.spark.connector.writer.TableWriter$$anonfun$writeInternal$1.apply(TableWriter.scala:233)
    at com.datastax.spark.connector.writer.TableWriter$$anonfun$writeInternal$1.apply(TableWriter.scala:210)
    at com.datastax.spark.connector.cql.CassandraConnector$$anonfun$withSessionDo$1.apply(CassandraConnector.scala:112)
    at com.datastax.spark.connector.cql.CassandraConnector$$anonfun$withSessionDo$1.apply(CassandraConnector.scala:111)
    at com.datastax.spark.connector.cql.CassandraConnector.closeResourceAfterUse(CassandraConnector.scala:145)
    at com.datastax.spark.connector.cql.CassandraConnector.withSessionDo(CassandraConnector.scala:111)
    at com.datastax.spark.connector.writer.TableWriter.writeInternal(TableWriter.scala:210)
    at com.datastax.spark.connector.writer.TableWriter.insert(TableWriter.scala:197)
    at com.datastax.spark.connector.writer.TableWriter.write(TableWriter.scala:183)
    at com.datastax.spark.connector.RDDFunctions$$anonfun$saveToCassandra$1.apply(RDDFunctions.scala:36)
    at com.datastax.spark.connector.RDDFunctions$$anonfun$saveToCassandra$1.apply(RDDFunctions.scala:36)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:109)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:345)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:1)

这是一个已知问题(link)-您正在将数据从启用了DSE搜索的表复制到没有该表的表中。您只需要在转换中删除此列即可:

val df3 = df2.drop("solr_query").... // your transformations

或者您可以简单地使用更新的驱动程序(如果使用的是OSS驱动程序,则为2.3.1)或包含此修复程序的相应DSE版本。