当前位置:主页 > python教程 > spark编程python实例

spark编程python实例解读

发布:2023-04-04 08:05:01 59


给网友们整理相关的编程文章,网友崔涵映根据主题投稿了本篇教程内容,涉及到spark编程、python实例、spark编程python实例、spark编程python实例相关内容,已被545网友关注,涉猎到的知识点内容可以在下方电子书获得。

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

ubuntu-spark-python-notebook1

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

sparkcode-saveaspy

2.在shell中提交应用

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

sparkcode-spark-submit

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  at /usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.py:288

spark-python-error-scstop

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[ ]:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持码农之家。


相关文章

  • Python实例化class的执行顺序实例方法

    发布:2019-11-07

    这篇文章主要给大家介绍了关于Python中实例化class的执行顺序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们随着小编来一起


  • python自动扫雷实例分享

    发布:2020-01-19

    用python实现扫雷,非常有意思,这篇文章主要给大家介绍了关于python实现自动扫雷的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友


  • 朴素贝叶斯Python实例及解析

    发布:2023-01-03

    给大家整理一篇关于Python的教程,这篇文章主要为大家详细介绍了朴素贝叶斯Python算法实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下


网友讨论