# Stream Processing with SQL

This page shows how to processing stream data in HStreamDB with SQL using Java SDK.

# Prerequisites

Make sure you have HStreamDB running and accessible.

# Execute Real-time Query on Stream

Here is an example:


Observer<HRecord> observer =
    new Observer<HRecord>() {
      @Override
      public void onNext(HRecord value) {
        System.out.println("get hrecord: {}" + value);
      }

      @Override
      public void onError(Throwable t) {
        System.out.println("error!");
      }

      @Override
      public void onCompleted() {}
    };

Queryer queryer =
    client
        .newQueryer()
        .sql("select * from test_stream where temperature > 30 emit changes;")
        .resultObserver(observer)
        .build();

// queryer will fetch real-time data at background
queryer.startAsync().awaitRunning();

// ... execute query for some time ...

// finally, you can stop the queryer using queryer.stopAsync()
queryer.stopAsync().awaitTerminated();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

You can use a Queryer object to create a real-time SQL statement, also you need to provide an Observer object which contains your logic for processing the results returned by the query. The Queryer will process data in a background thread.