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()