cuddly-jelly-27016
04/07/2025, 6:38 PMGenericSparkTask by configuring GenericSparkConf for given Flyte tasks
## How was this patch tested?
• Running the following workflow successfully locally and remotely
from flytekit import workflow, task, ImageSpec
from flytekitplugins.spark import GenericSparkConf
branch = "jan/generic_spark_task"
flytekit_spark_generic = f"git+https://github.com/flyteorg/flytekit.git@{branch}#subdirectory=plugins/flytekit-spark"
container_image = ImageSpec(
name="genericspark",
base_image="ghcr.io/fiedlernr9/sparkfun:CLADEqpZcRJqAgyCVmuVtg",
apt_packages=["git"],
packages=[flytekit_spark_generic],
copy=[
"javathings/WordCount/WordCount.jar",
],
registry="ghcr.io/fiedlernr9",
)
@task(
task_config=GenericSparkConf(
main_class="WordCount",
applications_path="/root/javathings/WordCount/WordCount.jar",
spark_conf={
"spark.driver.memory": "5000M",
"spark.executor.memory": "2000M",
"spark.executor.cores": "1",
"spark.executor.instances": "1",
"spark.driver.cores": "2",
},
),
container_image=container_image,
)
def foo(): ...
@workflow
def wf():
foo()
• where WordCount.jar is the following compiled java code:
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.SparkConf;
import scala.Tuple2;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class WordCount {
public static void main(String[] args) {
// Create a Spark Configuration and Context
SparkConf conf = new SparkConf().setAppName("WordCount"); //.setMaster();
JavaSparkContext sc = new JavaSparkContext(conf);
List<String> data = generateRandomList(100000);
// Create an RDD from the hardcoded list
JavaRDD<String> input = sc.parallelize(data);
// Split the lines into words
JavaRDD<String> words = input.flatMap(line -> Arrays.asList(line.split(" ")).iterator());
// Map each word to a tuple (word, 1)
JavaPairRDD<String, Integer> wordPairs = words.mapToPair(word -> new Tuple2<>(word, 1));
// Reduce by key (word) to count occurrences
JavaPairRDD<String, Integer> wordCounts = wordPairs.reduceByKey(Integer::sum);
// Print the results
wordCounts.foreach(wordCount -> System.out.println(wordCount._1() + " : " + wordCount._2()));
// Stop the Spark context
sc.stop();
}
// Method to generate a random list of sentences
public static List<String> generateRandomList(int size) {
String[] words = {"hello", "world", "java", "spark", "big", "data", "science", "cloud", "compute", "stream"};
Random random = new Random();
List<String> randomList = new ArrayList<>();
for (int i = 0; i < size; i++) {
String sentence = words[random.nextInt(words.length)] + " " + words[random.nextInt(words.length)];
randomList.add(sentence);
}
return randomList;
}
}
### Screenshots
[image](https://private-user-images.githubusercontent.com/89976021/431124032-544584fe-cb63-4cfd-abb3-128d812071b5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDQxMzA1MzYsIm5iZiI6MTc0NDEzMDIzNiwicGF0aCI6Ii84OTk3NjAyMS80MzExMjQwMzItNTQ0NTg0ZmUtY2I2My00Y2ZkLWFiYjMtMTI4ZDgxMjA3MWI1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTA0MDglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwNDA4VDE2MzcxNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQwZGI5YmI5NDNkZWE2MjBkZjcyZDBhYjZlZDk5ODVjZjc4ZDU4MzEzYzQzMDU1OGVmZTRhOWQwOTc0M2Q3MjgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.dMkWa8YmTyj7E2uDOcS7VH8J0eQH92XIDQ19sPoL5C4)
## Check all the applicable boxes
• I updated the documentation accordingly.
• All new and existing tests passed.
• All commits are signed-off.
## Docs link
### Summary by Bito
This pull request introduces a new generic spark task feature to the flytekit framework, defining configuration and execution logic for spark tasks with plugin registration. The update expands spark support by providing a robust task interface that integrates with the existing plugin ecosystem.
Unit tests added: False
Estimated effort to review (1-5, lower is better): 1
flyteorg/flytekitcuddly-jelly-27016
04/08/2025, 4:37 PM