<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to use Java MaskFormatter in sparksql? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-use-java-maskformatter-in-sparksql/m-p/12051#M6918</link>
    <description>&lt;P&gt;I create a function based on Java MaskFormatter function in Databricks/Scala.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But when I call it from sparksql, I received error message&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Error in SQL statement: AnalysisException: Undefined function: formatAccount. This function is neither a built-in/temporary function, nor a persistent function that is qualified as spark_catalog.default.formataccount.; line 1 pos 32&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is my function&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import javax.swing.text.MaskFormatter
&amp;nbsp;
def formatAccount(account: String, mask:String ) : String =
&amp;nbsp;{
&amp;nbsp; val formatter = new MaskFormatter(mask.replace("X", "A"))
&amp;nbsp; formatter.setValueContainsLiteralCharacters(false)
&amp;nbsp; val formatAccount = formatter.valueToString(account)
&amp;nbsp;  formatAccount
&amp;nbsp;}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the query code which received the error message&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;sql("""select java_method(emitToKafka ,formatAccount("1222233334", "X-XXXX-XXXX-X"))""")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However if I run below code, it works fine.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;formatAccount("1222233334", "X-XXXX-XXXX-X")&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;res0: String = 1-2222-3333-4&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;what could be missed?&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jan 2023 17:42:13 GMT</pubDate>
    <dc:creator>Databrickguy</dc:creator>
    <dc:date>2023-01-13T17:42:13Z</dc:date>
    <item>
      <title>How to use Java MaskFormatter in sparksql?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-java-maskformatter-in-sparksql/m-p/12051#M6918</link>
      <description>&lt;P&gt;I create a function based on Java MaskFormatter function in Databricks/Scala.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But when I call it from sparksql, I received error message&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Error in SQL statement: AnalysisException: Undefined function: formatAccount. This function is neither a built-in/temporary function, nor a persistent function that is qualified as spark_catalog.default.formataccount.; line 1 pos 32&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is my function&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import javax.swing.text.MaskFormatter
&amp;nbsp;
def formatAccount(account: String, mask:String ) : String =
&amp;nbsp;{
&amp;nbsp; val formatter = new MaskFormatter(mask.replace("X", "A"))
&amp;nbsp; formatter.setValueContainsLiteralCharacters(false)
&amp;nbsp; val formatAccount = formatter.valueToString(account)
&amp;nbsp;  formatAccount
&amp;nbsp;}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the query code which received the error message&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;sql("""select java_method(emitToKafka ,formatAccount("1222233334", "X-XXXX-XXXX-X"))""")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However if I run below code, it works fine.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;formatAccount("1222233334", "X-XXXX-XXXX-X")&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;res0: String = 1-2222-3333-4&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;what could be missed?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 17:42:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-java-maskformatter-in-sparksql/m-p/12051#M6918</guid>
      <dc:creator>Databrickguy</dc:creator>
      <dc:date>2023-01-13T17:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Java MaskFormatter in sparksql?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-java-maskformatter-in-sparksql/m-p/12052#M6919</link>
      <description>&lt;P&gt;@Tim zhang​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The issue is that the formatAccount function is defined as a Scala function, but SparkSQL is looking for a SQL function. You need to register the Scala function as a SQL function so that it can be called from SparkSQL. You can register the Scala function as a SQL function using the spark.udf.register method. Here is an example code snippet:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import org.apache.spark.sql.functions.udf
&amp;nbsp;
val formatAccountUDF = udf((account: String, mask:String ) =&amp;gt; {
  val formatter = new MaskFormatter(mask.replace("X", "A"))
  formatter.setValueContainsLiteralCharacters(false)
  val formatAccount = formatter.valueToString(account)
  formatAccount
})
&amp;nbsp;
spark.udf.register("formatAccount", formatAccountUDF)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;After registering the function, you can use it in your SparkSQL queries:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;sql("""select formatAccount("1222233334", "X-XXXX-XXXX-X")""")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hopefully this should work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2023 14:57:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-java-maskformatter-in-sparksql/m-p/12052#M6919</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-04-10T14:57:56Z</dc:date>
    </item>
  </channel>
</rss>

