Skip to content

query from InfluxDB in java with only fastJson dependency needed.只依赖fastJson的InfluxDb数据库查询

Notifications You must be signed in to change notification settings

selfmakeit/influxDB-Query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

influxDB-Query

query from InfluxDB in java with only fastJson dependency needed

1. Configure datasource in datasource.properties.

image

2.Get message from property file and create connection.

final static DataSourceConfig dataSourceConfig = new DataSourceConfig();
String ip =dataSourceConfig.getPropertyByName("ip");
String port =dataSourceConfig.getPropertyByName("port");
String dbName =dataSourceConfig.getPropertyByName("dbname");

3. Do query in your method like this:

    InfluxDB influxdb = new InfluxDB();
    influxdb.setServerIp(ip);
    influxdb.setServerPort(Integer.valueOf(port));
    influxdb.setDbName(dbName);
    if (influxdb.Connect()) {
        String sql1 = "SELECT \"usage_idle\" as idle ,(\"usage_system\" +\"usage_user\") as \"used\" ,\"host\" FROM \"cpu\" WHERE time > now() - 10s  group by \"host\"";
        String sql2 = ";SELECT \"usage_idle\" as idle ,(\"usage_system\" +\"usage_user\") as \"used\" ,\"host\" FROM \"cpu\" WHERE time > now() - 10s group by \"host\"";
        QueryResult qr = influxdb.Query(sql1+sql2);
        if(null==qr){
            return;
        }
        List<Statement> statements = qr.getStatements();
        for(Statement s:statements){
            System.out.println("Statement_id:"+s.getStatement_id());
            List<Series> series = s.getSeries();
            int i=0;
            for(Series ser:series){
                System.out.println("      series"+(++i)+":");
                System.out.println("           name:"+ser.getName());
                System.out.println("           tags:"+ser.getTags().toString());
                System.out.println("           datas:"+ser.getDatas().toString());
                // 可循环取出datas里的单条数据。。。。
            }
        }
        influxdb.DisConnect();
    }

4. Dont forget close connect with influxdb.DisConnect();

5. You can send multi sql in one query ,and separate sql with ";"

6. print query result should be like this:

image

About

query from InfluxDB in java with only fastJson dependency needed.只依赖fastJson的InfluxDb数据库查询

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages