15

I have a requirement to filter a data frame based on a condition that a column value should starts with a predefined string.

I am trying following:

 val domainConfigJSON = sqlContext.read
    .jdbc(url, "CONFIG", prop)
    .select("DID", "CONF", "KEY").filter("key like 'config.*'")

And getting exception:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'KEY = 'config.*'' at line 1

Using spark: 1.6.1
ZygD
  • 22,092
  • 39
  • 79
  • 102
Anush
  • 149
  • 1
  • 1
  • 4

2 Answers2

34

You can use the startsWith function present in Column class.

myDataFrame.filter(col("columnName").startswith("PREFIX"))
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
jeanr
  • 1,031
  • 8
  • 15
0

I used the same function but I was getting errors then I checked what is the error?

actually, we need to use startsWith(literals: String) but the above function having lowercase startswith().

Ex : df.filter(col("ACCOUNT_NUMBER").startsWith("9"))

m4n0
  • 29,823
  • 27
  • 76
  • 89