0

I am getting this whole expression as String (subCategory == "Serveware Sets") || (category == "Cookware & Bakeware") || (category == "Small Appliances") || (category == "Dinnerware & Serveware")

Now I want to convert this string to the expression so that I can directly compare the values of the subCategory and the category. How can I do the same.

2
  • 1
    are you asking how to parse it? then my question for you is, what's the grammar of your expression? Commented Feb 22, 2018 at 23:03
  • .. and what are these variables? What's their context? Are they Scala variables in your program or part of a DSL (e.g. DB column names, front-end related variables, ...)? Or it's related to Spark SQL maybe? Better to give more information in your question so that people can help you better. I guess maybe you have a fixed set of variables and you have an expression to parse, so it really depends on the grammar. Commented Feb 22, 2018 at 23:45

2 Answers 2

1

This can be done using expr:

val filterExpression = """(subCategory == "Serveware Sets") || (category == "Cookware & Bakeware") || (category == "Small Appliances") || (category == "Dinnerware & Serveware")"""

import org.apache.spark.sql.functions.expr

df
 .where(expr(filterExpression))
Sign up to request clarification or add additional context in comments.

2 Comments

Well this is valid if the question is asked in the context of Spark SQL.
@RaphaelRoth, I guess || symbol need to be changed to or string for the expr to work. isn't it?
0

You can change the || symbol to or so that your expression becomes

(subCategory == "Serveware Sets") or (category == "Cookware & Bakeware") or (category == "Small Appliances") or (category == "Dinnerware & Serveware")

And then you should be able to use the string expression in filtering or selecting rows from dataframes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.