Condividi tramite


Usare la shell Spark con Databricks Connect per Python

Nota

Questo articolo illustra Databricks Connect per Databricks Runtime 13.3 LTS e versioni successive.

Questo articolo illustra come usare Databricks Connect per Python e la shell Spark. Databricks Connect consente di connettere le applicazioni più diffuse al calcolo di Azure Databricks. Vedere Che cos'è Databricks Connect?.

Nota

Prima di iniziare a usare Databricks Connect, è necessario configurare il client Databricks Connect.

La shell Di Spark funziona solo con l'autenticazione con token di accesso personale di Azure Databricks.

Per usare Databricks Connect with the Spark shell and Python (Connettersi con la shell Spark e Python), seguire queste istruzioni.

  1. Per avviare la shell Spark e connetterla al cluster in esecuzione, eseguire uno dei comandi seguenti dall'ambiente virtuale Python attivato:

    Se è stata impostata la SPARK_REMOTE variabile di ambiente, eseguire il comando seguente:

    pyspark
    

    Se non è stata impostata la SPARK_REMOTE variabile di ambiente, eseguire invece il comando seguente:

    pyspark --remote "sc://<workspace-instance-name>:443/;token=<access-token-value>;x-databricks-cluster-id=<cluster-id>"
    

    Viene visualizzata la shell Spark, ad esempio:

    Python 3.10 ...
    [Clang ...] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    Welcome to
         ____              __
        / __/__  ___ _____/ /__
       _\ \/ _ \/ _ `/ __/  '_/
      /__ / .__/\_,_/_/ /_/\_\   version 13.x.dev0
         /_/
    
    Using Python version 3.10 ...
    Client connected to the Spark Connect server at sc://...:.../;token=...;x-databricks-cluster-id=...
    SparkSession available as 'spark'.
    >>>
    

Eseguire ora un semplice comando PySpark, ad esempio spark.range(1,10).show(). Se non sono presenti errori, la connessione è stata completata correttamente.

  1. Per informazioni su come usare la shell Spark con Python per eseguire comandi nel calcolo, vedere Interactive Analysis with the Spark Shell with Python (Analisi interattiva con Spark Shell con Spark Shell).

    Usare la variabile predefinita spark per rappresentare nel SparkSession cluster in esecuzione, ad esempio:

    >>> df = spark.read.table("samples.nyctaxi.trips")
    >>> df.show(5)
    +--------------------+---------------------+-------------+-----------+----------+-----------+
    |tpep_pickup_datetime|tpep_dropoff_datetime|trip_distance|fare_amount|pickup_zip|dropoff_zip|
    +--------------------+---------------------+-------------+-----------+----------+-----------+
    | 2016-02-14 16:52:13|  2016-02-14 17:16:04|         4.94|       19.0|     10282|      10171|
    | 2016-02-04 18:44:19|  2016-02-04 18:46:00|         0.28|        3.5|     10110|      10110|
    | 2016-02-17 17:13:57|  2016-02-17 17:17:55|          0.7|        5.0|     10103|      10023|
    | 2016-02-18 10:36:07|  2016-02-18 10:41:45|          0.8|        6.0|     10022|      10017|
    | 2016-02-22 14:14:41|  2016-02-22 14:31:52|         4.51|       17.0|     10110|      10282|
    +--------------------+---------------------+-------------+-----------+----------+-----------+
    only showing top 5 rows
    

    Tutto il codice Python viene eseguito localmente, mentre tutto il codice PySpark che coinvolge le operazioni del dataframe viene eseguito nel cluster nell'area di lavoro remota di Azure Databricks e le risposte di esecuzione vengono inviate al chiamante locale.

  2. Per arrestare la shell spark, premere Ctrl + d o Ctrl + zoppure eseguire il comando quit() o exit().