0

Body (String)라는 데이터 프레임 열이 있습니다. 본문 열 데이터는 다음과 같습니다.Spark UDF 내에서 XML 문자열 처리 및 구조 필드 반환

<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? 

본문 본문 두 개의 별도 코드 열과 텍스트를 준비하고 싶습니다. 코드는 code라는 요소 사이에 있고 text는 그 밖의 모든 것입니다.

나는이 작동하지 않습니다이

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

처럼 보이는 UDF를 만들었습니다. 내 질문은 입니다. 1. 알 수 있듯이 데이터에는 루트 노드가 없습니다. 스칼라 XML 구문 분석을 계속 사용할 수 있습니까? 2. 코드를 제외한 나머지 모든 것을 텍스트로 구문 분석하는 방법.

뭔가 잘못 내 코드에있을 경우 나

예상 출력 알려 주시기 바랍니다 : 대신 교체하고, 당신은 또한 RewriteRule를 사용하고 비워 XML 클래스의 transform 메소드를 오버라이드 (override) 할 수

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

오류 (있는 경우)? 예상되는 결과는 무엇입니까? – philantrovert

+0

스파크 셸에서 오류 메시지가 표시되지 않습니다. UDF 본문에 잘못된 것이 있습니다. spark-shell은 함수를 생성하지 않습니다. – Makkena

+0

좋습니다. 코드 태그는 여러 위치에 있습니다. 그들 모두를 원 하시겠습니까 아니면'pre '즉'decimal trans = ...'안에 넣으시겠습니까? – philantrovert

답변

0

을 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 같은 : 당신은 당신 posts5 dataframe를 호출 할 수 있습니다

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

지금처럼 :

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

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

특정 열을 추출하려면

+0

고맙습니다. 지금 이해합니다. 나는 당신의 대답을 upvote하기에 충분한 명성이 없습니다. – Makkena

+0

이것을 구현하려고하면 오류가 발생합니다. SAXParseException : 엔터티 "nbsp"가 참조되었지만 선언되지 않았습니다. 나는 을 추가했지만 문자열은 작동하지 않는다. 그것에 대해 아는 것이 있습니까? – Makkena

+0

''다음에''을 붙이고 작동하는지 확인하십시오. – philantrovert