how to make reflectMethod as UDF
there is a problme for me:( Coule people give me some help:)
this is my require: dynamic load jars + register UDF.
the code:
//Test.ImTest
object ImTest extends Serializable {
def len(bookTitle: String):String =
{"ImTest"}
}
// main
val ru = scala.reflect.runtime.universe
val classMirror = ru.runtimeMirror(getClass.getClassLoader)
val classTest = classMirror.staticModule("Test.ImTest")
val methods = classMirror.reflectModule(classTest)
val objMirror = classMirror.reflect(methods.instance)
val method = methods.symbol.typeSignature.member(ru.TermName("len")).asMethod
val result = objMirror.reflectMethod(method)("bbb")
def d(s: String) = {
objMirror.reflectMethod(method)(s)
}
spark.udf.register("len", d _)
spark.sql("select len('bb')").show()
errors:
18/11/21 09:58:52 INFO execution.SparkSqlParser: Parsing command: select len('bb')
18/11/21 09:58:54 INFO codegen.CodeGenerator: Code generated in 475.734954 ms
Exception in thread "main" org.apache.spark.SparkException: Task not serializable
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:298)
at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:288)
at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:108)
at org.apache.spark.SparkContext.clean(SparkContext.scala:2106)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:840)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:839)
Caused by: java.io.NotSerializableException:
scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9
Serialization stack:
- object not serializable (class: scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9,
value: method len)
- field (class: Test.Main$$anonfun$main$1, name: method$1, type: interface scala.reflect.api.Symbols$MethodSymbolApi)
- object (class Test.Main$$anonfun$main$1, )
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2, name:
func$2, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2,
)
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF, name: f, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF, UDF:len(bb))
- element of array (index: 0)
- array (class [Ljava.lang.Object;, size 2)
- field (class: org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8, name:
references$1, type: class [Ljava.lang.Object;)
- object (class org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8,
) at
org.apache.spark.serializer.SerializationDebugger$.improveException(SerializationDebugger.scala:40)
at
org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:46)
at
org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:100)
at
org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:295)
... 44 more
scala apache-spark
add a comment |
there is a problme for me:( Coule people give me some help:)
this is my require: dynamic load jars + register UDF.
the code:
//Test.ImTest
object ImTest extends Serializable {
def len(bookTitle: String):String =
{"ImTest"}
}
// main
val ru = scala.reflect.runtime.universe
val classMirror = ru.runtimeMirror(getClass.getClassLoader)
val classTest = classMirror.staticModule("Test.ImTest")
val methods = classMirror.reflectModule(classTest)
val objMirror = classMirror.reflect(methods.instance)
val method = methods.symbol.typeSignature.member(ru.TermName("len")).asMethod
val result = objMirror.reflectMethod(method)("bbb")
def d(s: String) = {
objMirror.reflectMethod(method)(s)
}
spark.udf.register("len", d _)
spark.sql("select len('bb')").show()
errors:
18/11/21 09:58:52 INFO execution.SparkSqlParser: Parsing command: select len('bb')
18/11/21 09:58:54 INFO codegen.CodeGenerator: Code generated in 475.734954 ms
Exception in thread "main" org.apache.spark.SparkException: Task not serializable
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:298)
at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:288)
at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:108)
at org.apache.spark.SparkContext.clean(SparkContext.scala:2106)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:840)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:839)
Caused by: java.io.NotSerializableException:
scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9
Serialization stack:
- object not serializable (class: scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9,
value: method len)
- field (class: Test.Main$$anonfun$main$1, name: method$1, type: interface scala.reflect.api.Symbols$MethodSymbolApi)
- object (class Test.Main$$anonfun$main$1, )
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2, name:
func$2, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2,
)
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF, name: f, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF, UDF:len(bb))
- element of array (index: 0)
- array (class [Ljava.lang.Object;, size 2)
- field (class: org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8, name:
references$1, type: class [Ljava.lang.Object;)
- object (class org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8,
) at
org.apache.spark.serializer.SerializationDebugger$.improveException(SerializationDebugger.scala:40)
at
org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:46)
at
org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:100)
at
org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:295)
... 44 more
scala apache-spark
add a comment |
there is a problme for me:( Coule people give me some help:)
this is my require: dynamic load jars + register UDF.
the code:
//Test.ImTest
object ImTest extends Serializable {
def len(bookTitle: String):String =
{"ImTest"}
}
// main
val ru = scala.reflect.runtime.universe
val classMirror = ru.runtimeMirror(getClass.getClassLoader)
val classTest = classMirror.staticModule("Test.ImTest")
val methods = classMirror.reflectModule(classTest)
val objMirror = classMirror.reflect(methods.instance)
val method = methods.symbol.typeSignature.member(ru.TermName("len")).asMethod
val result = objMirror.reflectMethod(method)("bbb")
def d(s: String) = {
objMirror.reflectMethod(method)(s)
}
spark.udf.register("len", d _)
spark.sql("select len('bb')").show()
errors:
18/11/21 09:58:52 INFO execution.SparkSqlParser: Parsing command: select len('bb')
18/11/21 09:58:54 INFO codegen.CodeGenerator: Code generated in 475.734954 ms
Exception in thread "main" org.apache.spark.SparkException: Task not serializable
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:298)
at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:288)
at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:108)
at org.apache.spark.SparkContext.clean(SparkContext.scala:2106)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:840)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:839)
Caused by: java.io.NotSerializableException:
scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9
Serialization stack:
- object not serializable (class: scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9,
value: method len)
- field (class: Test.Main$$anonfun$main$1, name: method$1, type: interface scala.reflect.api.Symbols$MethodSymbolApi)
- object (class Test.Main$$anonfun$main$1, )
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2, name:
func$2, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2,
)
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF, name: f, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF, UDF:len(bb))
- element of array (index: 0)
- array (class [Ljava.lang.Object;, size 2)
- field (class: org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8, name:
references$1, type: class [Ljava.lang.Object;)
- object (class org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8,
) at
org.apache.spark.serializer.SerializationDebugger$.improveException(SerializationDebugger.scala:40)
at
org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:46)
at
org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:100)
at
org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:295)
... 44 more
scala apache-spark
there is a problme for me:( Coule people give me some help:)
this is my require: dynamic load jars + register UDF.
the code:
//Test.ImTest
object ImTest extends Serializable {
def len(bookTitle: String):String =
{"ImTest"}
}
// main
val ru = scala.reflect.runtime.universe
val classMirror = ru.runtimeMirror(getClass.getClassLoader)
val classTest = classMirror.staticModule("Test.ImTest")
val methods = classMirror.reflectModule(classTest)
val objMirror = classMirror.reflect(methods.instance)
val method = methods.symbol.typeSignature.member(ru.TermName("len")).asMethod
val result = objMirror.reflectMethod(method)("bbb")
def d(s: String) = {
objMirror.reflectMethod(method)(s)
}
spark.udf.register("len", d _)
spark.sql("select len('bb')").show()
errors:
18/11/21 09:58:52 INFO execution.SparkSqlParser: Parsing command: select len('bb')
18/11/21 09:58:54 INFO codegen.CodeGenerator: Code generated in 475.734954 ms
Exception in thread "main" org.apache.spark.SparkException: Task not serializable
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:298)
at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:288)
at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:108)
at org.apache.spark.SparkContext.clean(SparkContext.scala:2106)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:840)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsWithIndex$1.apply(RDD.scala:839)
Caused by: java.io.NotSerializableException:
scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9
Serialization stack:
- object not serializable (class: scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$9,
value: method len)
- field (class: Test.Main$$anonfun$main$1, name: method$1, type: interface scala.reflect.api.Symbols$MethodSymbolApi)
- object (class Test.Main$$anonfun$main$1, )
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2, name:
func$2, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2,
)
- field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF, name: f, type: interface scala.Function1)
- object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF, UDF:len(bb))
- element of array (index: 0)
- array (class [Ljava.lang.Object;, size 2)
- field (class: org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8, name:
references$1, type: class [Ljava.lang.Object;)
- object (class org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8,
) at
org.apache.spark.serializer.SerializationDebugger$.improveException(SerializationDebugger.scala:40)
at
org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:46)
at
org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:100)
at
org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:295)
... 44 more
scala apache-spark
scala apache-spark
edited Nov 21 '18 at 6:50


Ram Ghadiyaram
16.5k64477
16.5k64477
asked Nov 21 '18 at 1:51
mengmeng
239
239
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404246%2fhow-to-make-reflectmethod-as-udf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404246%2fhow-to-make-reflectmethod-as-udf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown