Skip to content

Commit a6b00a9

Browse files
add maven project example
1 parent 69e1ff5 commit a6b00a9

File tree

11 files changed

+263
-2
lines changed

11 files changed

+263
-2
lines changed

examples/kotlin-dataframe-plugin-example/README.md renamed to examples/kotlin-dataframe-plugin-gradle-example/README.md

File renamed without changes.

examples/kotlin-dataframe-plugin-example/build.gradle.kts renamed to examples/kotlin-dataframe-plugin-gradle-example/build.gradle.kts

File renamed without changes.

examples/kotlin-dataframe-plugin-example/gradle.properties renamed to examples/kotlin-dataframe-plugin-gradle-example/gradle.properties

File renamed without changes.

examples/kotlin-dataframe-plugin-example/gradle/wrapper/gradle-wrapper.jar renamed to examples/kotlin-dataframe-plugin-gradle-example/gradle/wrapper/gradle-wrapper.jar

File renamed without changes.

examples/kotlin-dataframe-plugin-example/gradle/wrapper/gradle-wrapper.properties renamed to examples/kotlin-dataframe-plugin-gradle-example/gradle/wrapper/gradle-wrapper.properties

File renamed without changes.

examples/kotlin-dataframe-plugin-example/settings.gradle.kts renamed to examples/kotlin-dataframe-plugin-gradle-example/settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ pluginManagement {
88
plugins {
99
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
1010
}
11-
rootProject.name = "kotlin-dataframe-plugin-example"
11+
rootProject.name = "kotlin-dataframe-plugin-gradle-example"

examples/kotlin-dataframe-plugin-example/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/plugin/Main.kt renamed to examples/kotlin-dataframe-plugin-gradle-example/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/plugin/Main.kt

File renamed without changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
.kotlin
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
16+
### Eclipse ###
17+
.apt_generated
18+
.classpath
19+
.factorypath
20+
.project
21+
.settings
22+
.springBeans
23+
.sts4-cache
24+
25+
### NetBeans ###
26+
/nbproject/private/
27+
/nbbuild/
28+
/dist/
29+
/nbdist/
30+
/.nb-gradle/
31+
build/
32+
!**/src/main/**/build/
33+
!**/src/test/**/build/
34+
35+
### VS Code ###
36+
.vscode/
37+
38+
### Mac OS ###
39+
.DS_Store
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>dataframe_maven</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<kotlin.code.style>official</kotlin.code.style>
14+
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
15+
</properties>
16+
17+
<repositories>
18+
<repository>
19+
<id>mavenCentral</id>
20+
<url>https://repo1.maven.org/maven2/</url>
21+
</repository>
22+
</repositories>
23+
24+
<build>
25+
<sourceDirectory>src/main/kotlin</sourceDirectory>
26+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.jetbrains.kotlin</groupId>
30+
<artifactId>kotlin-maven-plugin</artifactId>
31+
<version>2.3.0-RC</version>
32+
<configuration>
33+
<compilerPlugins>
34+
<plugin>kotlin-dataframe</plugin>
35+
</compilerPlugins>
36+
</configuration>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.jetbrains.kotlin</groupId>
41+
<artifactId>kotlin-maven-dataframe</artifactId>
42+
<version>2.3.0-RC</version>
43+
</dependency>
44+
</dependencies>
45+
<executions>
46+
<execution>
47+
<id>compile</id>
48+
<phase>compile</phase>
49+
<goals>
50+
<goal>compile</goal>
51+
</goals>
52+
</execution>
53+
<execution>
54+
<id>test-compile</id>
55+
<phase>test-compile</phase>
56+
<goals>
57+
<goal>test-compile</goal>
58+
</goals>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
<plugin>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>2.22.2</version>
65+
</plugin>
66+
<plugin>
67+
<artifactId>maven-failsafe-plugin</artifactId>
68+
<version>2.22.2</version>
69+
</plugin>
70+
<plugin>
71+
<groupId>org.codehaus.mojo</groupId>
72+
<artifactId>exec-maven-plugin</artifactId>
73+
<version>1.6.0</version>
74+
<configuration>
75+
<mainClass>MainKt</mainClass>
76+
</configuration>
77+
</plugin>
78+
</plugins>
79+
</build>
80+
81+
<dependencies>
82+
<dependency>
83+
<groupId>org.jetbrains.kotlin</groupId>
84+
<artifactId>kotlin-test-junit5</artifactId>
85+
<version>2.3.0-RC</version>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.junit.jupiter</groupId>
90+
<artifactId>junit-jupiter</artifactId>
91+
<version>5.10.0</version>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.jetbrains.kotlin</groupId>
96+
<artifactId>kotlin-stdlib</artifactId>
97+
<version>2.3.0-RC</version>
98+
</dependency>
99+
<!-- DataFrame and Kandy dependencies -->
100+
<dependency>
101+
<groupId>org.jetbrains.kotlinx</groupId>
102+
<artifactId>dataframe</artifactId>
103+
<version>1.0.0-Beta4</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.jetbrains.kotlinx</groupId>
107+
<artifactId>kandy-lets-plot</artifactId>
108+
<version>0.8.3</version>
109+
</dependency>
110+
</dependencies>
111+
112+
</project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.jetbrains.kotlinx.dataframe.examples.plugin
2+
3+
import org.jetbrains.kotlinx.dataframe.DataFrame
4+
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
5+
import org.jetbrains.kotlinx.dataframe.api.add
6+
import org.jetbrains.kotlinx.dataframe.api.aggregate
7+
import org.jetbrains.kotlinx.dataframe.api.convert
8+
import org.jetbrains.kotlinx.dataframe.api.convertTo
9+
import org.jetbrains.kotlinx.dataframe.api.filter
10+
import org.jetbrains.kotlinx.dataframe.api.groupBy
11+
import org.jetbrains.kotlinx.dataframe.api.into
12+
import org.jetbrains.kotlinx.dataframe.api.max
13+
import org.jetbrains.kotlinx.dataframe.api.rename
14+
import org.jetbrains.kotlinx.dataframe.api.renameToCamelCase
15+
import org.jetbrains.kotlinx.dataframe.api.with
16+
import org.jetbrains.kotlinx.dataframe.io.readCsv
17+
import org.jetbrains.kotlinx.dataframe.io.writeCsv
18+
import org.jetbrains.kotlinx.kandy.dsl.plot
19+
import org.jetbrains.kotlinx.kandy.letsplot.export.save
20+
import org.jetbrains.kotlinx.kandy.letsplot.feature.layout
21+
import org.jetbrains.kotlinx.kandy.letsplot.layers.bars
22+
import java.net.URL
23+
24+
// Declare data schema for the DataFrame from jetbrains_repositories.csv.
25+
@DataSchema
26+
data class Repositories(
27+
val full_name: String,
28+
val html_url: URL,
29+
val stargazers_count: Int,
30+
val topics: String,
31+
val watchers: Int,
32+
)
33+
34+
// Define kinds of repositories.
35+
enum class RepoKind {
36+
Kotlin,
37+
IntelliJ,
38+
Other,
39+
}
40+
41+
// A rule for determining the kind of repository based on its name and topics.
42+
fun getKind(fullName: String, topics: List<String>): RepoKind {
43+
fun checkContains(name: String) = name in topics || fullName.lowercase().contains(name)
44+
45+
return when {
46+
checkContains("kotlin") -> RepoKind.Kotlin
47+
checkContains("idea") || checkContains("intellij") -> RepoKind.IntelliJ
48+
else -> RepoKind.Other
49+
}
50+
}
51+
52+
fun main() {
53+
val repos = DataFrame
54+
// Read DataFrame from the CSV file.
55+
.readCsv("https://raw.githubusercontent.com/Kotlin/dataframe/master/data/jetbrains_repositories.csv")
56+
// And convert it to match the `Repositories` schema.
57+
.convertTo<Repositories>()
58+
59+
// With Compiler Plugin, the DataFrame schema changes immediately after each operation:
60+
// For example, if a new column is added or the old one is renamed (or its type is changed)
61+
// during the operation, you can use the new name immediately in the following operations:
62+
repos
63+
// Add a new "name" column...
64+
.add("name") { full_name.substringAfterLast("/") }
65+
// ... and now we can use "name" extension in DataFrame operations, such as `filter`.
66+
.filter { name.lowercase().contains("kotlin") }
67+
68+
// Let's update the DataFrame with some operations using these features.
69+
val reposUpdated = repos
70+
// Rename columns to CamelCase.
71+
// Note that after that, in the following operations, extension properties will have
72+
// new names corresponding to the column names.
73+
.renameToCamelCase()
74+
// Rename "stargazersCount" column to "stars".
75+
.rename { stargazersCount }.into("stars")
76+
// And we can immediately use the updated name in the filtering.
77+
.filter { stars > 50 }
78+
// Convert values in the "topic" column (which were `String` initially)
79+
// to the list of topics.
80+
.convert { topics }.with {
81+
val inner = it.removeSurrounding("[", "]")
82+
if (inner.isEmpty()) emptyList() else inner.split(',').map(String::trim)
83+
}
84+
// Now "topics" is a `List<String>` column.
85+
// Add a new column with the number of topics.
86+
.add("topicCount") { topics.size }
87+
// Add a new column with the kind of repository.
88+
.add("kind") { getKind(fullName, topics) }
89+
90+
// Write the updated DataFrame to a CSV file.
91+
reposUpdated.writeCsv("jetbrains_repositories_new.csv")
92+
93+
reposUpdated
94+
// Group repositories by kind
95+
.groupBy { kind }
96+
// And then compute the maximum stars in each group.
97+
.aggregate {
98+
max { stars } into "maxStars"
99+
}
100+
// Build a bar plot showing the maximum number of stars per repository kind.
101+
.plot {
102+
bars {
103+
x(kind)
104+
y(maxStars)
105+
}
106+
layout.title = "Max stars per repo kind"
107+
}
108+
// Save the plot to an SVG file.
109+
.save("kindToStars.svg")
110+
}

0 commit comments

Comments
 (0)