Last active
January 21, 2016 16:04
-
-
Save olim7t/84eda9f24c85b6d04ba0 to your computer and use it in GitHub Desktop.
Custom codec to handle CQL `date` columns as Java ints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.datastax.driver.core.*; | |
import com.datastax.driver.core.exceptions.InvalidTypeException; | |
import java.nio.ByteBuffer; | |
public class LongToDateDemo { | |
public static class LongToDateCodec extends TypeCodec<Integer> { | |
// Piggyback on the default codecs' implementation | |
private PrimitiveIntCodec intCodec = TypeCodec.cint(); | |
private TypeCodec<LocalDate> dateCodec = TypeCodec.date(); | |
public LongToDateCodec() { | |
super(DataType.date(), Integer.class); | |
} | |
@Override | |
public ByteBuffer serialize(Integer daysSinceEpoch, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
if (daysSinceEpoch == null) | |
return null; | |
int unsigned = CodecUtils.fromSignedToUnsignedInt(daysSinceEpoch); | |
return intCodec.serializeNoBoxing(unsigned, protocolVersion); | |
} | |
@Override | |
public Integer deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
if (bytes == null || bytes.remaining() == 0) | |
return null; | |
int unsigned = intCodec.deserializeNoBoxing(bytes, protocolVersion); | |
return CodecUtils.fromUnsignedToSignedInt(unsigned); | |
} | |
@Override | |
public Integer parse(String value) throws InvalidTypeException { | |
return dateCodec.parse(value).getDaysSinceEpoch(); | |
} | |
@Override | |
public String format(Integer value) throws InvalidTypeException { | |
return dateCodec.format(LocalDate.fromDaysSinceEpoch(value)); | |
} | |
} | |
public static void main(String[] args) { | |
Cluster cluster = null; | |
try { | |
CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; | |
// Or `new CodecRegistry()` if you're going to create other Cluster instances in the same VM that must not | |
// have the codec | |
codecRegistry.register(new LongToDateCodec()); | |
cluster = Cluster.builder() | |
.addContactPoint("127.0.0.1") | |
.withCodecRegistry(codecRegistry) | |
.build(); | |
Session session = cluster.connect(); | |
session.execute("CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}"); | |
session.execute("CREATE TABLE IF NOT EXISTS test.foo(d date PRIMARY KEY)"); | |
// set: | |
PreparedStatement pst = session.prepare("INSERT INTO test.foo (d) VALUES (?)"); | |
session.execute(pst.bind(1)); | |
// Warning -- a simple statement like this won't work: | |
// session.execute("INSERT INTO test.foo (d) VALUES (?)", 1); | |
// The driver can't infer the CQL type, so it will serialize 1 as a CQL int, which is signed | |
// retrieve: | |
Row row = session.execute("SELECT * FROM test.foo").one(); | |
System.out.println(row.getInt("d")); | |
} finally { | |
if (cluster != null) cluster.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment