Class 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 with sink.

    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 single LogicalSourceOperator in its chain.

    • Method Detail

      • 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 - If expression 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 using attribute("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 - If attributes 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 - If assignedField is null or empty, or if expression 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 - If mapFunction returns a simple type. In this case, use map(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 - If mapFunction returns a complex type. In this case, use map(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 - If mapFunction returns a simple type. In this case, use map(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 - If mapFunction returns a complex type. In this case, use map(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., a TumblingWindow or SlidingWindow.
        Returns:
        A builder to continue the specification of the window operation.
        Throws:
        java.lang.IllegalArgumentException - If windowDefinition 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 - If joinedQuery 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 - If otherQuery 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 - If sourceName 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 - If model or name are null, or if name 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 call inferModel(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 - If model or name are null, or if name is empty.
        java.io.IOException - If the input stream cannot be read.
      • sink

        public Sink sink​(@NonNull Sink sink)
                  throws java.lang.IllegalArgumentException
        Add a Sink 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 the Sink 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 - If sink is null, or if the query already has a sink, or if sink is already attached to another query.
      • getSink

        public Sink getSink()
        Get the Sink of this query object.
        Returns:
        The Sink of this query object or null.