Advertisement
Guest User

Untitled

a guest
Jul 27th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. public final class ConnectionManager {
  2.  
  3.     private Session session;
  4.     private Cluster cluster;
  5.     private String keySpace;
  6.     private String contactPoint;
  7.     private Integer replicationFactor;
  8.  
  9.     public ConnectionManager setContactPoint(String contactPoint) {
  10.         this.contactPoint = contactPoint;
  11.         this.replicationFactor = 1;
  12.         return this;
  13.     }
  14.  
  15.     public ConnectionManager setKeySpace(String keySpace) {
  16.         this.keySpace = keySpace;
  17.         return this;
  18.     }
  19.  
  20.     public ConnectionManager setReplicationFactor(Integer replicationFactor) {
  21.         this.replicationFactor = replicationFactor;
  22.         return this;
  23.     }
  24.  
  25.     public ConnectionManager buildConnection() {
  26.         CodecRegistry codecRegistry = new CodecRegistry();
  27.         cluster = Cluster.builder()
  28.                 .addContactPoint(this.contactPoint)
  29.                 .withCodecRegistry(codecRegistry)
  30.                 .withoutJMXReporting()
  31.                 .build();
  32.         Session s = cluster.connect();
  33.  
  34.         SimpleStatement statement = SchemaBuilder
  35.                 .createKeyspace(this.keySpace)
  36.                 .ifNotExists()
  37.                 .withSimpleStrategy(this.replicationFactor)
  38.                 .build();
  39.  
  40.         s.execute(statement.getQuery());
  41.  
  42.         session = cluster.connect(this.keySpace);
  43.         return this;
  44.     }
  45.  
  46.     public Session getSession() {
  47.         return this.session;
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement