spark编程python实例

spark编程python实例

ValueError: Cannot run multiple SparkContexts at once; existing SparkContext(app=PySparkShell, master=local[])

1.pyspark在jupyter notebook中开发,测试,提交

1.1.启动

IPYTHON_OPTS="notebook" /opt/spark/bin/pyspark

下载应用,将应用下载为.py文件(默认notebook后缀是.ipynb)

在shell中提交应用

wxl@wxl-pc:/opt/spark/bin$ spark-submit /bin/spark-submit /home/wxl/Downloads/pysparkdemo.py

!

3.遇到的错误及解决

ValueError: Cannot run multiple SparkContexts at once; existing SparkContext(app=PySparkShell, master=local[*]) d*

3.1.错误

ValueError: Cannot run multiple SparkContexts at once; existing SparkContext(app=PySparkShell, master=local[*]) d*

ValueError: Cannot run multiple SparkContexts at once; existing SparkContext(app=PySparkShell, master=local[*]) created by <module> at /usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.py:288

3.2.解决,成功运行

在from之后添加

try:
    sc.stop()
except:
    pass
sc=SparkContext('local[2]','First Spark App')

贴上错误解决方法来源StackOverFlow

4.源码

pysparkdemo.ipynb

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pyspark import SparkContext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    sc.stop()\n",
    "except:\n",
    "    pass\n",
    "sc=SparkContext('local[2]','First Spark App')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = sc.textFile(\"data/UserPurchaseHistory.csv\").map(lambda line: line.split(\",\")).map(lambda record: (record[0], record[1], record[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total purchases: 5\n"
     ]
    }
   ],
   "source": [
    "numPurchases = data.count()\n",
    "print \"Total purchases: %d\" % numPurchases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

pysparkdemo.py

# coding: utf-8

# In[1]:

from pyspark import SparkContext


# In[2]:

try:
    sc.stop()
except:
    pass
sc=SparkContext('local[2]','First Spark App')


# In[3]:

data = sc.textFile("data/UserPurchaseHistory.csv").map(lambda line: line.split(",")).map(lambda record: (record[0], record[1], record[2]))


# In[4]:

numPurchases = data.count()
print "Total purchases: %d" % numPurchases


# In[ ]:

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏有困难要上,没有困难创造困难也要上!

Kettle 7.1 连接Hadoop集群

在Tools -> Hadoop Distribution 中选择 “HortonWorks HDP 2.5.x”。

56220
来自专栏挖掘大数据

如何搭建Hadoop伪分布式集群?

如何搭建Hadoop伪分布式集群,本文将详细介绍。

29000
来自专栏Hadoop实操

CentOS6.5安装CDH5.13

Cloudera前一段时间发布了CDH5.13版本,5.13的新功能可以参考前一篇文章CDH5.13和CM5.13的新功能,本文章主要讲述如何在CentOS6....

1.6K80
来自专栏我是攻城师

CDH-Hadoop2.6+ Apache Pig0.15安装记录

37250
来自专栏Hadoop实操

如何使用Cloudera Manager启用HDFS的HA

在HDFS集群中NameNode存在单点故障(SPOF),对于只有一个NameNode的集群,如果NameNode机器出现意外,将导致整个集群无法使用。为了解决...

1.5K40
来自专栏分布式系统和大数据处理

使用.net通过odbc访问Hive

在 写入数据到Hive表(命令行) 这篇文章中,我们通过命令行的方式和hive进行了交互。但在通常情况下,是通过编程的方式来操作Hive,Hive提供了JDBC...

51040
来自专栏蓝天

Hadoop-2.8.0分布式安装手册

10.12.154.79: Error: JAVA_HOME is not set and could not be found.

65120
来自专栏Hadoop实操

如何在Hue中添加Spark Notebook

CDH集群中可以使用Hue访问Hive、Impala、HBase、Solr等,在Hue3.8版本后也提供了Notebook组件(支持R、Scala及python...

83630
来自专栏Hadoop实操

CDH5弃用的项目

18920
来自专栏Hadoop实操

如何在CDH中安装Kudu&Spark2&Kafka

在CDH的默认安装包中,是不包含Kafka,Kudu和Spark2的,需要单独下载特定的Parcel包才能安装相应服务。本文档主要描述在离线环境下,在CentO...

2.4K90

扫码关注云+社区

领取腾讯云代金券