Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CQL3 Collections #10

Open
abh1nav opened this issue Apr 28, 2014 · 4 comments
Open

Support for CQL3 Collections #10

abh1nav opened this issue Apr 28, 2014 · 4 comments

Comments

@abh1nav
Copy link

abh1nav commented Apr 28, 2014

Hi Rohit,

Is there any documentation regarding support for CQL3 collections?
Specifically, I'm trying to write a transformer for a table that looks like:

CREATE TABLE users (
  uid text,
  lastupdate text,
  properties map<text,text>,
  PRIMARY KEY (uid)
);
case class User(uid: String, lastupdate: String, 
  properties: Map[String, String])
implicit def cql3ToUser(k: Map[String, ByteBuffer], v: Map[String, ByteBuffer]): User = 
  User(k("uid"), v("lastupdate"), v("properties"))

It seems there's no implicit converter inside RichByteBuffer for ByteBuffer to Map[String, String].

Thanks,
--Abhinav

@ra2085
Copy link

ra2085 commented Apr 28, 2014

You can have a look at the org.apache.cassandra.db.marshal package, it has a collection type. Haven't used the Collection type specifically, but I've already used the Decimal datatype successfully. I've already opened an issue to support implicits on those datatypes few weeks ago, you can easily use the example I posted with a Collection type.

Open issue cassandra 2.0 datatypes: #8

Regards!

@abh1nav
Copy link
Author

abh1nav commented Apr 28, 2014

@ra2085 Thanks! This ended up working flawlessly:

import org.apache.cassandra.db.marshal.{UTF8Type, MapType}

implicit def ByteBuffer2MapSS(b: ByteBuffer): Map[String,String] =
    MapType.getInstance(UTF8Type.instance, UTF8Type.instance).compose(b).toMap

@milliondreams
Copy link
Member

We should get the support for this in the next release RichByteBuffer

Since we will be support CQL Auto Paging in next release and hence having DS driver as dependency, I was thinking of rewriting the RichByteBuffer using DataType.

For this case it will look like -

import com.datastax.driver.core.DataType

import scala.collection.JavaConverters._

implicit def MapSS2ByteBuffer(map: Map[String, String]): ByteBuffer = DataType.map(DataType.text(), DataType.text()).serialize(map.asJava)

implicit def ByteBuffer2MapSS(buffer: ByteBuffer): Map[String, String] = DataType.map(DataType.text(), DataType.text()).deserialize(buffer).asInstanceOf[java.util.Map[String, String]].asScala.toMap

@abh1nav
Copy link
Author

abh1nav commented Apr 28, 2014

That looks much cleaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants