0
votes

I am adding new features to an open source project(pillar) to migrate Cassandra tables. I have a problem in operation that insert values a new table. There is a table in Cassandra:

create table customer(
  name text,
  age int,
  point int,
  primary key(name, age)
)

I want to migrate from this table to test_person table.

create table test_person (
  name text,
  surname text,
  point int,
  city text,
  primary key(name)
)

Here is an operation:



var s: PreparedStatement = session.prepare("insert into test_person  (name, age, point) values (?, ?, ?)"); 

var r: Row = session.execute("select * from customer").one()

var arr: Array[AnyRef] = new Array[AnyRef](3)

arr(0) = row.getObject("name")

arr(1) = row.getObject("age")

arr(2) = row.getObject("point")



session.execute(s.bind(arr))


This is error message: Type mismatch Can't assign primitive value to object.

I got as object and assign an array typed of AnyRef. What is wrong?

How can I handle this

2
Which line throws error? - Samar
arr(1) = row.getObject("age") arr(2) = row.getObject("point") - Mustafa
does row.getObject("age") return an object or an int? - Samar
Class name is java.lang.Integer - Mustafa

2 Answers

0
votes

This is happening because there is an implicit conversion happening from java.lang.Integer to Int. And Int is of type AnyVal not an AnyRef. Try using Array[Any] instead of Array[AnyRef] OR You can disable the implicit conversion by import scala.Predef.{Integer2int => _}

// This method in Predef.scala is causing the conversion
implicit def Integer2int(x: java.lang.Integer): Int
0
votes

That is because, AnyRef is for objects and AnyVal is for primitives. You can use an Array[Any] in your case:

var s: PreparedStatement = session.prepare("insert into test_person  (name, age, point) values (?, ?, ?)");
var r: Row = session.execute("select * from customer").one()
val arr = Array(r.getString("name"), r.getInt("age"), r.getInt("point"))