Created
November 13, 2015 11:45
-
-
Save olim7t/57e76a2ce91d011734a0 to your computer and use it in GitHub Desktop.
Standalone program that can be used as a template to reproduce Java driver issues
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.Cluster; | |
import com.datastax.driver.core.Session; | |
import com.datastax.driver.mapping.Mapper; | |
import com.datastax.driver.mapping.MappingManager; | |
import com.datastax.driver.mapping.annotations.Frozen; | |
import com.datastax.driver.mapping.annotations.PartitionKey; | |
import com.datastax.driver.mapping.annotations.Table; | |
import com.datastax.driver.mapping.annotations.UDT; | |
public class DriverDemo { | |
// Make sure you run against a development Cassandra cluster. | |
static final String CONTACT_POINT = "127.0.0.1"; | |
// This will create and populate the keyspace if it does not exist. | |
static final String KEYSPACE = "test"; | |
public static void main(String[] args) { | |
Cluster cluster = null; | |
try { | |
cluster = Cluster.builder() | |
.addContactPoint(CONTACT_POINT) | |
.build(); | |
Session session = cluster.connect(); | |
createSchema(session); | |
runQueries(session); | |
} finally { | |
if (cluster != null) | |
cluster.close(); | |
} | |
} | |
private static void createSchema(Session session) { | |
session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};", KEYSPACE)); | |
session.execute(String.format("CREATE TYPE IF NOT EXISTS %s.address ( street text )", KEYSPACE)); | |
session.execute(String.format("CREATE TABLE IF NOT EXISTS %s.user (name text PRIMARY KEY , address frozen<address> )", KEYSPACE)); | |
} | |
private static void runQueries(Session session) { | |
MappingManager manager = new MappingManager(session); | |
Mapper<User> userMapper = manager.mapper(User.class); | |
userMapper.save(new User("John Q", new Address("main street"))); | |
User user = userMapper.get("John Q"); | |
System.out.println(user.getAddress().getStreet()); | |
} | |
@UDT(keyspace = KEYSPACE, name = "address") | |
public static class Address { | |
private String street; | |
public Address() { | |
} | |
public Address(String street) { | |
this.street = street; | |
} | |
public String getStreet() { | |
return street; | |
} | |
public void setStreet(String street) { | |
this.street = street; | |
} | |
} | |
@Table(keyspace = KEYSPACE, name = "user") | |
public static class User { | |
@PartitionKey | |
private String name; | |
@Frozen | |
private Address address; | |
public User() { | |
} | |
public User(String name, Address address) { | |
this.name = name; | |
this.address = address; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Address getAddress() { | |
return address; | |
} | |
public void setAddress(Address address) { | |
this.address = address; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment