Class Query
- java.lang.Object
-
- stream.nebula.runtime.Query
-
public class Query extends java.lang.Object
The Query class provides a fluid user code API to specify NebulaStream query objects which manipulate stream contents.Query objects are created with the factory method
NebulaStreamRuntime.readFromSource(java.lang.String)
and closed by adding a sink to it withsink
.The operators that are added to a query object are represented by a single operator chain. The top of this operator chain can be accessed by
getQueryPlanOperator()
. Sinks are not included in the operator chain. A newly created query contains a singleLogicalSourceOperator
in its chain.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Query
as(@NonNull java.lang.String sourceName)
Rename the logical source of this stream.Query
filter(@NonNull LogicalExpression expression)
Filter the stream represented by this query object.Query
flatMap(@NonNull java.lang.String assignedField, @NonNull FlatMapFunction<?,?> flatMapFunction)
Call a FlatMap UDF which returns a simple type and replace the stream with the results of the UDF.Query
flatMap(@NonNull FlatMapFunction<?,?> flatMapFunction)
Call a FlatMap UDF which returns a complex type and replace the contents of the stream with the results of the UDF.Operator
getQueryPlanOperator()
Retrieve the top of this query's operator chain.Sink
getSink()
Get theSink
of this query object.InferModelOperator.InferModelOn
inferModel(@NonNull java.io.InputStream model, @NonNull java.lang.String name)
Start an inferModel operation on this stream.InferModelOperator.InferModelOn
inferModel(@NonNull java.lang.String modelFileName)
Convenience method to callinferModel(java.io.InputStream, java.lang.String)
with a file name.JoinOperator.JoinWhere
joinWith(@NonNull Query joinedQuery)
Start a joinWith operation on the stream.Query
map(@NonNull java.lang.String assignedField, @NonNull Expression expression)
Map the results of a computed expression to an existing or a new field in the stream.Query
map(@NonNull java.lang.String assignedField, @NonNull MapFunction<?,?> mapFunction)
Call a Map UDF which returns a simple type and replace the stream with the result of the UDF.Query
map(@NonNull MapFunction<?,?> mapFunction)
Call a Map UDF which returns a complex type and replace the contents of the stream with the result of the UDF.Query
project(@NonNull FieldExpression... attributes)
Project the stream on one or more attributes.Sink
sink(@NonNull Sink sink)
Add aSink
to this query object.Query
unionWith(@NonNull Query otherQuery)
Create a union of this stream with another stream.WindowOperator
window(@NonNull WindowDefinition windowDefinition)
Start a window operation on the stream.
-
-
-
Method Detail
-
getQueryPlanOperator
public Operator getQueryPlanOperator()
Retrieve the top of this query's operator chain.- Returns:
- The
Operator
instance representing the last operation called on this query object, or aLogicalSourceOperator
if the query was just created withNebulaStreamRuntime.readFromSource(java.lang.String)
.
-
filter
public Query filter(@NonNull LogicalExpression expression) throws java.lang.IllegalArgumentException
Filter the stream represented by this query object.- Parameters:
expression
- The filter expression.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- Ifexpression
is null.
-
project
public Query project(@NonNull FieldExpression... attributes) throws java.lang.IllegalArgumentException
Project the stream on one or more attributes.Attributes are created using
Expressions.attribute(java.lang.String)
and can be renamed usingattribute("old_name").as("new_name")
.Query query = QueryFactory.from("source_name").project( attribute("field1"), # Use original attribute name 'field1' in projected stream. attribute("field2").as("new_name")); # Rename attribute 'field2' to 'new_name' in projected stream.
- Parameters:
attributes
- The list of projected attributes.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- Ifattributes
is null or empty, or if any elements in it are null.
-
map
public Query map(@NonNull java.lang.String assignedField, @NonNull Expression expression) throws java.lang.IllegalArgumentException
Map the results of a computed expression to an existing or a new field in the stream.- Parameters:
assignedField
- The name of the existing or new attribute to which the result is assigned.expression
- The expression that is computed from the stream.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- IfassignedField
is null or empty, or ifexpression
is null.
-
map
public Query map(@NonNull MapFunction<?,?> mapFunction)
Call a Map UDF which returns a complex type and replace the contents of the stream with the result of the UDF.- Parameters:
mapFunction
- The UDF.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- IfmapFunction
returns a simple type. In this case, usemap(String, MapFunction)
to specify the field to which the result is assigned.
-
map
public Query map(@NonNull java.lang.String assignedField, @NonNull MapFunction<?,?> mapFunction)
Call a Map UDF which returns a simple type and replace the stream with the result of the UDF.- Parameters:
assignedField
- The field name to which the result is assigned.mapFunction
- The UDF.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- IfmapFunction
returns a complex type. In this case, usemap(MapFunction)
.
-
flatMap
public Query flatMap(@NonNull FlatMapFunction<?,?> flatMapFunction)
Call a FlatMap UDF which returns a complex type and replace the contents of the stream with the results of the UDF.- Parameters:
flatMapFunction
- The UDF.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- IfmapFunction
returns a simple type. In this case, usemap(String, MapFunction)
to specify the field to which the result is assigned.
-
flatMap
public Query flatMap(@NonNull java.lang.String assignedField, @NonNull FlatMapFunction<?,?> flatMapFunction)
Call a FlatMap UDF which returns a simple type and replace the stream with the results of the UDF.- Parameters:
assignedField
- The field name to which the result is assigned.flatMapFunction
- The UDF.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- IfmapFunction
returns a complex type. In this case, usemap(MapFunction)
.
-
window
public WindowOperator window(@NonNull WindowDefinition windowDefinition) throws java.lang.IllegalArgumentException
Start a window operation on the stream.- Parameters:
windowDefinition
- The type and properties of the window, i.e., aTumblingWindow
orSlidingWindow
.- Returns:
- A builder to continue the specification of the window operation.
- Throws:
java.lang.IllegalArgumentException
- IfwindowDefinition
is null.
-
joinWith
public JoinOperator.JoinWhere joinWith(@NonNull Query joinedQuery) throws java.lang.IllegalArgumentException
Start a joinWith operation on the stream.- Parameters:
joinedQuery
- The joined query object.- Returns:
- A builder to continue the specification of the join operation.
- Throws:
java.lang.IllegalArgumentException
- IfjoinedQuery
is null.
-
unionWith
public Query unionWith(@NonNull Query otherQuery) throws java.lang.IllegalArgumentException
Create a union of this stream with another stream.- Parameters:
otherQuery
- The query object representing the other stream.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- IfotherQuery
is null.
-
as
public Query as(@NonNull java.lang.String sourceName) throws java.lang.IllegalArgumentException
Rename the logical source of this stream.- Parameters:
sourceName
- The new name.- Returns:
- This query instance.
- Throws:
java.lang.IllegalArgumentException
- IfsourceName
is null or empty.
-
inferModel
public InferModelOperator.InferModelOn inferModel(@NonNull java.io.InputStream model, @NonNull java.lang.String name) throws java.lang.IllegalArgumentException, java.io.IOException
Start an inferModel operation on this stream.- Parameters:
model
- An input stream containing the model.name
- The name of the model.- Returns:
- A builder to continue the specification of the inferModel operation.
- Throws:
java.lang.IllegalArgumentException
- Ifmodel
orname
are null, or ifname
is empty.java.io.IOException
- If the input stream cannot be read.
-
inferModel
public InferModelOperator.InferModelOn inferModel(@NonNull java.lang.String modelFileName) throws java.lang.IllegalArgumentException, java.io.IOException
Convenience method to callinferModel(java.io.InputStream, java.lang.String)
with a file name.- Parameters:
modelFileName
- The name of the file containing the model.- Returns:
- A builder to continue the specification of the inferModel operation.
- Throws:
java.lang.IllegalArgumentException
- Ifmodel
orname
are null, or ifname
is empty.java.io.IOException
- If the input stream cannot be read.
-
sink
public Sink sink(@NonNull Sink sink) throws java.lang.IllegalArgumentException
Add aSink
to this query object.A query can only have one sink. A query must have a sink when it is submitted to the runtime. This method mutates the
Query
instance on which it is called and returns theSink
instance to stop user code from adding additional operators to it.- Parameters:
sink
- The sink for this query object.- Returns:
- The
sink
parameter to this method. - Throws:
java.lang.IllegalArgumentException
- Ifsink
is null, or if the query already has a sink, or ifsink
is already attached to another query.
-
-