1- /* package org.jetbrains.kotlinx.dataframe.examples.jdbc
1+ package org.jetbrains.kotlinx.dataframe.examples.jdbc
22
33import java.sql.DriverManager
44import java.util.Properties
55import org.jetbrains.kotlinx.dataframe.DataFrame
6+ import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
67import org.jetbrains.kotlinx.dataframe.api.cast
78import org.jetbrains.kotlinx.dataframe.api.describe
89import org.jetbrains.kotlinx.dataframe.api.filter
10+ import org.jetbrains.kotlinx.dataframe.api.generateInterfaces
911import org.jetbrains.kotlinx.dataframe.api.print
1012import org.jetbrains.kotlinx.dataframe.io.db.MySql
1113import org.jetbrains.kotlinx.dataframe.io.getSchemaForResultSet
@@ -16,22 +18,14 @@ import org.jetbrains.kotlinx.dataframe.io.readResultSet
1618import org.jetbrains.kotlinx.dataframe.io.readSqlQuery
1719import org.jetbrains.kotlinx.dataframe.io.readSqlTable
1820
19- /**
20- * For this example, the schema importing was configured in Gradle in the
21- * ```kotlin
22- * dataframes {
23- * schema {
24- * ...
25- * }
26- * }
27- * ```
28- * section.
29- *
30- * Open the `build.gradle.kts` to see or edit.
31- *
32- * NOTE: The idea of this example is to cover more available functionality
33- * and demonstrate different ways to establish connection to the database.
34- */
21+ @DataSchema
22+ interface TarantinoFilms {
23+ val genres: String?
24+ val name: String
25+ val rank: Float?
26+ val year: Int
27+ }
28+
3529fun main () {
3630 val props = Properties ()
3731 props.setProperty(" user" , USER_NAME )
@@ -42,12 +36,10 @@ fun main() {
4236 DriverManager .getConnection(URL , props).use { connection ->
4337 // read the data from the SQL table
4438 val actors = DataFrame .readSqlTable(connection, TABLE_NAME_ACTORS , 100 ).cast<Actors >()
45-
46- // TODO: .cast<Actors>(verify=true)
4739 actors.print ()
4840
4941 // filter and print the data
50- actors.filter { firstName!=null && firstName!! .contains("J") }.print()
42+ actors.filter { firstName.contains(" J" ) }.print ()
5143
5244 // extract the schema of the SQL table
5345 val actorSchema = DataFrame .getSchemaForSqlTable(connection, TABLE_NAME_ACTORS )
@@ -58,12 +50,15 @@ fun main() {
5850 println (" ---------------------------- Part 2: SQL Query ------------------------------------" )
5951 DriverManager .getConnection(URL , props).use { connection ->
6052 // read the data from as a result of an executed SQL query
61- val tarantinoFilms = DataFrame.readSqlQuery(connection, TARANTINO_FILMS_SQL_QUERY, 100).cast<TarantinoFilms>()
62- //TODO: .cast<TarantinoFilms>(verify=true)
53+ val tarantinoFilmsUntyped = DataFrame .readSqlQuery(connection, TARANTINO_FILMS_SQL_QUERY , 100 )
54+ tarantinoFilmsUntyped.generateInterfaces(" TarantinoFilms" ).print ()
55+
56+ val tarantinoFilms = tarantinoFilmsUntyped.cast<TarantinoFilms >()
57+
6358 tarantinoFilms.print ()
6459
6560 // transform and print the data
66- tarantinoFilms.filter { year != null && year!! > 2000 }.print()
61+ tarantinoFilms.filter { year > 2000 }.print ()
6762
6863 // extract the schema of the SQL table
6964 val tarantinoFilmsSchema = DataFrame .getSchemaForSqlQuery(connection, TARANTINO_FILMS_SQL_QUERY )
@@ -77,10 +72,10 @@ fun main() {
7772 st.executeQuery(TARANTINO_FILMS_SQL_QUERY ).use { rs ->
7873 // read the data from as a result of an executed SQL query
7974 val tarantinoFilms = DataFrame .readResultSet(rs, dbType = MySql , 100 ).cast<TarantinoFilms >()
80- // TODO: .cast<TarantinoFilms>(verify=true)
75+
8176 tarantinoFilms.print ()
8277 // transform and print the data
83- tarantinoFilms.filter { year!= null && year!! > 2000 }.print()
78+ tarantinoFilms.filter { year > 2000 }.print ()
8479
8580 // extract the schema of the SQL table
8681 val tarantinoFilmsSchema = DataFrame .getSchemaForResultSet(rs, dbType = MySql )
@@ -99,4 +94,4 @@ fun main() {
9994 it.describe()
10095 }
10196 }
102- }*/
97+ }
0 commit comments