การเชื่อมต่อกับ Spark

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

การโต้ตอบกับ Spark

โลโก้ของ Java, Scala, Python และ R

ภาษาที่ใช้โต้ตอบกับ Spark

  • Java — ระดับต่ำ, คอมไพล์
  • Scala, Python และ R — ระดับสูงพร้อม interactive REPL
Machine Learning with PySpark

การนำเข้า pyspark

นำเข้าโมดูล pyspark จาก Python

import pyspark

ตรวจสอบเวอร์ชันของโมดูล pyspark

pyspark.__version__
'2.4.1'
Machine Learning with PySpark

Sub-modules

นอกจาก pyspark แล้ว ยังมี

Machine Learning with PySpark

Spark URL

Remote Cluster ใช้ Spark URL — spark://<IP address | DNS name>:<port>

ตัวอย่าง:

  • spark://13.59.151.161:7077

Local Cluster

ตัวอย่าง:

  • local — 1 core เท่านั้น;
  • local[4] — 4 cores; หรือ
  • local[*] — ใช้ทุก core ที่มี
Machine Learning with PySpark

การสร้าง SparkSession

from pyspark.sql import SparkSession

สร้าง local cluster ด้วย SparkSession builder

spark = SparkSession.builder \
                    .master('local[*]') \
                    .appName('first_spark_application') \
                    .getOrCreate()

โต้ตอบกับ Spark...

# Close connection to Spark
>>> spark.stop()
Machine Learning with PySpark

มาเชื่อมต่อกับ Spark กันเถอะ!

Machine Learning with PySpark

Preparing Video For Download...