Spark SQL DataFrame漂亮打印

3

我不太擅长Scala(我更喜欢R语言),我希望在spark-shell中使用Scala将sqlDf.show()中的WrappedArray元素内容显示在两行中。我尝试了explode()函数,但是没有进一步的结果...

scala> val sqlDf = spark.sql("select t.articles.donneesComptablesArticle.taxes from  dau_temp t")
sqlDf: org.apache.spark.sql.DataFrame = [taxes: array<array<struct<baseImposition:bigint,codeCommunautaire:string,codeNatureTaxe:string,codeTaxe:string,droitCautionnable:boolean,droitPercu:boolean,imputationCreditCautionne:boolean,montantLiquidation:bigint,quotite:double,statutAi2:boolean,statutDeLiquidation:string,statutRessourcesPropres:boolean,typeTaxe:string>>>]

scala> sqlDf.show
16/12/21 15:13:21 WARN util.Utils: Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.debug.maxToStringFields' in SparkEnv.conf.
+--------------------+
|               taxes|
+--------------------+
|[WrappedArray([12...|
+--------------------+


scala> sqlDf.printSchema
root
 |-- taxes: array (nullable = true)
 |    |-- element: array (containsNull = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- baseImposition: long (nullable = true)
 |    |    |    |-- codeCommunautaire: string (nullable = true)
 |    |    |    |-- codeNatureTaxe: string (nullable = true)
 |    |    |    |-- codeTaxe: string (nullable = true)
 |    |    |    |-- droitCautionnable: boolean (nullable = true)
 |    |    |    |-- droitPercu: boolean (nullable = true)
 |    |    |    |-- imputationCreditCautionne: boolean (nullable = true)
 |    |    |    |-- montantLiquidation: long (nullable = true)
 |    |    |    |-- quotite: double (nullable = true)
 |    |    |    |-- statutAi2: boolean (nullable = true)
 |    |    |    |-- statutDeLiquidation: string (nullable = true)
 |    |    |    |-- statutRessourcesPropres: boolean (nullable = true)
 |    |    |    |-- typeTaxe: string (nullable = true)

scala> val sqlDfTaxes = sqlDf.select(explode(sqlDf("taxes")))
sqlDfTaxes: org.apache.spark.sql.DataFrame = [col: array<struct<baseImposition:bigint,codeCommunautaire:string,codeNatureTaxe:string,codeTaxe:string,droitCautionnable:boolean,droitPercu:boolean,imputationCreditCautionne:boolean,montantLiquidation:bigint,quotite:double,statutAi2:boolean,statutDeLiquidation:string,statutRessourcesPropres:boolean,typeTaxe:string>>]

scala> sqlDfTaxes.show()
16/12/21 15:22:28 WARN util.Utils: Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.debug.maxToStringFields' in SparkEnv.conf.
+--------------------+
|                 col|
+--------------------+
|[[12564,B00,TVA,A...|
+--------------------+

"可读"内容如下所示(这是我的目标:一个经典的行x列结构显示,带有标题):
codeTaxe codeCommunautaire baseImposition quotite montantLiquidation statutDeLiquidation
A445               B00          12564    20.0               2513                   C
U165               A00          12000     4.7                564                   C
codeNatureTaxe typeTaxe statutRessourcesPropres statutAi2 imputationCreditCautionne
TVA    ADVAL                   FALSE      TRUE                     FALSE
DD    ADVAL                    TRUE     FALSE                      TRUE
droitCautionnable droitPercu
FALSE       TRUE
FALSE       TRUE

每行的类别是什么(使用R包sparklyr查找):

<jobj[100]>
  class org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema
  [12564,B00,TVA,A445,false,true,false,2513,20.0,true,C,false,ADVAL]

[[1]][[1]][[2]]
<jobj[101]>
  class org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema
  [12000,A00,DD,U165,false,true,true,564,4.7,false,C,true,ADVAL]

1
sqlDf.show(truncate = false) 是一个不错的起点... - Tzach Zohar
@Tzach Zohar:谢谢!您确实回答了我“漂亮的打印”问题。现在,我的真正意思是获得一个传统的“行x列”结构,包括标题(就像我上面的“可读”内容)。 - guzu92
1个回答

2
您可以在每一列上进行拆分:
val flattenedtaxes = sqlDf.withColumn("codeCommunautaire",  org.apache.spark.sql.functions.explode($"taxes. codeCommunautaire"))

在此之后,你的flattenedtaxes将有两列,一列是税收(所有列都如原始数据),另外一列是新的codeCommunautaire


感谢您的提议,我遇到了以下错误:org.apache.spark.sql.AnalysisException: 由于数据类型不匹配,无法解析'taxes['codeCommunautaire']':参数2需要整数类型,但'codeCommunautaire'是字符串类型。 我会继续调查,但如果您有快速解决方案,请不要犹豫! - guzu92
我也可以把(虚构的)JSON文件发送给您,谢谢。 - guzu92
请发送给我,以便我查看并为您提供可工作的代码。 - toofrellik
正如你所说,这是一个json文件,你可以做一件事情,将你的sqlDf写入某个位置。然后使用json格式化程序读取该文件,val df = sqlContext.read.json("yourpathof/sqlDf"),然后只需打印df.show即可。 - toofrellik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接