1

I am querying a hive table using spark.

frame = sqlContext.sql("select max(id) from testing.test123") frame1=frame.map(lambda row: [str(c) for c in row]).collect() lastval =''.join(frame1[0][0]) 

I am getting a lastval which is what I expect

Now Using this lastval I want to query another table like below

abc = sqlcontext.sql("select * from testing.abc123 where id > {}". format(lastval)) 

When the lastval is a integer I am getting No errors. but when the lastval is None then I am getting the script as failed. Because the lastval should be an integer.

How do I specify if the lastvalue is None then take the lastval as 0

I tried like belwo but still when I do lastval it shows 'None'

if lastval is 'None': lastval = 0 
4
  • 2
    You are comparing against the string "None". Chances are, you want to compare against the python value None. if lastval is None:, or possibly actually testing for the empty string ''. Commented Mar 28, 2017 at 19:08
  • @JennerFelton: I believe he actually has a string 'None', but the error is closely related: He should instead check: lastval == 'None'. Commented Mar 28, 2017 at 19:11
  • @Dair I don't use the library but would if not lastval: be more Pythonic? Edit: Misread. Would it really return "None"? Commented Mar 28, 2017 at 19:12
  • 1
    @roganjosh: He calls ''.join(frame1[0][0]), I don't believe join returns None. But I think it can return a string 'None'... Commented Mar 28, 2017 at 19:14

1 Answer 1

1

You should not use is when comparing strings. Instead you should use ==:

if lastval == 'None': lastval = 0 

Or possibly you are saying that lastval = '', in which case you want to check:

if not lastval: lastval = 0 
Sign up to request clarification or add additional context in comments.

Comments