Spark核心类:SQLContext和DataFrame
http://blog.csdn.net/pipisorry/article/details/53320669
pyspark.sql.SQLContext
Main entry point for DataFrame and SQL functionality.
pyspark.sql.DataFrame
A distributed collection of data grouped into named columns.
spark df和pandas df
spark df的操作基本和pandas df操作一样的[Pandas小记(6) ]
相互转换
从pandas_df转换:
spark_df = SQLContext.createDataFrame(pandas_df)
sc = SparkContext(master='local[8]', appName='kmeans') sql_ctx = SQLContext(sc) lldf_rdd = sql_ctx.createDataFrame(lldf)
另外,createDataFrame支持从list转换spark_df,其中list元素可以为tuple,dict,rdd
从spark_df转换:
pandas_df = spark_df.toPandas()
- toPandas()
-
Returns the contents of this DataFrame as Pandas pandas.DataFrame.
Note that this method should only be used if the resulting Pandas’s DataFrame is expectedto be small, as all the data is loaded into the driver’s memory.
This is only available if Pandas is installed and available.
>>> df.toPandas() age name 0 2 Alice 1 5 Bob
[Spark与Pandas中DataFrame对比(详细)]
spark df方法
- rdd
-
Returns the content as an pyspark.RDD of Row.
- rollup(*cols)
-
Create a multi-dimensional rollup for the current DataFrame usingthe specified columns, so we can run aggregation on them.
>>> df.rollup("name", df.age).count().orderBy("name", "age").show() +-----+----+-----+ | name| age|count| +-----+----+-----+ | null|null| 2| |Alice|null| 1| |Alice| 2| 1| | Bob|null| 1| | Bob| 5| 1| +-----+----+-----+
- select(*cols)
-
Projects a set of expressions and returns a new DataFrame.
Parameters: cols – list of column names (string) or expressions (Column).If one of the column names is ‘*’, that column is expanded to include all columnsin the current DataFrame. >>> df.select('*').collect() [Row(age=2, name=u'Alice'), Row(age=5, name=u'Bob')] >>> df.select('name', 'age').collect() [Row(name=u'Alice', age=2), Row(name=u'Bob', age=5)] >>> df.select(df.name, (df.age + 10).alias('age')).collect() [Row(name=u'Alice', age=12), Row(name=u'Bob', age=15)]
- selectExpr(*expr)
-
Projects a set of SQL expressions and returns a new DataFrame.
This is a variant of select() that accepts SQL expressions.
>>> df.selectExpr("age * 2", "abs(age)").collect() [Row((age * 2)=4, abs(age)=2), Row((age * 2)=10, abs(age)=5)]
- toDF(*cols)
-
Returns a new class:DataFrame that with new specified column names
Parameters: cols – list of new column names (string) >>> df.toDF('f1', 'f2').collect() [Row(f1=2, f2=u'Alice'), Row(f1=5, f2=u'Bob')] - persist(storageLevel=StorageLevel(False, True, False, False, 1))¶
-
Sets the storage level to persist its values across operationsafter the first time it is computed. This can only be used to assigna new storage level if the RDD does not have a storage level set yet.If no storage level is specified defaults to (MEMORY_ONLY).
from: http://blog.csdn.net/pipisorry/article/details/53320669
ref:
Spark核心类:SQLContext和DataFrame的更多相关文章
- Spark核心类:弹性分布式数据集RDD及其转换和操作pyspark.RDD
http://blog.csdn.net/pipisorry/article/details/53257188 弹性分布式数据集RDD(Resilient Distributed Dataset) 术 ...
- Spark 核心篇-SparkContext
本章内容: 1.功能描述 本篇文章就要根据源码分析SparkContext所做的一些事情,用过Spark的开发者都知道SparkContext是编写Spark程序用到的第一个类,足以说明SparkCo ...
- Spark SQL初始化和创建DataFrame的几种方式
一.前述 1.SparkSQL介绍 Hive是Shark的前身,Shark是SparkSQL的前身,SparkSQL产生的根本原因是其完全脱离了Hive的限制. SparkSQL支持查询原 ...
- 【转载】Spark SQL 1.3.0 DataFrame介绍、使用
http://www.aboutyun.com/forum.php?mod=viewthread&tid=12358&page=1 1.DataFrame是什么?2.如何创建DataF ...
- [Spark][Python]spark 从 avro 文件获取 Dataframe 的例子
[Spark][Python]spark 从 avro 文件获取 Dataframe 的例子 从如下地址获取文件: https://github.com/databricks/spark-avro/r ...
- Spark 核心篇-SparkEnv
本章内容: 1.功能概述 SparkEnv是Spark的执行环境对象,其中包括与众多Executor执行相关的对象.Spark 对任务的计算都依托于 Executor 的能力,所有的 Executor ...
- 科普Spark,Spark核心是什么,如何使用Spark(1)
科普Spark,Spark是什么,如何使用Spark(1)转自:http://www.aboutyun.com/thread-6849-1-1.html 阅读本文章可以带着下面问题:1.Spark基于 ...
- 【二】Spark 核心
spark 核心 spark core RDD创建 >>> RDD转换 >>> RDD缓存 >>> RDD行动 >>> RDD输 ...
- 大数据体系概览Spark、Spark核心原理、架构原理、Spark特点
大数据体系概览Spark.Spark核心原理.架构原理.Spark特点 大数据体系概览(Spark的地位) 什么是Spark? Spark整体架构 Spark的特点 Spark核心原理 Spark架构 ...
随机推荐
- 两个activity的3D翻转动画.md
一.业务需求 这里在公司项目设计时,用到了一个小的需求,就是点击一个按钮然后整个activity的页面进行3d翻转; 二.设计思路 由于是2个activity的之间的翻转动画,就意味着前90度是A页面 ...
- jdk1.7中的常量池
在探究jdk1.7中的常量池,我们可以先看看以下的这段代码 public static void main(String[] args) throws Throwable { List<Stri ...
- [ Java学习基础 ] 浅析Java方法调用
先看一个示例,读者可先自行阅读判断输出结果: public class TestClass { private static void testMethod(){ System.out.printl ...
- StandardContext
错误信息: 2014-2-6 21:37:19 org.apache.catalina.startup.HostConfig checkResources信息: Reloading context [ ...
- [NOI 2014]魔法森林
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- 幸运序列(lucky)
[问题描述] Lsy喜欢幸运数字,众所周知,幸运数字就是数字位上只有4和7的数字. 但是本题的幸运序列和幸运数字完全没关系,就是一个非常非常普通的序列.哈哈,是不是感觉被耍了,没错,你就是被耍了. L ...
- 洛谷P3233 [HNOI2014]世界树
虚树= = #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring&g ...
- 洛谷P2319 [HNOI2006]超级英雄
一开始是用二分图匹配(网络流)+二分做的,后来发现直接用匈牙利更简单 #include<cstdio> #include<cstdlib> #include<algori ...
- ●BZOJ 1185 [HNOI2007]最小矩形覆盖
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1185 题解: 计算几何,凸包,旋转卡壳 结论:矩形的某一条边在凸包的一条边所在的直线上. ( ...
- ubuntu 安装 WPS for Linux(ubuntu)字体配置(字体缺失解决办法)及卸载libreoffice
从官网下载安装wps for Linux sudo dpkg -i wps-office_10.1.0.5672~a21_amd64.deb 启动WPS for Linux后,出现提示"系统 ...