Created
April 15, 2018 17:25
-
-
Save lankydan/bb9af7cba9a379871e5853fdcc87cff5 to your computer and use it in GitHub Desktop.
Datastax driver - configuration
This file contains 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
@Configuration | |
public class CassandraConfig { | |
@Bean | |
public Cluster cluster( | |
@Value("${cassandra.host:127.0.0.1}") String host, | |
@Value("${cassandra.cluster.name:cluster}") String clusterName, | |
@Value("${cassandra.port:9042}") int port) { | |
return Cluster.builder() | |
.addContactPoint(host) | |
.withPort(port) | |
.withClusterName(clusterName) | |
.build(); | |
} | |
@Bean | |
public Session session(Cluster cluster, @Value("${cassandra.keyspace}") String keyspace) | |
throws IOException { | |
final Session session = cluster.connect(); | |
setupKeyspace(session, keyspace); | |
return session; | |
} | |
private void setupKeyspace(Session session, String keyspace) throws IOException { | |
final Map<String, Object> replication = new HashMap<>(); | |
replication.put("class", "SimpleStrategy"); | |
replication.put("replication_factor", 1); | |
session.execute(createKeyspace(keyspace).ifNotExists().with().replication(replication)); | |
session.execute("USE " + keyspace); | |
// String[] statements = split(IOUtils.toString(getClass().getResourceAsStream("/cql/setup.cql")), ";"); | |
// Arrays.stream(statements).map(statement -> normalizeSpace(statement) + ";").forEach(session::execute); | |
} | |
@Bean | |
public MappingManager mappingManager(Session session) { | |
final PropertyMapper propertyMapper = | |
new DefaultPropertyMapper() | |
.setNamingStrategy(new DefaultNamingStrategy(LOWER_CAMEL_CASE, LOWER_SNAKE_CASE)); | |
final MappingConfiguration configuration = | |
MappingConfiguration.builder().withPropertyMapper(propertyMapper).build(); | |
return new MappingManager(session, configuration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment