-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1013 from jmarin/institutions-cassandra
Institutions cassandra
- Loading branch information
Showing
19 changed files
with
442 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,9 @@ db { | |
keepAliveConnection = true | ||
} | ||
} | ||
|
||
cassandra { | ||
host = "127.0.0.1" | ||
port = 9142 | ||
keyspace = "hmda_query" | ||
} |
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package hmda.query | ||
|
||
import com.typesafe.config.ConfigFactory | ||
|
||
object CassandraConfig { | ||
val config = ConfigFactory.load() | ||
val cassandraHost = config.getString("cassandra.host") | ||
val cassandraPort = config.getInt("cassandra.port") | ||
val cassandraKeyspace = config.getString("cassandra.keyspace") | ||
val numberOfRetries = config.getInt("cassandra.retries") | ||
val retryInterval = config.getLong("cassandra.retry-interval") | ||
} |
41 changes: 41 additions & 0 deletions
41
...y/src/main/scala/hmda/query/projections/institutions/InstitutionCassandraProjection.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package hmda.query.projections.institutions | ||
|
||
import akka.NotUsed | ||
import akka.actor.{ ActorSystem, Scheduler } | ||
import akka.stream.ActorMaterializer | ||
import akka.stream.alpakka.cassandra.scaladsl.CassandraSink | ||
import akka.stream.scaladsl.Source | ||
import hmda.persistence.messages.events.institutions.InstitutionEvents.{ InstitutionCreated, InstitutionModified } | ||
import hmda.persistence.processing.HmdaQuery._ | ||
import hmda.query.model.institutions.InstitutionQuery | ||
import hmda.query.repository.institutions.InstitutionCassandraRepository | ||
import hmda.query.repository.institutions.InstitutionConverter._ | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class InstitutionCassandraProjection extends InstitutionCassandraRepository { | ||
|
||
def startUp(): Unit = { | ||
|
||
createKeyspace() | ||
createTable() | ||
|
||
val source: Source[InstitutionQuery, NotUsed] = liveEvents("institutions").map { | ||
case InstitutionCreated(i) => i | ||
case InstitutionModified(i) => i | ||
} | ||
|
||
val sink = CassandraSink[InstitutionQuery](parallelism = 2, preparedStatement, statementBinder) | ||
|
||
source.runWith(sink) | ||
|
||
} | ||
|
||
override implicit def materializer: ActorMaterializer = ActorMaterializer() | ||
|
||
override implicit def system: ActorSystem = ActorSystem() | ||
|
||
override implicit val ec: ExecutionContext = system.dispatcher | ||
|
||
override implicit val scheduler: Scheduler = system.scheduler | ||
} |
70 changes: 70 additions & 0 deletions
70
query/src/main/scala/hmda/query/repository/CassandraRepository.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package hmda.query.repository | ||
|
||
import akka.actor.Scheduler | ||
import akka.{ Done, NotUsed } | ||
import akka.stream.scaladsl.Source | ||
import com.datastax.driver.core.policies.ExponentialReconnectionPolicy | ||
import com.datastax.driver.core.{ Cluster, ResultSet, Row, Session } | ||
import hmda.query.CassandraConfig._ | ||
import scala.annotation.tailrec | ||
import scala.concurrent.{ ExecutionContext, Future } | ||
import scala.util.{ Success, Try } | ||
import org.slf4j.LoggerFactory | ||
|
||
trait CassandraRepository[A] { | ||
|
||
implicit val ec: ExecutionContext | ||
implicit val scheduler: Scheduler | ||
|
||
val log = LoggerFactory.getLogger("CassandraRepository") | ||
|
||
val keyspace = "hmda_query" | ||
|
||
@tailrec | ||
private def retry[T](n: Int)(fn: => T): Try[T] = { | ||
log.info("*********ATTEMPTING CONNECTION TO CASSANDRA QUERY CLUSTER********") | ||
Try { fn } match { | ||
case x: Success[T] => x | ||
case _ if n > 1 => | ||
Thread.sleep(retryInterval) | ||
retry(n - 1)(fn) | ||
case fn => fn | ||
} | ||
} | ||
|
||
implicit val session: Session = { | ||
retry(numberOfRetries) { | ||
Cluster | ||
.builder | ||
.addContactPoint(cassandraHost) | ||
.withPort(cassandraPort) | ||
.withReconnectionPolicy(new ExponentialReconnectionPolicy(100L, 200000L)) | ||
.build | ||
.connect() | ||
}.getOrElse(null) | ||
} | ||
|
||
def createKeyspace(): ResultSet = { | ||
val query = | ||
s""" | ||
|CREATE KEYSPACE IF NOT EXISTS $keyspace WITH REPLICATION = { | ||
| 'class': 'SimpleStrategy', | ||
| 'replication_factor': '1' | ||
|} | ||
""".stripMargin | ||
|
||
session.execute(query) | ||
} | ||
def dropKeyspace(): ResultSet = { | ||
val query = | ||
s""" | ||
|DROP KEYSPACE $keyspace | ||
""".stripMargin | ||
session.execute(query) | ||
} | ||
def createTable(): ResultSet | ||
def dropTable(): ResultSet | ||
def insertData(source: Source[A, NotUsed]): Future[Done] | ||
def readData(fetchSize: Int): Future[Seq[Row]] | ||
|
||
} |
Oops, something went wrong.