处理Spark UDF中的XML字符串并返回结构字段



i有一个名为body(String)的数据框列。身体列的数据看起来像这样

<p>I want to use a track-bar to change a form's opacity.</p>
<p>This is my code:</p>
 <pre><code>decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;
</code></pre>
<p>When I build the application, it gives the following error:</p>
<blockquote>
  <p>Cannot implicitly convert type 'decimal' to 'double'.</p>
</blockquote>
<p>I tried using <code>trans</code> and <code>double</code> but then the 
control doesn't work. This code worked fine in a past VB.NET project. </p>
,While applying opacity to a form should we use a decimal or double value?

使用身体我想准备两个单独的列代码和文本。代码位于名为代码的元素之间,文本是其他所有内容。

我创建了一个看起来像这样的UDF

 case class bodyresults(text:String,code:String)
 val Body:String=>bodyresults=(body:String)=>{ val xmlbody=scala.xml.XML.loadString(body)
val code = (xmlbody \ "code").toString;
val text = "I want every thing else as text. what should I do"
(text,code)
}
val bodyudf=udf(Body)
val posts5=posts4.withColumn("codetext",bodyudf(col("Body")))

这不起作用。我的问题是1.您可以看到数据中没有根节点。我还可以使用scala xml解析吗?2.如何将除代码以外的其他所有内容解析为文本。

如果我的代码中有问题,请让我知道

预期输出:

 (code,text)
 code = decimal trans = trackBar1.Value / 5000;this.Opacity = trans;trans double  
 text = everything else  

而不是进行替换,您还可以使用 RewriteRule和覆盖XML类的transform方法将XML中的<pre>标记为空。

case class bodyresults(text:String,code:String)
val bodyudf = udf{ (body: String)  =>
    // Appending body tag explicitly to the xml before parsing  
    val xmlElems = XML.loadString(s""" <body> ${body} </body> """)
    // extract the code inside the req
    val code = (xmlElems \ "body" \ "pre" \ "code").text
    val text = (xmlElems \ "body").text.replaceAll(s"${code}" ,"" )
    bodyresults(text, code)
}

此UDF将返回StructType,例如:

org.apache.spark.sql.UserDefinedFunction = UserDefinedFunction(<function1>,StructType(StructField(text,StringType,true), StructField(code,StringType,true)),List(StringType))

您现在可以在 posts5 dataframe上调用它:

val posts5 = df.withColumn("codetext", bodyudf($"xml") )
posts5: org.apache.spark.sql.DataFrame = [xml: string, codetext: struct<text:string,code:string>]

提取特定列:

posts5.select($"codetext.code" ).show
+--------------------+
|                code|
+--------------------+
|decimal trans = t...|
+--------------------+

相关内容

  • 没有找到相关文章

最新更新