Question: Is there a way to get count of events in Azure Event Hub for a particular time period? I need to get the count of events which come for an hour.

Answer: 
Finally, I got a solution by myself. Here it is:

```
import org.apache.spark.sql.expressions.Window
 val windowIndex = Window.partitionBy().orderBy("time")

val result = eventWithTime
  .withColumn("groupId",
    when($"events" === "A", row_number over windowIndex).otherwise(null))
  .withColumn("groupId", last("groupId", ignoreNulls = true) over windowIndex)
  .filter($"groupId".isNotNull)

```

(I use column "time" just to order events like in example)

The idea here is to find all "events" with "A" and mark them with unique id. I did it using `row_number` and `Window.partitionBy()` functions. (Maybe it is would be better to use `monotonically_increasing_id` but I have a lot of data and there are some assumptions for correct work of `monotonically_increasing_id`). After that, I used function `last` with same window. Important thing here is to set ignoreNulls to "true". In that way all nulls will be filled with first non-null value before current row. Then I just drop first rows before first "A" because they are still nulls.

Ex.:

1. Before any operations

```
events
A
b
c
d
e
A
b
c
d
A
f

```

2. Assign unique id to all "A"s (otherwise null)

```
events | groupId
A      | 1
b      | null
c      | null
d      | null
e      | null
A      | 2
b      | null
c      | null
d      | null
A      | 3
f      | null

```

3. Fill nulls using last non-null value

```
events | groupId
A      | 1
b      | 1
c      | 1
d      | 1
e      | 1
A      | 2
b      | 2
c      | 2
d      | 2
A      | 3
f      | 3

```

Now we can just `groupBy` or `partitionBy` by groupId and do what we want.
```
CREATE TABLE  #tbl(event nvarchar(5))
insert into #tbl (event)values('A'),('b'),('c'),('d'),('e'),('A'),('b'),('c'), 
('d'),('A'),('f')    
CREATE TABLE #EventDetails(event nvarchar(5),groupId int)    
DECLARE @event_name varchar(20),@group_id int=0    

DECLARE event_cursor CURSOR FOR     
SELECT event
FROM #tbl     

OPEN event_cursor      
FETCH NEXT FROM event_cursor     
INTO @event_name    

WHILE @@FETCH_STATUS = 0    
BEGIN    
    if(@event_name ='a')
    begin
        set @group_id  = @group_id+1        
        insert into #EventDetails (event ,groupId)  
        values (@event_name,@group_id)
    end
    else
    begin
        insert into #EventDetails (event ,groupId)  
        values (@event_name,@group_id)
    end

FETCH NEXT FROM event_cursor     
INTO @event_name      
END     
CLOSE event_cursor;    
DEALLOCATE event_cursor;    

select * from #EventDetails

drop table #EventDetails
drop table #tbl

=====================
Result
=====================
event   groupId
A         1
b         1
c         1
d         1
e         1
A         2
b         2
c         2
d         2
A         3
f         3

```