Skip to content

Commit 919284d

Browse files
committed
tiny changes to exposed example, adding exposed query to compare with df
1 parent 19c0858 commit 919284d

File tree

2 files changed

+29
-10
lines changed
  • examples/idea-examples/unsupported-data-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/exposed

2 files changed

+29
-10
lines changed

examples/idea-examples/unsupported-data-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/exposed/main.kt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.jetbrains.kotlinx.dataframe.examples.exposed
22

33
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
56
import org.jetbrains.exposed.v1.jdbc.Database
67
import org.jetbrains.exposed.v1.jdbc.SchemaUtils
7-
import org.jetbrains.exposed.v1.jdbc.addLogger
88
import org.jetbrains.exposed.v1.jdbc.batchInsert
99
import org.jetbrains.exposed.v1.jdbc.deleteAll
10+
import org.jetbrains.exposed.v1.jdbc.select
1011
import org.jetbrains.exposed.v1.jdbc.selectAll
1112
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
1213
import org.jetbrains.kotlinx.dataframe.api.asSequence
@@ -29,25 +30,41 @@ fun main() {
2930

3031
// let's read the database!
3132
val df = transaction(db) {
32-
addLogger(StdOutSqlLogger)
33+
// addLogger(StdOutSqlLogger) // enable if you want to see verbose logs
3334

3435
// tables in Exposed need to be defined, see tables.kt
3536
SchemaUtils.create(Customers, Artists, Albums)
3637

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+
3752
// Perform the specific query you want to read into the DataFrame.
3853
// Note: DataFrames are in-memory structures, so don't make it too large if you don't have the RAM ;)
3954
val query = Customers.selectAll() // .where { Customers.company.isNotNull() }
4055

56+
println()
57+
4158
// read and convert the query to a typed DataFrame
4259
// 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>()
4562
}
4663

4764
println(df.size())
4865

4966
// 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
5168
df.groupBy { country }.count()
5269
.sortByDesc { "count"<Int>() }
5370
.print(columnTypes = true, borders = true)
@@ -60,15 +77,18 @@ fun main() {
6077

6178
// writing a DataFrame back into an SQL database with Exposed can also be done easily!
6279
transaction(db) {
63-
addLogger(StdOutSqlLogger)
80+
// addLogger(StdOutSqlLogger) // enable if you want to see verbose logs
6481

6582
// first delete the original contents
6683
Customers.deleteAll()
6784

85+
println()
86+
6887
// batch-insert our dataframe back into the SQL database as a sequence of rows
6988
Customers.batchInsert(df.asSequence()) { dfRow ->
7089
// we simply go over each value in the row and put it in the right place in the Exposed statement
7190
for (column in Customers.columns) {
91+
@Suppress("UNCHECKED_CAST")
7292
this[column as Column<Any?>] = dfRow[column.name]
7393
}
7494
}

examples/idea-examples/unsupported-data-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/exposed/tables.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
66
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
77
import org.jetbrains.kotlinx.dataframe.api.generateDataClasses
88
import org.jetbrains.kotlinx.dataframe.api.print
9-
import org.jetbrains.kotlinx.dataframe.codeGen.NameNormalizer
109

1110
object Albums : Table() {
1211
val albumId: Column<Int> = integer("AlbumId").autoIncrement()
@@ -60,15 +59,15 @@ fun main() {
6059
// we use a NameNormalizer to let DataFrame generate the same accessors as in the Table
6160
// while keeping the correct column names
6261
schema.generateDataClasses(
63-
name = "CustomersDf",
62+
name = "DfCustomers",
6463
nameNormalizer = nameNormalizer,
6564
).print()
6665
}
6766

6867
// created by Customers.toDataFrameSchema()
6968
// The same can be done for the other tables
7069
@DataSchema
71-
data class CustomersDf(
70+
data class DfCustomers(
7271
@ColumnName("Address")
7372
val address: String?,
7473
@ColumnName("City")

0 commit comments

Comments
 (0)