Spark SQL parquet文件

2018-11-26 16:33 更新

Parquet文件

Parquet是一種柱狀(columnar)格式,可以被許多其它的數(shù)據(jù)處理系統(tǒng)支持。Spark SQL提供支持讀和寫(xiě)Parquet文件的功能,這些文件可以自動(dòng)地保留原始數(shù)據(jù)的模式。

加載數(shù)據(jù)

// sqlContext from the previous example is used in this example.
// createSchemaRDD is used to implicitly convert an RDD to a SchemaRDD.
import sqlContext.createSchemaRDD

val people: RDD[Person] = ... // An RDD of case class objects, from the previous example.

// The RDD is implicitly converted to a SchemaRDD by createSchemaRDD, allowing it to be stored using Parquet.
people.saveAsParquetFile("people.parquet")

// Read in the parquet file created above.  Parquet files are self-describing so the schema is preserved.
// The result of loading a Parquet file is also a SchemaRDD.
val parquetFile = sqlContext.parquetFile("people.parquet")

//Parquet files can also be registered as tables and then used in SQL statements.
parquetFile.registerTempTable("parquetFile")
val teenagers = sqlContext.sql("SELECT name FROM parquetFile WHERE age >= 13 AND age <= 19")
teenagers.map(t => "Name: " + t(0)).collect().foreach(println)

配置

可以在SQLContext上使用setConf方法配置Parquet或者在用SQL時(shí)運(yùn)行SET key=value命令來(lái)配置Parquet。

Property NameDefaultMeaning
spark.sql.parquet.binaryAsStringfalse一些其它的Parquet-producing系統(tǒng),特別是Impala和其它版本的Spark SQL,當(dāng)寫(xiě)出Parquet模式的時(shí)候,二進(jìn)制數(shù)據(jù)和字符串之間無(wú)法區(qū)分。這個(gè)標(biāo)記告訴Spark SQL將二進(jìn)制數(shù)據(jù)解釋為字符串來(lái)提供這些系統(tǒng)的兼容性。
spark.sql.parquet.cacheMetadatatrue打開(kāi)parquet元數(shù)據(jù)的緩存,可以提高靜態(tài)數(shù)據(jù)的查詢(xún)速度
spark.sql.parquet.compression.codecgzip設(shè)置寫(xiě)parquet文件時(shí)的壓縮算法,可以接受的值包括:uncompressed, snappy, gzip, lzo
spark.sql.parquet.filterPushdownfalse打開(kāi)Parquet過(guò)濾器的pushdown優(yōu)化。因?yàn)橐阎腜aruet錯(cuò)誤,這個(gè)特征默認(rèn)是關(guān)閉的。如果你的表不包含任何空的字符串或者二進(jìn)制列,打開(kāi)這個(gè)特征仍是安全的
spark.sql.hive.convertMetastoreParquettrue當(dāng)設(shè)置為false時(shí),Spark SQL將使用Hive SerDe代替內(nèi)置的支持
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)