pySpark DataFrame简介

1. 列名类型

pyspark.sql.types module
DataType
NullType
StringType
BinaryType
BooleanType
DateType
TimestampType
DecimalType
DoubleType
FloatType
ByteType
IntegerType
LongType
ShortType
ArrayType
MapType
StructField
StructType

2. 读取部分文件

from pyspark.sql import SQLContext
sc = SparkContext.getOrCreate()
sqlContext = SQLContext(sc)

limit_df = sqlContext.read.format('com.databricks.spark.csv') \
          .options(header='true', inferschema='true').load(label_path).limit(20)
limit_df.show()

3. DataFrame 和 RDD

DataFrame:

from pyspark.sql import SQLContext
sc = SparkContext.getOrCreate()
sqlContext = SQLContext(sc)
sqlContext.read.csv(filepath, schema=fileshema, header=False, sep='\t').first()

RDD:

sc.textFile(filepath).map(lambda x: str(x).split("\t")).first()

发现,RDD读取的速度比DataFrame读取的速度更快,RDD 42s, DataFrame则需要1Min 47 s, 两倍多的时间。

比较困惑,因为DataFrame 应该和RDD差不多,都是分布式的数据结构,但是DataFrame定义了数据的内部结构。

4. dict 和 DataFrame

dict 转变为dataFrame

from pyspark.sql import SQLContext
from pyspark import SparkContext
sc = SparkContext.getOrCreate()
sqlContext = SQLContext(sc)
rdd = sc.parallelize([{'a':1}, {'a':1, 'b':2}])
df = spark.read.json(rdd)
df.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章