|
1 | 1 | ---
|
| 2 | +apiVersion: v1 |
| 3 | +kind: ConfigMap |
| 4 | +metadata: |
| 5 | + name: spark-connect-client |
| 6 | +data: |
| 7 | + example.py: |- |
| 8 | + import sys |
| 9 | + |
| 10 | + from pyspark.sql import SparkSession |
| 11 | + import pyspark.sql.functions as fn |
| 12 | + |
| 13 | + if __name__ == "__main__": |
| 14 | + remote: str = sys.argv[1] |
| 15 | + |
| 16 | + print(f"Connecting to Spark Connect server at {remote}") |
| 17 | + |
| 18 | + # Adding s3a configuration properties here has no effect unfortunately. |
| 19 | + # They need to be set in the SparkConnectServer. |
| 20 | + spark = ( |
| 21 | + SparkSession.builder.appName("SimpleSparkConnectApp") |
| 22 | + .remote(remote) |
| 23 | + .getOrCreate() |
| 24 | + ) |
| 25 | + |
| 26 | + logFile = "/stackable/spark/README.md" |
| 27 | + |
| 28 | + print(f"Reading log file: {logFile}") |
| 29 | + logData = spark.read.text(logFile).cache() |
| 30 | + |
| 31 | + print("Counting words in log file") |
| 32 | + wc = ( |
| 33 | + logData.select( |
| 34 | + fn.explode(fn.split(logData["value"], r"\s+")) |
| 35 | + .alias("words")) |
| 36 | + .groupBy("words").count() |
| 37 | + ) |
| 38 | + |
| 39 | + wc.show() |
| 40 | + |
| 41 | + dest = "s3a://mybucket/wordcount" |
| 42 | + print(f"Writing word count to S3 {dest}") |
| 43 | + wc.write.mode("overwrite").parquet(dest) |
| 44 | + |
| 45 | + spark.stop() |
| 46 | +--- |
2 | 47 | apiVersion: batch/v1
|
3 | 48 | kind: Job
|
4 | 49 | metadata:
|
|
21 | 66 | command:
|
22 | 67 | [
|
23 | 68 | "/usr/bin/python",
|
24 |
| - "/stackable/spark-connect-examples/python/simple-connect-app.py", |
| 69 | + "/app/example.py", |
25 | 70 | "sc://spark-connect-server-default",
|
26 | 71 | ]
|
27 | 72 | resources:
|
|
31 | 76 | requests:
|
32 | 77 | cpu: 200m
|
33 | 78 | memory: 128Mi
|
| 79 | + env: |
| 80 | + - name: AWS_ACCESS_KEY_ID |
| 81 | + valueFrom: |
| 82 | + secretKeyRef: |
| 83 | + name: s3-credentials |
| 84 | + key: accessKey |
| 85 | + - name: AWS_SECRET_ACCESS_KEY |
| 86 | + valueFrom: |
| 87 | + secretKeyRef: |
| 88 | + name: s3-credentials |
| 89 | + key: secretKey |
| 90 | + volumeMounts: |
| 91 | + - name: spark-connect-client |
| 92 | + mountPath: /app |
| 93 | + volumes: |
| 94 | + - name: spark-connect-client |
| 95 | + configMap: |
| 96 | + name: spark-connect-client |
0 commit comments