Skip to content

Latest commit

 

History

History
99 lines (71 loc) · 3.82 KB

README.md

File metadata and controls

99 lines (71 loc) · 3.82 KB

InfluxQL

CI MVN JDOC SNAP

Java library inspired from the official influxql and influxqb go package.

Building strongly typed and secure InfluxQL queries from string is not always an easy task You have to take care of the sanitization, keep the distinction between function, identifier, numbers and string literals to build a valid query.

The influxQL parser contains all the required types to manually build a query from scratch. More importantly it also contains String() method on each type and statement. The parse r then take care of escaping character, putting quotes or not, ...

The implementation is based on types from the go parser AST. Once a statement object is built the properly formated and escaped InfluxQL statement can be obtained by calling toString()

Download

Relases can be found from Maven and as github artifacts.

<dependency>
    <groupId>io.github.willena</groupId>
    <artifactId>influxql</artifactId>
    <version>${version}</version>
</dependency>

Notices

For now, no checks are made. The generated query is only guaranteed to be syntax valid. Only the influxdb server will decide if your request is globally valid and can target your data.

Example

import io.github.willena.influxql.ast.expr.Dimension;
import io.github.willena.influxql.ast.expr.VarRef;
import io.github.willena.influxql.ast.field.Field;
import io.github.willena.influxql.ast.source.Measurement;
import io.github.willena.influxql.ast.statement.AlterRetentionPolicyStatement;
import io.github.willena.influxql.ast.statement.SelectStatement;
import io.github.willena.influxql.ast.extra.FunctionFactory.Aggregations.mean;
import io.github.willena.influxql.ast.source.Measurement.measurement;
import io.github.willena.influxql.ast.expr.Dimension.sampledBy;

import java.time.Duration;

public static void main(String[] args) {
    AlterRetentionPolicyStatement stm = new AlterRetentionPolicyStatement.Builder()
            .on("DB")
            .policyName("Policy")
            .build();

    System.out.println(stm);
    // Prints 'ALTER RETENTION POLICY Policy ON "DB"'

    SelectStatement select = new SelectStatement.Builder()
            .select(Field.of(mean(VarRef.of("field"))))
            .from(measurement("meas"))
            .groupBy(sampledBy(Duration.ofDays(1)))
            .build();
    System.out.println(select);
    // Prints 'SELECT MEAN(field) FROM meas GROUP BY time(1d)'
}

Note:

The InfluxQL ANTLR-based gramar included in the repo is not fully working. A parser for influxql syntax is not yet included.

Licence

   InfluxQL Java package
   Copyright 2024 Guillaume VILLENA aka "Willena"
   
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.