我有一个这样的目录,其中包含json文件
/user/myuser/check/database=helloworld/table=program/proc_dt=2017-04-04/part-00000
json文件架构内容如下:
hadoop fs -cat /user/myuser/check/database=helloworld/table=program/proc_dt=2017-04-04/part-00000
{ “工作类型”: “生产者”, “为person_id”: “7d422349554”, “订单”: “1”, “ENTITY_ID”: “123”} { “工作类型”: “生产者”, “为person_id”: “af7dc39bc”, “订单”: “3”, “ENTITY_ID”: “f2323”}
当我尝试使用以下命令从json文件中读取模式时,我也在模式中获取目录的名称。
import scala.collection.mutable.ArrayBuffer
var flattenedDatasetPath = "/user/myuser/check/database=helloworld/table=program/proc_dt=2017-04-04/"
var flattenedFileSchemaList = ArrayBuffer[String]()
val flattenedDataSetDF = sqlContext.read.json(flattenedDatasetPath)
var fieldNamesArr=flattenedDataSetDF.schema.fields
for(f<-fieldNamesArr){
println(f.name)
flattenedFileSchemaList+=f.name
}
这是我得到的输出
entity_id
job_type
order
person_id
database
table
proc_dt
为什么目录名称是模式的一部分?
答案 0 :(得分:1)
这是因为默认情况下,spark.sql.sources.partitionColumnTypeInference.enabled
在Spark中设置为true
。
sqlContext.setConf("spark.sql.sources.partitionColumnTypeInference.enabled", "false")
自动推断分区列的数据类型。 目前,支持数字数据类型和字符串类型。有时 用户可能不希望自动推断出数据类型 分区列。对于这些用例,自动类型 推理可以配置 spark.sql.sources.partitionColumnTypeInference.enabled ,这是 默认为true 。禁用类型推断时,将使用字符串类型 用于分区列。