0

I am trying to read a file containing names and insert the name along with timestamp data to cassandra table using Spark and Scala. Below is my code

case class Names(name:String, auditDate:DateTime )

def main(args: Array[String]): Unit = {
    System.setProperty("hadoop.home.dir", "D:\\backup\\lib\\winutils");
    val conf = new SparkConf()
      .set("spark.cassandra.connection.host", "172.16.109.202")
      //.set("spark.cassandra.connection.host", "192.168.1.17")
      .setAppName("CassandraLoader")
      .setMaster("local")
    var context = new SparkContext(conf)

    var namesFile = context.textFile("src/main/resources/names.txt")

    namesFile.map(x=>Names(x,DateTime.now()))
      .saveToCassandra("practice","names",SomeColumns("name", "insert_date"))

  }

The cassandra table details is in below

CREATE TABLE practice.names (
    name text PRIMARY KEY,
    insert_date timestamp
)

When I try to execute the code, I am getting the below error

Exception in thread "main" java.lang.IllegalArgumentException: requirement failed: Columns not found in com.sample.practice.Names: [insert_date]
    at scala.Predef$.require(Predef.scala:233)
    at com.datastax.spark.connector.mapper.DefaultColumnMapper.columnMapForWriting(DefaultColumnMapper.scala:108)
    at com.datastax.spark.connector.writer.MappedToGettableDataConverter$$anon$1.<init>(MappedToGettableDataConverter.scala:29)
    at com.datastax.spark.connector.writer.MappedToGettableDataConverter$.apply(MappedToGettableDataConverter.scala:20)
    at com.datastax.spark.connector.writer.DefaultRowWriter.<init>(DefaultRowWriter.scala:17)
    at com.datastax.spark.connector.writer.DefaultRowWriter$$anon$1.rowWriter(DefaultRowWriter.scala:31)
    at com.datastax.spark.connector.writer.DefaultRowWriter$$anon$1.rowWriter(DefaultRowWriter.scala:29)
    at com.datastax.spark.connector.writer.TableWriter$.apply(TableWriter.scala:271)
    at com.datastax.spark.connector.RDDFunctions.saveToCassandra(RDDFunctions.scala:36)
    at com.sample.practice.CqlInsertDate$.main(CqlInsertDate.scala:30)
    at com.sample.practice.CqlInsertDate.main(CqlInsertDate.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

When I try to print the RDD instead of saving to cassandra, I am getting the below output

Names(Frank,2017-01-30T14:03:16.911+05:30)
Names(Jean,2017-01-30T14:03:17.115+05:30)
Names(Joe,2017-01-30T14:03:17.116+05:30)

Below is my SBT file details

version := "1.0"

scalaVersion := "2.10.6"

libraryDependencies += "com.datastax.spark" % "spark-cassandra-connector_2.10" % "2.0.0-M3"

libraryDependencies += "org.apache.spark" % "spark-core_2.10" % "2.0.2"

libraryDependencies += "org.apache.spark" % "spark-sql_2.10" % "2.0.2"

libraryDependencies += "org.apache.spark" % "spark-hive_2.10" % "2.0.2"

I am using Cassandra 2.1. Please help. Thanks in advance.

0

1 Answer 1

2

Try to change your class field to insert_date or vice versa table column to auditDate

Sign up to request clarification or add additional context in comments.

1 Comment

@FaigB , what is the logic here ? i am facing the same kind of the problem, what to do ?what is the trick here ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.