Created
January 10, 2018 09:29
-
-
Save alexott/f9b91d81a21c97199e65c79451c26cd7 to your computer and use it in GitHub Desktop.
java.util.LocalDate mapping in Cassandra
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
package jdtest1; | |
import java.util.UUID; | |
import com.datastax.driver.core.Cluster; | |
import com.datastax.driver.core.Session; | |
import com.datastax.driver.mapping.Mapper; | |
import com.datastax.driver.mapping.MappingManager; | |
public class App { | |
public static void main(String[] args) { | |
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build(); | |
Session session = cluster.connect(); | |
MappingManager manager = new MappingManager(session); | |
Mapper<TestData> mapper = manager.mapper(TestData.class); | |
UUID uuid = UUID.fromString("e7ae5cf3-d358-4d99-b900-85902fda9bb1"); | |
TestData td = mapper.get(uuid); | |
if (td == null) { | |
System.out.println("Can't find given UUID"); | |
} else { | |
System.out.println("UUID: " + td.getId().toString() + ", date: " + td.getDdate().toString()); | |
} | |
session.close(); | |
cluster.close(); | |
} | |
} |
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
create table test.dtest (id uuid primary key, ddate date); | |
insert into test.dtest (id, ddate) values (e7ae5cf3-d358-4d99-b900-85902fda9bb0, '2010-06-30'); | |
insert into test.dtest (id, ddate) values (e7ae5cf3-d358-4d99-b900-85902fda9bb1, '2010-06-29'); | |
insert into test.dtest (id, ddate) values (e7ae5cf3-d358-4d99-b900-85902fda9bb2, '2010-06-28'); |
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
>mvn exec:java -Dexec.mainClass="com.datastax.alexott.demos.jdtest1.App" 10:25 130 | |
[INFO] Scanning for projects... | |
[INFO] | |
[INFO] ------------------------------------------------------------------------ | |
[INFO] Building jdtest1 0.0.1-SNAPSHOT | |
[INFO] ------------------------------------------------------------------------ | |
[INFO] | |
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ jdtest1 >>> | |
[INFO] | |
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ jdtest1 <<< | |
[INFO] | |
[INFO] | |
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ jdtest1 --- | |
UUID: e7ae5cf3-d358-4d99-b900-85902fda9bb1, date: 2010-06-29 | |
[INFO] ------------------------------------------------------------------------ | |
[INFO] BUILD SUCCESS | |
[INFO] ------------------------------------------------------------------------ | |
[INFO] Total time: 4.404 s | |
[INFO] Finished at: 2018-01-10T10:25:16+01:00 | |
[INFO] Final Memory: 15M/272M | |
[INFO] ------------------------------------------------------------------------ |
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
package jdtest1; | |
import java.time.LocalDate; | |
import java.util.UUID; | |
import com.datastax.driver.extras.codecs.jdk8.LocalDateCodec; | |
import com.datastax.driver.mapping.annotations.Column; | |
import com.datastax.driver.mapping.annotations.PartitionKey; | |
import com.datastax.driver.mapping.annotations.Table; | |
@Table(keyspace = "test", name = "dtest", readConsistency = "ONE", writeConsistency = "ONE") | |
public class TestData { | |
@PartitionKey | |
@Column(name = "id") | |
private UUID id; | |
@Column(name = "ddate", codec = LocalDateCodec.class) | |
LocalDate ddate; | |
public UUID getId() { | |
return id; | |
} | |
public void setId(UUID id) { | |
this.id = id; | |
} | |
public LocalDate getDdate() { | |
return ddate; | |
} | |
public void setDdate(LocalDate ddate) { | |
this.ddate = ddate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment