Executing Spark Scala scripts inside running Spark Application

I have a Java application which performs some Spark pipeline operations e.g.:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class SparkJobExample {
public void run(...) {
var spark = SparkSession.builder()
.master("local[*]")
.getOrCreate();
//Load initial df
var initDf = spark.read()
.csv(<path>);
initDf.createOrReplaceTempView("initDf");
//Operation 1
firstDf = <some operations on initDf>
firstDf.createOrReplaceTempView("firstDf");
...
//Operation N
nDf = <some operations on previous dfs>
nDf.createOrReplaceTempView("nDf");
//Save result df...
}
}
</code>
<code>public class SparkJobExample { public void run(...) { var spark = SparkSession.builder() .master("local[*]") .getOrCreate(); //Load initial df var initDf = spark.read() .csv(<path>); initDf.createOrReplaceTempView("initDf"); //Operation 1 firstDf = <some operations on initDf> firstDf.createOrReplaceTempView("firstDf"); ... //Operation N nDf = <some operations on previous dfs> nDf.createOrReplaceTempView("nDf"); //Save result df... } } </code>
public class SparkJobExample {

    public void run(...) {

        var spark = SparkSession.builder()
                .master("local[*]")
                .getOrCreate();

        //Load initial df
        var initDf = spark.read()
                .csv(<path>);
        initDf.createOrReplaceTempView("initDf");
        
        //Operation 1
        firstDf = <some operations on initDf>
        firstDf.createOrReplaceTempView("firstDf");

        ...

        //Operation N
        nDf = <some operations on previous dfs>
        nDf.createOrReplaceTempView("nDf");
    
        //Save result df...
    }
}

It is working very good, but I would like to run some operations dynamically from Spark Script Scala provided as file dynamically, e.g.:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import org.apache.spark.sql.SparkSession
object DynamicSparkScript {
def main(args: Array[String]): Unit = {
// Get the same session which was initialized in regular java code
val spark = SparkSession.builder().getOrCreate()
// Read DataFrame which was created in regular java code and saved as temp view
val df = spark.table(<name of View saved in regular java code>)
val resultDf = df... //some operations on df read above
resultDf.createOrReplaceTempView("scriptRes")
}
</code>
<code>import org.apache.spark.sql.SparkSession object DynamicSparkScript { def main(args: Array[String]): Unit = { // Get the same session which was initialized in regular java code val spark = SparkSession.builder().getOrCreate() // Read DataFrame which was created in regular java code and saved as temp view val df = spark.table(<name of View saved in regular java code>) val resultDf = df... //some operations on df read above resultDf.createOrReplaceTempView("scriptRes") } </code>
import org.apache.spark.sql.SparkSession

object DynamicSparkScript {
  def main(args: Array[String]): Unit = {

    // Get the same session which was initialized in regular java code
    val spark = SparkSession.builder().getOrCreate()

    // Read DataFrame which was created in regular java code and saved as temp view
    val df = spark.table(<name of View saved in regular java code>)

    val resultDf = df... //some operations on df read above

    resultDf.createOrReplaceTempView("scriptRes")
  }

So for example after executing Operation N in regular java code, I would like to execute this Spark Scala script, which uses View saved in java code, performs some operations and saves result DF, which is later available from Java Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> //Java Code
//Operation N
nDf = <some operations on previous dfs>
nDf.createOrReplaceTempView("nDf");
//Read and execute scala script which creates View name "scriptRes"
var scalaScriptPath = <path to scala script file>
<execute scala script which saves DF as View named "scriptRes">
//Use DF created in script
var fromScriptDf = spark.table("scriptRes")
finalDf = fromScriptDf. ...<some operations>
//save final df
</code>
<code> //Java Code //Operation N nDf = <some operations on previous dfs> nDf.createOrReplaceTempView("nDf"); //Read and execute scala script which creates View name "scriptRes" var scalaScriptPath = <path to scala script file> <execute scala script which saves DF as View named "scriptRes"> //Use DF created in script var fromScriptDf = spark.table("scriptRes") finalDf = fromScriptDf. ...<some operations> //save final df </code>
        //Java Code

        //Operation N
        nDf = <some operations on previous dfs>
        nDf.createOrReplaceTempView("nDf");

        //Read and execute scala script which creates View name "scriptRes"
        var scalaScriptPath = <path to scala script file>

        <execute scala script which saves DF as View named "scriptRes">

        //Use DF created in script
        var fromScriptDf = spark.table("scriptRes")
        finalDf = fromScriptDf. ...<some operations>
  
        //save final df

If I understand correctly, there is no problem with passing DF like this, as it should be available within Spark Session.

However I can’t figure out how to perform <execute scala script which saves DF as View named "scriptRes"> fragment and run this Scala script correctly. I receive path to this file dynamically, once app is already running, its not available on startup. Please note that I can’t use SparkLauncher object inside the regular code.

I had two ideas:

  1. Packing this scala script inside jar and running this as another process from within java code. Seems like it could work, although this would require me to pack all Spark libs inside this jar, which would make it heavy and the whole action would be probably really slow (especially that there will be multiple different scripts)
  2. Using Dynamic Class Loading and Java Reflection API to generate this class at runtime and invoke its method.

Is there any dedicated functionality or better way to do this? I was thinking if I could just read code from file and somehow pass it to existing SparkSession but haven’t found anything.
If not, which solution is better in your opinion?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật