天天看點

influxDB學習1 InfluxDB工具類2 測試類3 pox.xml檔案配置4 效果圖

文章目錄

  • 1 InfluxDB工具類
  • 2 測試類
  • 3 pox.xml檔案配置
  • 4 效果圖
本文介紹influxDB的基礎用法

1 InfluxDB工具類

package com.wllfengshu.utils;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import java.util.Map;

/**
 * 時序資料庫 InfluxDB操作工具類
 * @author tiandixuanwu
 *
 */
public class InfluxdbUtil {

	//以下配置資訊可以從配置檔案或者環境變量中讀
    private static String openurl = "http://192.168.40.254:8086";//連接配接位址
    private static String username = "user";//使用者名(預設是user/admin)
    private static String password = "admin";//密碼
    private static String database = "PARAMTER_DB";//資料庫

	private static InfluxdbUtil influxdbUtil = null;
	private static InfluxDB influxDB = null;
	private InfluxdbUtil(){}
	
	static{
		try {
			//建立連接配接
			influxDB = InfluxDBFactory.connect(openurl, username, password);
			//設定資料儲存政策 defalut 政策名 /database 資料庫名/ 30d 資料儲存時限30天/ 1 副本個數為1/ 結尾DEFAULT表示設為預設的政策
			String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT","defalut", database, "30d", 1);
			influxDB.query(new Query(command, database));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 或者工具類的執行個體
	 * @return 
	 * @throws
	 */
	public static InfluxdbUtil getInfluxdbUtil() {
		if (influxdbUtil == null) {
			influxdbUtil=new InfluxdbUtil();
		}
		return influxdbUtil;
	}

	/**
	 * 查詢
	 * @param command 查詢語句
	 * @return
	 */
	public QueryResult query(String command) {
		return influxDB.query(new Query(command, database));
	}
	
	/**
	 * 插入(influxdb不需要建立表,直接在插入時指定表,無則建立,有則插入)
	 * @param tags 标簽
	 * @param fields 字段
	 * @param measurement 表名
	 */
	public void insert(Map<String, String> tags, Map<String, Object> fields,String measurement) {
		Builder builder = Point.measurement(measurement);
		builder.tag(tags);
		builder.fields(fields);
		influxDB.write(database, "", builder.build());
	}

	/**
	 * 删除資料
	 * @param command 删除語句
	 * @return 傳回錯誤資訊
	 */
	public String deleteMeasurementData(String command) {
		QueryResult result = influxDB.query(new Query(command, database));
		return result.getError();
	}

	/**
	 * 建立資料庫
	 * @param dbName
	 */
	public void createDB(String dbName) {
		influxDB.createDatabase(dbName);
	}

	/**
	 * 删除資料庫
	 * @param dbName
	 */
	public void deleteDB(String dbName) {
		influxDB.deleteDatabase(dbName);
	}
	
}

           

2 測試類

package com.wllfengshu.influx;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.influxdb.dto.QueryResult.Series;
import org.junit.Before;
import org.junit.Test;

import com.wllfengshu.utils.InfluxdbUtil;

/**
 * @author tiandixuanwu
 */
public class TestMain {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private InfluxdbUtil influxdbUtil = null;
	
	@Before
	public void init(){
		influxdbUtil = InfluxdbUtil.getInfluxdbUtil();
	}
	
	@Test
	public void createDB(){
		//建立資料庫
		influxdbUtil.createDB("PARAMTER_DB");
	}
	
	@Test
	public void testInsert(){
		//測試插入資料
		Map<String, String> tags = new HashMap<>();
		Map<String, Object> fields = new HashMap<>();
		tags.put("TAG_NAME","name");
		fields.put("TAG_VALUE","張三");//注意,這裡其實應該是FIELD_VALUE
		fields.put("TIMAMPEST", sdf.format(new Date()));
		influxdbUtil.insert(tags, fields, "test");
	}
	
	@Test
	public void testQuery(){
		//讀取資料
		QueryResult queryResult = influxdbUtil.query("select * from test");
		for (Result result:queryResult.getResults()) {
			List<Series> series = result.getSeries();
			for (Series serie : series) {
				System.out.println(serie.getName());
				System.out.println(serie.getColumns());
				System.out.println(serie.getValues());
			}
		}
	}
}

           

3 pox.xml檔案配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wllfengshu</groupId>
	<artifactId>testInfluxdb</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.influxdb</groupId>
			<artifactId>influxdb-java</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>
           

4 效果圖

(1)以下為程式輸出的結果:

influxDB學習1 InfluxDB工具類2 測試類3 pox.xml檔案配置4 效果圖

(2)上面的資料在指令行中看到的結果:

influxDB學習1 InfluxDB工具類2 測試類3 pox.xml檔案配置4 效果圖
注:本人使用的是maven,大家也可以不使用,自己引入jar,下載下傳位址:https://download.csdn.net/download/tiandixuanwuliang/10693992
注:influxdb安裝參考教程:https://www.cnblogs.com/mafeng/p/6848166.html

注:參考網址:

https://blog.csdn.net/dailuwen/article/details/73741297

https://www.linuxdaxue.com/influxdb-basic-operation.html