spring对接InfluxDB(二)--数据获取之单条查询

这里简单介绍下从代码中获取InfluxDB中数据的方法。

官网示例:

curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"

pretty=true表示返回的数据是json格式,使用的是GET方法,同时指定database,q表示要执行的sql。

先往数据库中插入数据:

    public void writePoints(){

        String url="http://localhost:8086/write?db=mydb";

        String pointValue="pkk,name=pangkun1,address=shanghai age=21\n" +
                "pkk,name=pangkun2,address=shanghai age=22\n" +
                "pkk,name=pangkun3,address=shanghai age=23";

        RestTemplate restTemplate=new RestTemplate();

        restTemplate.postForObject(url,pointValue,Object.class);
    }

从数据库获取数据JAVA代码:

    public void getTest(){
        RestTemplate restTemplate=new RestTemplate();

        String url="http://localhost:8086/query?pretty=true&q=select * from pkk&db=mydb";

        String points=restTemplate.getForObject(url,String.class);

        System.out.println("points:"+points);
    }

返回的结果:

points:{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "pkk",
                    "columns": [
                        "time",
                        "address",
                        "age",
                        "name"
                    ],
                    "values": [
                        [
                            "2017-07-28T01:32:03.378554614Z",
                            "shanghai",
                            21,
                            "pangkun1"
                        ],
                        [
                            "2017-07-28T01:32:03.378554614Z",
                            "shanghai",
                            23,
                            "pangkun3"
                        ],
                        [
                            "2017-07-28T01:32:03.378554614Z",
                            "shanghai",
                            22,
                            "pangkun2"
                        ]
                    ]
                }
            ]
        }
    ]
}

你可能感兴趣的:(InfluxDB,Spring,SpringBoot)