-
Notifications
You must be signed in to change notification settings - Fork 7
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 #830 from navikt/kafka_bq_sink
Legger til kafka BigQuery sink app
- Loading branch information
Showing
10 changed files
with
260 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
apiVersion: nais.io/v1alpha1 | ||
kind: Application | ||
metadata: | ||
name: notifikasjon-kafka-bq | ||
namespace: fager | ||
labels: | ||
team: fager | ||
spec: | ||
image: {{image}} | ||
resources: | ||
requests: | ||
cpu: 200m | ||
memory: 256Mi | ||
limits: | ||
memory: 512Mi | ||
liveness: | ||
path: /internal/alive | ||
readiness: | ||
path: /internal/ready | ||
replicas: | ||
min: 1 | ||
max: 1 | ||
prometheus: | ||
enabled: true | ||
path: /internal/metrics | ||
kafka: | ||
pool: nav-dev | ||
gcp: | ||
bigQueryDatasets: | ||
- name: debughendelse | ||
permission: READWRITE | ||
env: | ||
- name: BIGQUERY_DATASET_ID | ||
value: debughendelse | ||
- name: BIGQUERY_TABLE_NAME | ||
value: notifikasjon |
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,36 @@ | ||
apiVersion: nais.io/v1alpha1 | ||
kind: Application | ||
metadata: | ||
name: notifikasjon-kafka-bq | ||
namespace: fager | ||
labels: | ||
team: fager | ||
spec: | ||
image: {{image}} | ||
resources: | ||
requests: | ||
cpu: 200m | ||
memory: 256Mi | ||
limits: | ||
memory: 512Mi | ||
liveness: | ||
path: /internal/alive | ||
readiness: | ||
path: /internal/ready | ||
replicas: | ||
min: 1 | ||
max: 1 | ||
prometheus: | ||
enabled: true | ||
path: /internal/metrics | ||
kafka: | ||
pool: nav-prod | ||
gcp: | ||
bigQueryDatasets: | ||
- name: debughendelse | ||
permission: READWRITE | ||
env: | ||
- name: BIGQUERY_DATASET_ID | ||
value: debughendelse | ||
- name: BIGQUERY_TABLE_NAME | ||
value: notifikasjon |
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
45 changes: 45 additions & 0 deletions
45
app/src/main/kotlin/no/nav/arbeidsgiver/notifikasjon/infrastruktur/BigQueryClient.kt
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,45 @@ | ||
package no.nav.arbeidsgiver.notifikasjon.infrastruktur | ||
|
||
import com.google.cloud.bigquery.* | ||
|
||
interface BigQueryClient { | ||
fun getOrCreateTable(tableName: String, schema: Schema): TableId | ||
fun insert(tableId: TableId, insertId: String, row: Map<String, Any>) | ||
} | ||
|
||
class BigQueryClientImpl( | ||
projectId: String, | ||
private val datasetId: String, | ||
) : BigQueryClient { | ||
private val bigQuery = BigQueryOptions.newBuilder() | ||
.setProjectId(projectId) | ||
.build() | ||
.service | ||
|
||
override fun getOrCreateTable(tableName: String, schema: Schema): TableId { | ||
val tableId = TableId.of(datasetId, tableName) | ||
val existingTable = bigQuery.getTable(tableId) | ||
|
||
return if (existingTable != null) { | ||
tableId | ||
} else { | ||
createTable(tableId, schema) | ||
} | ||
} | ||
|
||
private fun createTable(tableId: TableId, schema: Schema): TableId { | ||
val tableDefinition = StandardTableDefinition.of(schema) | ||
val tableInfo = TableInfo.of(tableId, tableDefinition) | ||
return bigQuery.create(tableInfo).tableId | ||
} | ||
|
||
override fun insert(tableId: TableId, insertId: String, row: Map<String, Any>) { | ||
val insertResponse = bigQuery.getTable(tableId).insert( | ||
listOf(InsertAllRequest.RowToInsert.of(insertId, row)) | ||
) | ||
|
||
if (insertResponse.hasErrors()) { | ||
throw RuntimeException("Lagring i BigQuery feilet: '${insertResponse.insertErrors}'") | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/kotlin/no/nav/arbeidsgiver/notifikasjon/kafka_bq/BigQueryHendelseService.kt
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,35 @@ | ||
package no.nav.arbeidsgiver.notifikasjon.kafka_bq | ||
|
||
import com.google.cloud.bigquery.Field | ||
import com.google.cloud.bigquery.Schema | ||
import com.google.cloud.bigquery.StandardSQLTypeName | ||
import com.google.cloud.bigquery.TableId | ||
import no.nav.arbeidsgiver.notifikasjon.hendelse.HendelseModel | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.BigQueryClient | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.json.laxObjectMapper | ||
|
||
class BigQueryHendelseService( | ||
private val bigQueryClient: BigQueryClient, | ||
private val tableName: String, | ||
) { | ||
private val tableId: TableId by lazy { | ||
bigQueryClient.getOrCreateTable(tableName, schema) | ||
} | ||
|
||
private val schema = Schema.of( | ||
Field.of("hendelseId", StandardSQLTypeName.STRING), | ||
Field.of("aggregateId", StandardSQLTypeName.STRING), | ||
Field.of("virksomhetsnummer", StandardSQLTypeName.STRING), | ||
Field.of("data", StandardSQLTypeName.JSON) | ||
) | ||
|
||
fun insertHendelse(hendelse: HendelseModel.Hendelse) { | ||
val row = mapOf( | ||
"hendelseId" to hendelse.hendelseId.toString(), | ||
"aggregateId" to hendelse.aggregateId.toString(), | ||
"virksomhetsnummer" to hendelse.virksomhetsnummer, | ||
"data" to laxObjectMapper.writeValueAsString(hendelse) | ||
) | ||
bigQueryClient.insert(tableId, hendelse.hendelseId.toString(), row) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/kotlin/no/nav/arbeidsgiver/notifikasjon/kafka_bq/KafkaBQ.kt
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,44 @@ | ||
package no.nav.arbeidsgiver.notifikasjon.kafka_bq | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.BigQueryClientImpl | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.Health | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.Subsystem | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.http.launchHttpServer | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.kafka.HendelsesstrømKafkaImpl | ||
|
||
object KafkaBQ { | ||
private val hendelsesstrøm by lazy { | ||
HendelsesstrømKafkaImpl(groupId = "kafka-bq-v1") | ||
} | ||
private val projectId = System.getenv("GCP_TEAM_PROJECT_ID") | ||
?: error("Missing required environment variable: GCP_TEAM_PROJECT_ID") | ||
private val datasetId = System.getenv("BIGQUERY_DATASET_ID") | ||
?: error("Missing required environment variable: BIGQUERY_DATASET_ID") | ||
private val tableName = System.getenv("BIGQUERY_TABLE_NAME") | ||
?: error("Missing required environment variable: BIGQUERY_TABLE_NAME") | ||
|
||
private val bigQueryHendelseService = BigQueryHendelseService( | ||
tableName = tableName, | ||
bigQueryClient = BigQueryClientImpl( | ||
projectId = projectId, | ||
datasetId = datasetId | ||
), | ||
) | ||
|
||
fun main(httpPort: Int = 8080) { | ||
runBlocking(Dispatchers.Default) { | ||
Health.subsystemReady[Subsystem.DATABASE] = true | ||
|
||
launch { | ||
hendelsesstrøm.forEach { hendelse -> | ||
bigQueryHendelseService.insertHendelse(hendelse) | ||
} | ||
} | ||
|
||
launchHttpServer(httpPort = httpPort) | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...src/test/kotlin/no/nav/arbeidsgiver/notifikasjon/kafka_bq/BigQueryHendelseServiceTests.kt
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,52 @@ | ||
package no.nav.arbeidsgiver.notifikasjon.kafka_bq | ||
|
||
import com.google.cloud.bigquery.Schema | ||
import com.google.cloud.bigquery.TableId | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.BigQueryClient | ||
import no.nav.arbeidsgiver.notifikasjon.util.EksempelHendelse | ||
|
||
class BigQueryHendelseServiceTest : FunSpec({ | ||
|
||
test("insertHendelse should store the correct row in the BigQuery Local Client") { | ||
val datasetId = "myLocalDataset" | ||
val tableName = "myLocalTable" | ||
|
||
val localBigQueryClient = LocalBigQueryClient(datasetId) | ||
|
||
val bigQueryHendelseService = BigQueryHendelseService( | ||
bigQueryClient = localBigQueryClient, | ||
tableName = tableName | ||
) | ||
|
||
val testHendelse = EksempelHendelse.SakOpprettet | ||
|
||
bigQueryHendelseService.insertHendelse(testHendelse) | ||
|
||
val tableId = TableId.of(datasetId, tableName) | ||
val rows = localBigQueryClient.tableData[tableId] | ||
|
||
rows?.size shouldBe 1 | ||
|
||
val insertedRow = rows?.first() | ||
insertedRow?.get("hendelseId") shouldBe testHendelse.hendelseId.toString() | ||
|
||
val jsonData = insertedRow?.get("data") as String | ||
jsonData.contains(testHendelse.hendelseId.toString()) shouldBe true | ||
} | ||
}) | ||
|
||
class LocalBigQueryClient(private val datasetId: String) : BigQueryClient { | ||
val tableData = mutableMapOf<TableId, MutableList<Map<String, Any>>>() | ||
|
||
override fun getOrCreateTable(tableName: String, schema: Schema): TableId { | ||
val tableId = TableId.of(datasetId, tableName) | ||
tableData.putIfAbsent(tableId, mutableListOf()) | ||
return tableId | ||
} | ||
|
||
override fun insert(tableId: TableId, insertId: String, row: Map<String, Any>) { | ||
tableData[tableId]?.add(row) | ||
} | ||
} |