Last active
March 10, 2018 18:31
-
-
Save alexott/6e6fd8b0eca717f9f10984c9de75123b to your computer and use it in GitHub Desktop.
UDT + Object mapper in DataStax Java Driver
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 com.datastax.demos.alexott.product; | |
import com.datastax.driver.core.Cluster; | |
import com.datastax.driver.core.CodecRegistry; | |
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) { | |
CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; | |
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").withCodecRegistry(codecRegistry).build(); | |
Session session = cluster.connect(); | |
MappingManager manager = new MappingManager(session); | |
Mapper<Product> mapper = manager.mapper(Product.class); | |
Product product = mapper.get("test"); | |
System.out.println("Product: " + product); | |
session.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
package com.datastax.demos.alexott.product; | |
import com.datastax.driver.mapping.annotations.UDT; | |
@UDT(keyspace = "test", name = "information") | |
public class Information { | |
String info1; | |
String info2; | |
public String getInfo1() { | |
return info1; | |
} | |
public void setInfo1(String info1) { | |
this.info1 = info1; | |
} | |
public String getInfo2() { | |
return info2; | |
} | |
public void setInfo2(String info2) { | |
this.info2 = info2; | |
} | |
@Override | |
public String toString() { | |
return "Information [info1=" + info1 + ", info2=" + info2 + "]"; | |
} | |
} |
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
Product: Product [id=test, details={k1=Information [info1=v-info1, info2=v-info2]}, moreDetails=[Information [info1=l-info1, info2=linfo2]]] |
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 com.datastax.demos.alexott.product; | |
import java.util.List; | |
import java.util.Map; | |
import com.datastax.driver.mapping.annotations.Frozen; | |
import com.datastax.driver.mapping.annotations.FrozenValue; | |
import com.datastax.driver.mapping.annotations.PartitionKey; | |
import com.datastax.driver.mapping.annotations.Table; | |
@Table(keyspace = "test", name = "product") | |
public class Product { | |
@PartitionKey | |
String id; | |
@FrozenValue | |
@Frozen | |
Map<String, Information> details; | |
@Frozen | |
List<Information> moreDetails; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public Map<String, Information> getDetails() { | |
return details; | |
} | |
public void setDetails(Map<String, Information> details) { | |
this.details = details; | |
} | |
public List<Information> getMoreDetails() { | |
return moreDetails; | |
} | |
public void setMoreDetails(List<Information> moreDetails) { | |
this.moreDetails = moreDetails; | |
} | |
@Override | |
public String toString() { | |
return "Product [id=" + id + ", details=" + details + ", moreDetails=" + moreDetails + "]"; | |
} | |
} |
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 keyspace if not exists test with replication = {'class': 'SimpleStrategy', 'replication_factor': 1}; | |
use test; | |
create type information ( | |
info1 text, | |
info2 text | |
); | |
create table product ( | |
id text primary key, | |
details frozen<map<text, frozen<information>>>, | |
moreDetails frozen<list<information>> | |
); | |
insert into product(id, details, moreDetails) values('test', {'k1':{info1:'v-info1',info2:'v-info2'}},[{info1:'l-info1',info2:'linfo2'}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment