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:

  1. Observer<HRecord> observer =
  2. new Observer<HRecord>() {
  3. @Override
  4. public void onNext(HRecord value) {
  5. System.out.println("get hrecord: {}" + value);
  6. }
  7. @Override
  8. public void onError(Throwable t) {
  9. System.out.println("error!");
  10. }
  11. @Override
  12. public void onCompleted() {}
  13. };
  14. Queryer queryer =
  15. client
  16. .newQueryer()
  17. .sql("select * from test_stream where temperature > 30 emit changes;")
  18. .resultObserver(observer)
  19. .build();
  20. // queryer will fetch real-time data at background
  21. queryer.startAsync().awaitRunning();
  22. // ... execute query for some time ...
  23. // finally, you can stop the queryer using queryer.stopAsync()
  24. queryer.stopAsync().awaitTerminated();

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.