Skip to content

Commit

Permalink
limit support added to test anonymization
Browse files Browse the repository at this point in the history
  • Loading branch information
sunitparekh committed Nov 3, 2017
1 parent 4e821db commit 1cb4de0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<version>1.4.196</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4</version>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/dataanon/dsl/Whitelist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.dataanon.jdbc.TableReader
import com.github.dataanon.jdbc.WhitelistTableWriter
import reactor.core.publisher.Flux

class Whitelist(private val sourceDbConfig: Map<String, String>, private val destDbConfig: Map<String, String>) : Strategy() {
class Whitelist(private val sourceDbConfig: Map<String, Any>, private val destDbConfig: Map<String, Any>) : Strategy() {

override fun execute() {
Flux.fromIterable(Iterable { TableReader(sourceDbConfig, tableName, anonymizer.columns, anonymizer.whitelist) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement

class BlacklistTableWriter(dbConfig: Map<String, String>, tableName: String, private val columns: Columns, private val primaryKey: Array<String>) : BaseSubscriber<Record>() {
private val conn: Connection = DriverManager.getConnection(dbConfig["url"], dbConfig["user"], dbConfig["password"])
class BlacklistTableWriter(dbConfig: Map<String, Any>, tableName: String, private val columns: Columns, private val primaryKey: Array<String>) : BaseSubscriber<Record>() {
private var conn: Connection = DriverManager.getConnection(dbConfig["url"] as String, dbConfig["user"] as String, dbConfig["password"] as String)
private val stmt: PreparedStatement

init {
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/com/github/dataanon/jdbc/TableReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet

class TableReader(dbConfig: Map<String, String>, tableName: String, private val columns: Columns, private val whitelist: Array<String>) : Iterator<Record> {
private var conn: Connection = DriverManager.getConnection(dbConfig["url"], dbConfig["user"], dbConfig["password"])
class TableReader(dbConfig: Map<String, Any>, tableName: String, private val columns: Columns, private val whitelist: Array<String>) : Iterator<Record> {
private var conn: Connection = DriverManager.getConnection(dbConfig["url"] as String, dbConfig["user"] as String, dbConfig["password"] as String)
private var rs: ResultSet
private var index = 0

init {
val stmt = conn.createStatement()
val sql = "SELECT " +
whitelist.joinToString(",") + "," + columns.names().joinToString(",") +
" FROM " + tableName
" FROM " + tableName +
(if(dbConfig.containsKey("limit")) " LIMIT ${dbConfig["limit"]} " else "")
println(sql)
rs = stmt.executeQuery(sql)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement

class WhitelistTableWriter(dbConfig: Map<String, String>, tableName: String, private val columns: Columns, private val whitelist: Array<String>) : BaseSubscriber<Record>() {
private val conn: Connection = DriverManager.getConnection(dbConfig["url"], dbConfig["user"], dbConfig["password"])
class WhitelistTableWriter(dbConfig: Map<String, Any>, tableName: String, private val columns: Columns, private val whitelist: Array<String>) : BaseSubscriber<Record>() {
private var conn: Connection = DriverManager.getConnection(dbConfig["url"] as String, dbConfig["user"] as String, dbConfig["password"] as String)
private val stmt: PreparedStatement

init {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.dataanon.strategies

import com.github.dataanon.Field
import com.github.dataanon.Record

class DefaultDoubleStrategy(private val value: Double = 4.2) : AnonymizationStrategy {
override fun anonymize(field: Field, record: Record): Any {
return value
}
}
22 changes: 22 additions & 0 deletions src/test/kotlin/com/github/dataanon/20MPostgresWhitelist.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.dataanon

import com.github.dataanon.dsl.Whitelist
import com.github.dataanon.strategies.DefaultDoubleStrategy

/**
* CREATE TABLE RATINGS_A(USERID INTEGER, MOVIEID INTEGER,RATING NUMERIC,TIMESTAMP BIGINT, PRIMARY KEY(USERID, MOVIEID))
*/
fun main(args: Array<String>) {
val source = hashMapOf("url" to "jdbc:postgresql://localhost:5432/movies", "user" to "sunitparekh", "password" to ""
, "limit" to 100000
)
val dest = hashMapOf("url" to "jdbc:postgresql://localhost:5432/moviesdest", "user" to "sunitparekh", "password" to "")


Whitelist(source,dest)
.table("RATINGS_A") {
whitelist("MOVIEID","USERID","TIMESTAMP")
anonymize("RATING").using(DefaultDoubleStrategy(4.3))
}
.execute()
}

0 comments on commit 1cb4de0

Please sign in to comment.