Skip to content

Commit

Permalink
add flink example
Browse files Browse the repository at this point in the history
  • Loading branch information
paulk-asert committed May 23, 2024
1 parent 1a24aed commit 0afa285
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ commonsIoVersion = 2.11.0
commonsMath3Version = 3.6.1
commonsMath4Version = 4.0-beta1
djlVersion = 0.28.0
flinkVersion = 1.18.1
flinkMlVersion = 2.3.0
flinkStatefunVersion = 3.2.0
groovy3Version = 3.0.21
groovy4Version = 4.0.21
igniteVersion = 2.16.0
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def subprojects = [
'LanguageProcessingSparkNLP',
'Mnist',
'Whiskey',
'WhiskeyFlink',
'WhiskeyIgnite',
'WhiskeySpark',
'WhiskeyWayang'
Expand Down
55 changes: 55 additions & 0 deletions subprojects/WhiskeyFlink/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply plugin: 'groovy'
apply plugin: 'application'

ext.appName = 'WhiskeyFlink'

application {
mainClass = appName
}

tasks.named('run').configure {
description = "Run $appName as a JVM application/Groovy script"
}

dependencies {
implementation "org.apache.groovy:groovy:$groovy4Version"
// implementation project(':ChartUtil')
implementation("org.apache.flink:statefun-flink-core:$flinkStatefunVersion") {
exclude(group: 'org.apache.flink', module: 'flink-streaming-java_2.12')
exclude(group: 'org.apache.flink', module: 'flink-metrics-dropwizard')
}
implementation "org.apache.flink:flink-ml-uber-1.17:$flinkMlVersion"
// implementation "org.apache.flink:flink-csv:$flinkVersion"
implementation "org.apache.flink:flink-table-runtime:$flinkVersion"
implementation "org.apache.flink:flink-connector-files:$flinkVersion"
runtimeOnly "org.apache.flink:flink-table-api-java-bridge:$flinkVersion"
runtimeOnly "org.apache.flink:flink-table-planner-loader:$flinkVersion"
runtimeOnly "org.apache.flink:flink-clients:$flinkVersion"
runtimeOnly "org.slf4j:slf4j-simple:$slf4jVersion"
// implementation "org.apache.commons:commons-csv:$commonsCsvVersion"
}

tasks.register('versionInfo') {
doLast {
File javaHome = new File(System.getProperty('java.home'))
logger.lifecycle "Using Java from $javaHome (version ${System.getProperty('java.version')})"
}
}

run.dependsOn versionInfo
69 changes: 69 additions & 0 deletions subprojects/WhiskeyFlink/src/main/groovy/WhiskeyFlink.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import groovy.transform.CompileStatic
import org.apache.flink.api.common.eventtime.WatermarkStrategy
import org.apache.flink.api.common.functions.FlatMapFunction
import org.apache.flink.connector.file.src.FileSource
import org.apache.flink.connector.file.src.reader.TextLineInputFormat
import org.apache.flink.core.fs.Path
import org.apache.flink.ml.clustering.kmeans.KMeans
import org.apache.flink.ml.clustering.kmeans.KMeansModelData
import org.apache.flink.ml.linalg.DenseVector
import org.apache.flink.ml.linalg.Vectors
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment
import org.apache.flink.types.Row
import org.apache.flink.util.CloseableIterator
import org.apache.flink.util.Collector

@CompileStatic
class WhiskeyFlink {
static FlatMapFunction<String, DenseVector> splitAndChop = (String s, Collector out) ->
out.collect(Vectors.dense(s.split(',')[2..-1] as double[]))
static headerSkipped = false

static void main(args) {
var file = WhiskeyFlink.classLoader.getResource('whiskey.csv').file
var eEnv = StreamExecutionEnvironment.executionEnvironment
var tEnv = StreamTableEnvironment.create(eEnv)

var source = FileSource.forRecordStreamFormat(new TextLineInputFormat(), new Path(file)).build()
var stream = eEnv
.fromSource(source, WatermarkStrategy.noWatermarks(), "csvfile")
.filter(s -> { var saved = headerSkipped; headerSkipped = true; saved })
.flatMap(splitAndChop)

var inputTable = tEnv.fromDataStream(stream).limit(1, 86).as("features")

var kmeans = new KMeans(k: 3, seed: 1L)

var kmeansModel = kmeans.fit(inputTable)

var outputTable = kmeansModel.transform(inputTable)[0]

var clusters = [:].withDefault{ [] }
outputTable.execute().collect().each { row ->
var features = (DenseVector) row.getField(kmeans.featuresCol)
int clusterId = (Integer) row.getField(kmeans.predictionCol)
clusters[clusterId] << features
}
clusters.each { k, v ->
println "Cluster ID: $k"
println "Features list:\n${v.join('\n')}"
}
}
}
87 changes: 87 additions & 0 deletions subprojects/WhiskeyFlink/src/main/resources/whiskey.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
RowID,Distillery,Body,Sweetness,Smoky,Medicinal,Tobacco,Honey,Spicy,Winey,Nutty,Malty,Fruity,Floral
01,Aberfeldy,2,2,2,0,0,2,1,2,2,2,2,2
02,Aberlour,3,3,1,0,0,4,3,2,2,3,3,2
03,AnCnoc,1,3,2,0,0,2,0,0,2,2,3,2
04,Ardbeg,4,1,4,4,0,0,2,0,1,2,1,0
05,Ardmore,2,2,2,0,0,1,1,1,2,3,1,1
06,ArranIsleOf,2,3,1,1,0,1,1,1,0,1,1,2
07,Auchentoshan,0,2,0,0,0,1,1,0,2,2,3,3
08,Auchroisk,2,3,1,0,0,2,1,2,2,2,2,1
09,Aultmore,2,2,1,0,0,1,0,0,2,2,2,2
10,Balblair,2,3,2,1,0,0,2,0,2,1,2,1
11,Balmenach,4,3,2,0,0,2,1,3,3,0,1,2
12,Belvenie,3,2,1,0,0,3,2,1,0,2,2,2
13,BenNevis,4,2,2,0,0,2,2,0,2,2,2,2
14,Benriach,2,2,1,0,0,2,2,0,0,2,3,2
15,Benrinnes,3,2,2,0,0,3,1,1,2,3,2,2
16,Benromach,2,2,2,0,0,2,2,1,2,2,2,2
17,Bladnoch,1,2,1,0,0,0,1,1,0,2,2,3
18,BlairAthol,2,2,2,0,0,1,2,2,2,2,2,2
19,Bowmore,2,2,3,1,0,2,2,1,1,1,1,2
20,Bruichladdich,1,1,2,2,0,2,2,1,2,2,2,2
21,Bunnahabhain,1,2,1,1,0,1,1,1,1,2,2,3
22,Caol Ila,3,1,4,2,1,0,2,0,2,1,1,1
23,Cardhu,1,3,1,0,0,1,1,0,2,2,2,2
24,Clynelish,3,2,3,3,1,0,2,0,1,1,2,0
25,Craigallechie,2,2,2,0,1,2,2,1,2,2,1,4
26,Craigganmore,2,3,2,1,0,0,1,0,2,2,2,2
27,Dailuaine,4,2,2,0,0,1,2,2,2,2,2,1
28,Dalmore,3,2,2,1,0,1,2,2,1,2,3,1
29,Dalwhinnie,2,2,2,0,0,2,1,0,1,2,2,2
30,Deanston,2,2,1,0,0,2,1,1,1,3,2,1
31,Dufftown,2,3,1,1,0,0,0,0,1,2,2,2
32,Edradour,2,3,1,0,0,2,1,1,4,2,2,2
33,GlenDeveronMacduff,2,3,1,1,1,1,1,2,0,2,0,1
34,GlenElgin,2,3,1,0,0,2,1,1,1,1,2,3
35,GlenGarioch,2,1,3,0,0,0,3,1,0,2,2,2
36,GlenGrant,1,2,0,0,0,1,0,1,2,1,2,1
37,GlenKeith,2,3,1,0,0,1,2,1,2,1,2,1
38,GlenMoray,1,2,1,0,0,1,2,1,2,2,2,4
39,GlenOrd,3,2,1,0,0,1,2,1,1,2,2,2
40,GlenScotia,2,2,2,2,0,1,0,1,2,2,1,1
41,GlenSpey,1,3,1,0,0,0,1,1,1,2,0,2
42,Glenallachie,1,3,1,0,0,1,1,0,1,2,2,2
43,Glendronach,4,2,2,0,0,2,1,4,2,2,2,0
44,Glendullan,3,2,1,0,0,2,1,2,1,2,3,2
45,Glenfarclas,2,4,1,0,0,1,2,3,2,3,2,2
46,Glenfiddich,1,3,1,0,0,0,0,0,0,2,2,2
47,Glengoyne,1,2,0,0,0,1,1,1,2,2,3,2
48,Glenkinchie,1,2,1,0,0,1,2,0,0,2,2,2
49,Glenlivet,2,3,1,0,0,2,2,2,1,2,2,3
50,Glenlossie,1,2,1,0,0,1,2,0,1,2,2,2
51,Glenmorangie,2,2,1,1,0,1,2,0,2,1,2,2
52,Glenrothes,2,3,1,0,0,1,1,2,1,2,2,0
53,Glenturret,2,3,1,0,0,2,2,2,2,2,1,2
54,Highland Park,2,2,3,1,0,2,1,1,1,2,1,1
55,Inchgower,1,3,1,1,0,2,2,0,1,2,1,2
56,Isle of Jura,2,1,2,2,0,1,1,0,2,1,1,1
57,Knochando,2,3,1,0,0,2,2,1,2,1,2,2
58,Lagavulin,4,1,4,4,1,0,1,2,1,1,1,0
59,Laphroig,4,2,4,4,1,0,0,1,1,1,0,0
60,Linkwood,2,3,1,0,0,1,1,2,0,1,3,2
61,Loch Lomond,1,1,1,1,0,1,1,0,1,2,1,2
62,Longmorn,3,2,1,0,0,1,1,1,3,3,2,3
63,Macallan,4,3,1,0,0,2,1,4,2,2,3,1
64,Mannochmore,2,1,1,0,0,1,1,1,2,1,2,2
65,Miltonduff,2,4,1,0,0,1,0,0,2,1,1,2
66,Mortlach,3,2,2,0,0,2,3,3,2,1,2,2
67,Oban,2,2,2,2,0,0,2,0,2,2,2,0
68,OldFettercairn,1,2,2,0,1,2,2,1,2,3,1,1
69,OldPulteney,2,1,2,2,1,0,1,1,2,2,2,2
70,RoyalBrackla,2,3,2,1,1,1,2,1,0,2,3,2
71,RoyalLochnagar,3,2,2,0,0,2,2,2,2,2,3,1
72,Scapa,2,2,1,1,0,2,1,1,2,2,2,2
73,Speyburn,2,4,1,0,0,2,1,0,0,2,1,2
74,Speyside,2,2,1,0,0,1,0,1,2,2,2,2
75,Springbank,2,2,2,2,0,2,2,1,2,1,0,1
76,Strathisla,2,2,1,0,0,2,2,2,3,3,3,2
77,Strathmill,2,3,1,0,0,0,2,0,2,1,3,2
78,Talisker,4,2,3,3,0,1,3,0,1,2,2,0
79,Tamdhu,1,2,1,0,0,2,0,1,1,2,2,2
80,Tamnavulin,1,3,2,0,0,0,2,0,2,1,2,3
81,Teaninich,2,2,2,1,0,0,2,0,0,0,2,2
82,Tobermory,1,1,1,0,0,1,0,0,1,2,2,2
83,Tomatin,2,3,2,0,0,2,2,1,1,2,0,1
84,Tomintoul,0,3,1,0,0,2,2,1,1,2,1,2
85,Tomore,2,2,1,0,0,1,0,1,2,1,0,0
86,Tullibardine,2,3,0,0,1,0,2,1,1,2,2,1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source: https://www.niss.org/sites/default/files/ScotchWhisky01.txt

0 comments on commit 0afa285

Please sign in to comment.