Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.util.*

plugins {
kotlin("jvm") version "2.0.0"
kotlin("jvm") version "2.0.20"
id("org.jetbrains.kotlinx.dataframe") version "0.14.1"
}

Expand All @@ -10,23 +10,24 @@ version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev/")
}

dependencies {
implementation ("org.jetbrains.kotlinx:dataframe:0.14.1")
implementation ("org.jetbrains.kotlinx:kandy-lets-plot:0.7.0")
implementation ("org.jetbrains.kotlinx:kandy-api:0.7.0")
implementation ("org.mariadb.jdbc:mariadb-java-client:3.1.4")
kotlinCompilerPluginClasspath("org.jetbrains.kotlinx.dataframe:compiler-plugin-all:0.14.1")
testImplementation(kotlin("test"))
}


val props = Properties()
file("local.properties").inputStream().use { props.load(it) }

dataframes {
schema {
data = "jdbc:mariadb://localhost:3306/imdb"
data = "jdbc:mariadb://localhost:3307/imdb"
name = "org.jetbrains.kotlinx.dataframe.examples.jdbc.Actors"
jdbcOptions {
user = props.getProperty("db.user")
Expand All @@ -35,7 +36,7 @@ dataframes {
}
}
schema {
data = "jdbc:mariadb://localhost:3306/imdb"
data = "jdbc:mariadb://localhost:3307/imdb"
name = "org.jetbrains.kotlinx.dataframe.examples.jdbc.TarantinoFilms"
jdbcOptions {
user = System.getenv("DB_USER") ?: props.getProperty("db.user")
Expand All @@ -58,4 +59,12 @@ tasks.test {
}
kotlin {
jvmToolchain(11)
}

tasks.compileKotlin {
compilerOptions {
freeCompilerArgs.addAll("-P", "plugin:org.jetbrains.kotlinx.dataframe:path=${projectDir.absolutePath}")
freeCompilerArgs.addAll("-P", "plugin:org.jetbrains.kotlinx.dataframe:schemas=${layout.buildDirectory.file("generated").get().asFile.absolutePath}")
}
compilerExecutionStrategy.set(org.jetbrains.kotlin.gradle.tasks.KotlinCompilerExecutionStrategy.IN_PROCESS)
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
kotlin.code.style=official
kotlin.dataframe.add.ksp=false
308 changes: 154 additions & 154 deletions notebooks/imdb.ipynb

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
pluginManagement {
repositories {
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev/")
mavenCentral()
gradlePluginPortal()
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
}
Expand Down
54 changes: 43 additions & 11 deletions src/main/kotlin/Example_1_Define_schema_manually.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,73 @@
package org.jetbrains.kotlinx.dataframe.examples.jdbc

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.add
import org.jetbrains.kotlinx.dataframe.api.describe
import org.jetbrains.kotlinx.dataframe.api.select
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.filter
import org.jetbrains.kotlinx.dataframe.api.sortByDesc
import org.jetbrains.kotlinx.dataframe.api.print
import org.jetbrains.kotlinx.dataframe.api.take
import org.jetbrains.kotlinx.dataframe.examples.jdbc.PASSWORD
import org.jetbrains.kotlinx.dataframe.examples.jdbc.TABLE_NAME_MOVIES
import org.jetbrains.kotlinx.dataframe.examples.jdbc.URL
import org.jetbrains.kotlinx.dataframe.examples.jdbc.USER_NAME
import org.jetbrains.kotlinx.dataframe.io.readSqlTable
import org.jetbrains.kotlinx.dataframe.io.DbConnectionConfig
import org.jetbrains.kotlinx.dataframe.io.readDataFrame
import org.jetbrains.kotlinx.dataframe.io.getSchemaForSqlTable
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema

@DataSchema
interface Movies {
val id: Int
val name: String
val year: Int
val rank: Float?
val id: kotlin.Int
val name: kotlin.String
val year: kotlin.Int
val rank: kotlin.Float?
}

fun main() {
// define the database configuration
val dbConfig = DbConnectionConfig(URL, USER_NAME, PASSWORD)

val moviesSchema = DataFrame.getSchemaForSqlTable(dbConfig, TABLE_NAME_MOVIES)
moviesSchema.printAsInterfaceWithAnnotation(TABLE_NAME_MOVIES)

// read the table
val movies = DataFrame.readSqlTable(dbConfig, TABLE_NAME_MOVIES, 10000).cast<Movies>(verify=true)


// print the dataframe
movies.print()

// print the dataframe metadata and statistics
movies.describe().print()

// print names of top-10 rated films
movies.sortByDesc { rank }
.take(10)
.select { name }
.print()
val result = movies.sortByDesc { rank }
.select { name and year }
.add("oldFilm") { year < 1973 }
.add("containsReward") { name.contains("Reward") }

result.filter { oldFilm and containsReward }.take(10).print()
}

fun String.convertToValidInterfaceName(): String {
return this.split("_", " ")
.joinToString("") { it.replaceFirstChar { char -> char.uppercaseChar() } }
}

fun DataFrameSchema.printAsInterfaceWithAnnotation(interfaceName: String? = null) {
val schemaBuilder = StringBuilder()
schemaBuilder.appendLine("@DataSchema")
if(interfaceName != null) schemaBuilder.appendLine("interface ${interfaceName.convertToValidInterfaceName()} {")
else schemaBuilder.appendLine("interface GeneratedSchema {")

columns.forEach { (name, column) ->
val type = column.type
schemaBuilder.appendLine(" val $name: $type")
}

schemaBuilder.appendLine("}")

println(schemaBuilder.toString())
}
13 changes: 9 additions & 4 deletions src/main/kotlin/Example_2_Import_schema_annotation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
jdbcOptions = JdbcOptions(USER_NAME, PASSWORD, sqlQuery = ACTORS_IN_LATEST_MOVIES)
)

package org.jetbrains.kotlinx.dataframe.examples.jdbc

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.annotations.ImportDataSchema
import org.jetbrains.kotlinx.dataframe.annotations.JdbcOptions
Expand All @@ -23,13 +21,20 @@ import org.jetbrains.kotlinx.dataframe.api.count
import org.jetbrains.kotlinx.dataframe.io.readSqlQuery
import org.jetbrains.kotlinx.dataframe.io.readSqlTable
import org.jetbrains.kotlinx.dataframe.api.take
import org.jetbrains.kotlinx.dataframe.examples.jdbc.ACTORS_IN_LATEST_MOVIES
//import org.jetbrains.kotlinx.dataframe.examples.jdbc.Directors
//import org.jetbrains.kotlinx.dataframe.examples.jdbc.NewActors
import org.jetbrains.kotlinx.dataframe.examples.jdbc.PASSWORD
import org.jetbrains.kotlinx.dataframe.examples.jdbc.TABLE_NAME_DIRECTORS
import org.jetbrains.kotlinx.dataframe.examples.jdbc.URL
import org.jetbrains.kotlinx.dataframe.examples.jdbc.USER_NAME
import org.jetbrains.kotlinx.dataframe.io.DbConnectionConfig

fun main() {
// Part 1: Reading the table `directors`

// define the database configuration
val dbConfig = DbConnectionConfig(URL, USER_NAME, PASSWORD)
/* val dbConfig = DbConnectionConfig(URL, USER_NAME, PASSWORD)

// read the table
val directors = DataFrame.readSqlTable(dbConfig, TABLE_NAME_DIRECTORS, 1000).cast<Directors>()
Expand All @@ -48,5 +53,5 @@ fun main() {
// build a report of different roles' popularity
newActors.groupBy { role }
.count()
.print()
.print()*/
}
17 changes: 11 additions & 6 deletions src/main/kotlin/Example_3_Import_schema_via_Gradle.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.jetbrains.kotlinx.dataframe.examples.jdbc

import java.sql.DriverManager
import java.util.Properties
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.describe
import org.jetbrains.kotlinx.dataframe.api.filter
import org.jetbrains.kotlinx.dataframe.api.print
import org.jetbrains.kotlinx.dataframe.examples.jdbc.Actors
import org.jetbrains.kotlinx.dataframe.examples.jdbc.PASSWORD
import org.jetbrains.kotlinx.dataframe.examples.jdbc.TABLE_NAME_ACTORS
import org.jetbrains.kotlinx.dataframe.examples.jdbc.TARANTINO_FILMS_SQL_QUERY
import org.jetbrains.kotlinx.dataframe.examples.jdbc.TarantinoFilms
import org.jetbrains.kotlinx.dataframe.examples.jdbc.URL
import org.jetbrains.kotlinx.dataframe.examples.jdbc.USER_NAME
import org.jetbrains.kotlinx.dataframe.io.*

/**
Expand All @@ -26,22 +31,22 @@ import org.jetbrains.kotlinx.dataframe.io.*
* and demonstrate different ways to establish connection to the database.
*/
fun main() {
val props = Properties()
/*val props = Properties()
props.setProperty("user", USER_NAME)
props.setProperty("password", PASSWORD)

// Part 1: Getting the data from the SQL table with an explicit announcement of the Connection object from the JDBC driver.
println("---------------------------- Part 1: SQL Table ------------------------------------")
DriverManager.getConnection(URL, props).use { connection ->
// read the data from the SQL table
val actors = DataFrame.readSqlTable(connection, TABLE_NAME_ACTORS, 100).cast<Actors>()
val actors = DataFrame.readSqlTable(connection, TABLE_NAME_ACTORS, 100).cast<Actors>()
actors.print()

// filter and print the data
actors.filter { firstName!=null && firstName!!.contains("J") }.print()

// extract the schema of the SQL table
val actorSchema = DataFrame.getSchemaForSqlTable(connection, TABLE_NAME_ACTORS)
val actorSchema = DataFrame.getSchemaForSqlTable(connection, TABLE_NAME_ACTORS)
actorSchema.print()
}

Expand Down Expand Up @@ -89,5 +94,5 @@ fun main() {
it.print()
it.describe()
}
}
}*/
}
2 changes: 1 addition & 1 deletion src/main/kotlin/jdbcUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jetbrains.kotlinx.dataframe.examples.jdbc

const val URL = "jdbc:mariadb://localhost:3306/imdb"
const val URL = "jdbc:mariadb://localhost:3307/imdb"
const val USER_NAME = "root"
const val PASSWORD = "pass"
const val TABLE_NAME_ACTORS = "actors"
Expand Down