1
1
package org.jetbrains.kotlinx.dataframe.examples.exposed
2
2
3
3
import org.jetbrains.exposed.v1.core.Column
4
- import org.jetbrains.exposed.v1.core.StdOutSqlLogger
4
+ import org.jetbrains.exposed.v1.core.SortOrder
5
+ import org.jetbrains.exposed.v1.core.count
5
6
import org.jetbrains.exposed.v1.jdbc.Database
6
7
import org.jetbrains.exposed.v1.jdbc.SchemaUtils
7
- import org.jetbrains.exposed.v1.jdbc.addLogger
8
8
import org.jetbrains.exposed.v1.jdbc.batchInsert
9
9
import org.jetbrains.exposed.v1.jdbc.deleteAll
10
+ import org.jetbrains.exposed.v1.jdbc.select
10
11
import org.jetbrains.exposed.v1.jdbc.selectAll
11
12
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
12
13
import org.jetbrains.kotlinx.dataframe.api.asSequence
@@ -29,25 +30,41 @@ fun main() {
29
30
30
31
// let's read the database!
31
32
val df = transaction(db) {
32
- addLogger(StdOutSqlLogger )
33
+ // addLogger(StdOutSqlLogger) // enable if you want to see verbose logs
33
34
34
35
// tables in Exposed need to be defined, see tables.kt
35
36
SchemaUtils .create(Customers , Artists , Albums )
36
37
38
+ println ()
39
+
40
+ // In Exposed, we can write queries like this.
41
+ // Here, we count per country how many customers there are and print the results:
42
+ Customers
43
+ .select(Customers .country, Customers .customerId.count())
44
+ .groupBy(Customers .country)
45
+ .orderBy(Customers .customerId.count() to SortOrder .DESC )
46
+ .forEach {
47
+ println (" ${it[Customers .country]} : ${it[Customers .customerId.count()]} customers" )
48
+ }
49
+
50
+ println ()
51
+
37
52
// Perform the specific query you want to read into the DataFrame.
38
53
// Note: DataFrames are in-memory structures, so don't make it too large if you don't have the RAM ;)
39
54
val query = Customers .selectAll() // .where { Customers.company.isNotNull() }
40
55
56
+ println ()
57
+
41
58
// read and convert the query to a typed DataFrame
42
59
// see compatibilityLayer.kt for how we created convertToDataFrame<>()
43
- // and see tables.kt for how we created CustomersDf !
44
- query.convertToDataFrame<CustomersDf >()
60
+ // and see tables.kt for how we created DfCustomers !
61
+ query.convertToDataFrame<DfCustomers >()
45
62
}
46
63
47
64
println (df.size())
48
65
49
66
// now we have a DataFrame, we can perform DataFrame operations,
50
- // like seeing how often a country is represented
67
+ // like doing the same operation as we did in Exposed above
51
68
df.groupBy { country }.count()
52
69
.sortByDesc { " count" <Int >() }
53
70
.print (columnTypes = true , borders = true )
@@ -60,15 +77,18 @@ fun main() {
60
77
61
78
// writing a DataFrame back into an SQL database with Exposed can also be done easily!
62
79
transaction(db) {
63
- addLogger(StdOutSqlLogger )
80
+ // addLogger(StdOutSqlLogger) // enable if you want to see verbose logs
64
81
65
82
// first delete the original contents
66
83
Customers .deleteAll()
67
84
85
+ println ()
86
+
68
87
// batch-insert our dataframe back into the SQL database as a sequence of rows
69
88
Customers .batchInsert(df.asSequence()) { dfRow ->
70
89
// we simply go over each value in the row and put it in the right place in the Exposed statement
71
90
for (column in Customers .columns) {
91
+ @Suppress(" UNCHECKED_CAST" )
72
92
this [column as Column <Any ?>] = dfRow[column.name]
73
93
}
74
94
}
0 commit comments