You need to enable JavaScript to run this app.
E-MapReduce

E-MapReduce

Copy page
Download PDF
Serverless Spark jobs: based on Spark Connect
Connect to Spark Connect App and submit job
Copy page
Download PDF
Connect to Spark Connect App and submit job

Python Client

Get existing Spark Connect App

The following example demonstrates how to connect to an existing Spark Connect App and query its status information.

from emr_spark_connect import EMRSparkConnectSession, StaticCredentials
app = (
    EMRSparkConnectSession.builder
    .region("<region>")
    .credentials(StaticCredentials("<your-ak>", "<your-sk>"))
    .serverId("<connect-app-id>")
    .getOrCreateApp()
)
print(app.getServerId())
print(app.getStatus())
print(app.getTrackingUrl())

Create SparkSession

The following example demonstrates how to connect to a specified Spark Connect App, create a SparkSession, and execute an SQL query.

from emr_spark_connect import EMRSparkConnectSession, StaticCredentials

spark = (
    EMRSparkConnectSession.builder
    .region("<region>")
    .credentials(StaticCredentials("<your-ak>", "<your-sk>"))
    
    .serverId("<connect-app-id>")
    .getOrCreate()
)

spark.sql("select 'hello' as msg").show()
spark.stop()

Note

Parameter / Method

Required

Note

region("<region>")

Yes

Specifies region information.

credentials(StaticCredentials("<your-ak>", "<your-sk>"))

Yes

Specifies access credentials, including Access Key and Secret Key.

serverId("<connect-app-id>")

Required when connecting to an existing App

Specifies the unique identifier of the target Spark Connect App. Acquisition method:

  • Via console: In the Job instance list, locate the created Spark Connect App. The ID below is the unique identifier.

Image

  • Via SDK: When creating a Spark Connect App, after creation, use getServerId() for Python and getConnectId() for Java to request the App ID from the EMR Serverless Spark Connect server.

getOrCreateApp()

Yes

Create or get a Spark Connect App and return the App object.

getOrCreate()

Yes

Create a SparkSession and connect to the specified Spark Connect App.

app.getServerId()

No

Get Spark Connect App ID.

app.getStatus()

No

Get App status.

app.getTrackingUrl()

No

Get Tracking URL, typically used to navigate to the job/application details page or monitoring page.

Java Client

Get existing Spark Connect App

The following example demonstrates how to connect to an existing Spark Connect App and query its status information.

import com.volc.emr.spark.connect.EMRSparkConnectSession;
import com.volc.emr.spark.connect.EMRSparkConnectProvider;
import com.volc.emr.spark.connect.StaticCredentials;
import com.volc.emr.spark.connect.SparkApp;
import org.apache.spark.sql.SparkSession;

public class SparkConnectJavaExample {
    public static void main(String[] args) {
        SparkApp app = EMRSparkConnectSession.builder()
                .region("<region>")
                .credentials(new StaticCredentials("<your-ak>", "<your-sk>"))
                .serverId("<connect-app-id>")
                .getOrCreateApp();
        
        System.out.println(app.getConnectId());
        System.out.println(app.getStatus());
        System.out.println(app.getTrackingUrl());
    }
}

Create SparkSession

The following example demonstrates how to connect to a specified Spark Connect App, create a SparkSession, and execute an SQL query.

import com.volc.emr.spark.connect.EMRSparkConnectSession;
import com.volc.emr.spark.connect.StaticCredentials;
import org.apache.spark.sql.SparkSession;

public class SparkConnectJavaExample {
    public static void main(String[] args) {
        SparkSession spark = EMRSparkConnectSession.builder()
                .region("<region>")
                .credentials(new StaticCredentials("<your-ak>", "<your-sk>"))
                
                .serverId("<connect-app-id>")
                .getOrCreate();

        spark.sql("select 'hello' as msg").show();
        spark.stop();
    }
}

Note

Parameter / Method

Required

Note

region("<region>")

Yes

Specifies region information.

credentials(StaticCredentials("<your-ak>", "<your-sk>"))

Yes

Specifies access credentials, including Access Key and Secret Key.

serverId("<connect-app-id>")

Required when connecting to an existing App

Specifies the unique identifier of the target Spark Connect App. Acquisition method:

  • Via console: In the Job instance list, locate the created Spark Connect App. The ID below is the unique identifier.

Image

  • Via SDK: When creating a Spark Connect App, after creation, use getServerId() for Python and getConnectId() for Java to request the App ID from the EMR Serverless Spark Connect server.

getOrCreateApp()

Yes

Create or get a Spark Connect App and return the App object.

getOrCreate()

Yes

Create a SparkSession and connect to the specified Spark Connect App.

app.getServerId()

No

Get Spark Connect App ID.

app.getStatus()

No

Get App status.

app.getTrackingUrl()

No

Get Tracking URL, typically used to navigate to the job/application details page or monitoring page.

Last updated: 2026.08.14 16:24:26