简体   繁体   English

我如何使用 Spark 上下文?

[英]How can i use Spark Context?

I assign the value as sc = pyspark.SparkContext().我将值分配为 sc = pyspark.SparkContext()。 It run and doesnt respond for so long on jupyter notebook as asteric sign appears and doesnt show any error or so.它在 jupyter notebook 上运行并没有响应很长时间,因为星号出现并且没有显示任何错误左右。

I tried sc = SparkContext()我试过sc = SparkContext()

import pyspark
import os
from pyspark import SparkContext, SparkConf
sc = pyspark.SparkContext()  # At this part it don't respond
from pyspark.sql import SQLContext
sqlc = SQLContext(sc)

It should go on.它应该继续。

For Python,对于 Python,

from pyspark import SparkContext
sc = SparkContext(appName = "test")

But since you're working on pyspark version 2+ , you dont need to initialize spark context.但是由于您正在使用 pyspark version 2+ ,因此您不需要初始化 spark 上下文。 You can create a spark session and directly work on it.您可以创建一个 spark 会话并直接对其进行处理。

SPARK 2.0.0 onwards, SparkSession provides a single point of entry to interact with underlying Spark functionality and allows programming Spark with DataFrame and Dataset APIs.从 SPARK 2.0.0 开始,SparkSession 提供了与底层 Spark 功能交互的单一入口点,并允许使用 DataFrame 和 Dataset API 对 Spark 进行编程。 All the functionality available with sparkContext are also available in sparkSession. sparkContext 提供的所有功能在 sparkSession 中也可用。

In order to use APIs of SQL, HIVE, and Streaming, no need to create separate contexts as sparkSession includes all the APIs.为了使用 SQL、HIVE 和 Streaming 的 API,无需创建单独的上下文,因为 sparkSession 包含所有 API。

To configure a spark session,要配置 spark 会话,

session = SparkSession.builder.getOrCreate()

尝试以下导入: from pyspark import *之后您可以像这样使用它:

sc = SparkContext()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM