1

I'm having trouble finding how to convert a pyspark window statement into the equivalent SQL format.

Example:

  eventsDF \
    .groupBy(
      "deviceId",
      window("eventTime", "10 minutes", "5 minutes")) \
    .count()

It should be something like:

   select window, deviceId, count(deviceId)
   from events
   group by window eventTime 10 minutes 5 minutes, deviceId
2
  • I'm not sure if SQL could handle the group by in the syntax you mentioned. What's your current situation? do you get any error or the result is different or ...? Commented Feb 2, 2021 at 4:45
  • It's all syntax errors Commented Feb 2, 2021 at 5:15

2 Answers 2

1

You need to fix some syntax errors. The window should be wrapped in parentheses.

select
    window(eventTime, '10 minutes', '5 minutes'),
    deviceId,
    count(deviceId)
from events
group by
    window(eventTime, '10 minutes', '5 minutes'),
    deviceId
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @mck, do you know if there is any documentation about the window syntax? Looks like the official doc is incomplete: spark.apache.org/docs/latest/api/sql/#window
@Emer see the pyspark docs: spark.apache.org/docs/3.0.2/api/python/…
0

I reckon the result of the following query is what you expected:

select deviceId, count(deviceId)
from events
group by `eventTime`, `10 minutes`,  `5 minutes`, `deviceId`

the group by will group the rows on all the windows you expect

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.