CREATE VIEW
Create a new hstream view with the given name. An exception will be thrown if a view or stream with the same name already exists.
A view is NOT just an alias but physically maintained in the memory and is updated incrementally. Thus queries on a view are really fast and do not require extra resources.
Synopsis
CREATE VIEW view_name AS select_query;
Notes
view_name
is a valid identifier.select_query
is a validSELECT
(Stream) query. For more information, seeSELECT
section. There is no extra restrictions onselect_query
but we recommend using at least one aggregate function and aGROUP BY
clause. Otherwise, the query may be a little weird and consumes more resources. See the following examples:
// CREATE VIEW v1 AS SELECT id, SUM(sales) FROM s GROUP BY id EMIT CHANGES;
// what the view contains at time
// [t1] [t2] [t3]
// {"id":1, "SUM(sales)": 10} -> {"id":1, "SUM(sales)": 10} -> {"id":1, "SUM(sales)": 30}
// {"id":2, "SUM(sales)": 8} {"id":2, "SUM(sales)": 15}
// CREATE VIEW AS SELECT id, sales FROM s EMIT CHANGES;
// what the view contains at time
// [t1] [t2] [t3]
// {"id":1, "sales": 10} -> {"id":1, "sales": 10} -> {"id":1, "sales": 10}
// {"id":2, "sales": 8} {"id":1, "sales": 20}
// {"id":2, "sales": 8}
// {"id":2, "sales": 7}
Examples
CREATE VIEW foo AS SELECT a, SUM(a), COUNT(*) FROM s1 GROUP BY b EMIT CHANGES;
CREATE VIEW bar AS SELECT * FROM s2 EMIT CHANGES; // valid but not recommended