Java讀取InfluxDB數(shù)據(jù)庫的方法詳解
首先,創(chuàng)建一個Java項目,用于撰寫代碼。
接下來,配置所需要的依賴;這里我們就選擇可用于與InfluxDB數(shù)據(jù)庫進行交互的、官方支持的Java客戶端庫influxdb-java
。因為我是基于Maven來配置依賴的,所以就在項目的pom.xml
文件中,添加如下的內(nèi)容。
<dependencies> <dependency> <groupId>org.influxdb</groupId> <artifactId>influxdb-java</artifactId> <version>2.8</version> </dependency> </dependencies>
添加上述代碼后,如下圖所示。
剛剛增添完畢上述代碼時,pom.xml
文件中influxdb-java
依賴會飄紅,我們就刷新一下Maven,讓他下載對應的依賴;如下圖所示。
隨后,可以在Dependencies中看到已經(jīng)配置好了所需的influxdb-java
依賴,如下圖所示。
接下來,即可開始撰寫代碼。這里的這個代碼僅僅是一個非常簡單的示例,只是我當初學習用Java語言讀取InfluxDB數(shù)據(jù)庫用的;大家可以在這個基礎(chǔ)上,按照自己的需求進一步豐富代碼邏輯。完整代碼如下所示。
import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; import org.influxdb.dto.Query; import org.influxdb.dto.QueryResult; import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.Scanner; public class Main { private static final String url = "http://127.0.0.1:8086"; private static final String username = "root"; private static final String password = ""; public static void main(String[] args) { InfluxDB influxDB = InfluxDBFactory.connect(url, username, password); showDatabases(influxDB); showMeasurements(influxDB); showData(influxDB); } // Show database(s) private static void showDatabases(InfluxDB influxDB) { QueryResult queryResult = influxDB.query(new Query("SHOW DATABASES")); QueryResult.Series series = queryResult.getResults().get(0).getSeries().get(0); List<List<Object>> databaseList = series.getValues(); System.out.println("Database(s) is(are):"); for (List<Object> innerList: databaseList) { for (Object object: innerList) { System.out.println(object); } } System.out.println(); } // Show measurement(s) private static void showMeasurements(InfluxDB influxDB) { Scanner scanner = new Scanner(System.in); System.out.print("Please enter the database name: "); String databaseName = scanner.nextLine(); try { List<List<Object>> measurementsList = influxDB.query(new Query("SHOW MEASUREMENTS ON " + databaseName)).getResults().get(0).getSeries().get(0).getValues(); System.out.println("Measurement(s) for " + databaseName + " is(are):"); for (List<Object> innerList: measurementsList) { for (Object object: innerList) { System.out.println(object); } } System.out.println(); showTagKeys(influxDB, databaseName); showTagValues(influxDB, databaseName); showFieldKeys(influxDB, databaseName); } catch (Exception e) { System.out.println("Measurement(s) for " + databaseName + " is empty!"); return; } } // Show tag keys private static void showTagKeys(InfluxDB influxDB, String databaseName) { List<List<Object>> tagKeysList = influxDB.query(new Query("SHOW TAG KEYS ON " + databaseName)).getResults().get(0).getSeries().get(0).getValues(); System.out.println("Tag Key(s) for " + databaseName + " is(are):"); for (List<Object> innerList: tagKeysList) { for (Object object: innerList) { System.out.println(object); } } System.out.println(); } // Show tag values private static void showTagValues(InfluxDB influxDB, String databaseName) { Scanner scanner = new Scanner(System.in); System.out.print("Please enter the tag key name: "); String TagKey = scanner.nextLine(); List<List<Object>> tagValuesList = influxDB.query(new Query("SHOW TAG VALUES ON " + databaseName + " WITH KEY = " + TagKey)).getResults().get(0).getSeries().get(0).getValues(); System.out.println("Tag Value(s) is(are):"); for (List<Object> innerList: tagValuesList) { System.out.println(innerList.get(0) + ": " + innerList.get(1)); } System.out.println(); } // Show field keys private static void showFieldKeys(InfluxDB influxDB, String databaseName) { List<List<Object>> fieldKeysList = influxDB.query(new Query("SHOW FIELD KEYS ON " + databaseName)).getResults().get(0).getSeries().get(0).getValues(); System.out.println("Field Key(s) and Type(s) are:"); for (List<Object> innerList: fieldKeysList) { System.out.println(innerList.get(0) + ": " + innerList.get(1)); } System.out.println(); } // Show data private static void showData(@NotNull InfluxDB influxDB) { Scanner scanner = new Scanner(System.in); System.out.print("Please enter the database name: "); String databaseName = scanner.nextLine(); System.out.print("Please enter the measurements name: "); String measurementName = scanner.nextLine(); influxDB.setDatabase(databaseName); QueryResult dataResult = influxDB.query(new Query("SELECT * FROM " + measurementName)); System.out.println(dataResult); } }
代碼整體思路也很簡單,這里再簡單介紹一下代碼的流程。
首先,需要連接到InfluxDB數(shù)據(jù)庫。在這一部分,通過InfluxDBFactory.connect創(chuàng)建了一個本地運行的InfluxDB實例的連接,使用默認的URL (也就是http://127.0.0.1:8086),并配置用戶名和密碼。
隨后,showDatabases方法執(zhí)行了SHOW DATABASES,用以查詢并打印出InfluxDB實例所有數(shù)據(jù)庫的名字。從這一部分的代碼開始,后續(xù)所有代碼在操作數(shù)據(jù)庫方面的邏輯都是很類似的——通過模擬并執(zhí)行InfluxDB的數(shù)據(jù)庫語句,來實現(xiàn)各項操作。
接下來,showMeasurements方法可以讓我們輸入一個數(shù)據(jù)庫名,然后執(zhí)行SHOW MEASUREMENTS查詢來獲取該數(shù)據(jù)庫中所有measurement的名稱,并打印出來。如果measurement為空,則會輸出相應的提示信息。
其次,showTagKeys方法用于列出指定數(shù)據(jù)庫的所有tag key,其后的showTagValues方法則可以讓我們輸入一個tag key名,隨后查詢并打印出該tag key對應的所有tag value。
緊接著,showFieldKeys方法用于列出指定數(shù)據(jù)庫中所有的field key。
最后,showData方法讓我們輸入一個數(shù)據(jù)庫和measurement的名稱,隨后查詢、獲取該measurement下的所有數(shù)據(jù),并將結(jié)果直接打印出來。當然,我這里當初只是為了驗證是否讀取到了measurement,所以是直接打印的;在實際應用中,大家可以修改一下代碼,更優(yōu)雅地格式化輸出。
至此,大功告成。
以上就是Java讀取InfluxDB數(shù)據(jù)庫的方法詳解的詳細內(nèi)容,更多關(guān)于Java讀取InfluxDB的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java常用類庫Apache Commons工具類說明及使用實例詳解
這篇文章主要介紹了Java常用類庫Apache Commons工具類說明及使用實例詳解,需要的朋友可以參考下2020-02-02Android設(shè)備如何保證數(shù)據(jù)同步寫入磁盤的實現(xiàn)
這篇文章主要介紹了Android設(shè)備如何保證數(shù)據(jù)同步寫入磁盤的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09SpringMVC+MyBatis實現(xiàn)多數(shù)據(jù)源切換
在企業(yè)級應用開發(fā)中,經(jīng)常需要處理來自不同數(shù)據(jù)庫的數(shù)據(jù),為了滿足這一需求,我們可以通過配置多個數(shù)據(jù)源來實現(xiàn)對不同數(shù)據(jù)庫的訪問,下面我們來看看具體實現(xiàn)吧2025-01-01SpringBoot整合dataworks的實現(xiàn)過程
這篇文章主要介紹了SpringBoot整合dataworks的實現(xiàn)過程,實現(xiàn)主要是編寫工具類,如果需要則可以配置成SpringBean,注入容器即可使用,需要的朋友可以參考下2022-08-08idea啟動報錯:Command line is too long問題
在使用IDEA時,若遇到"Commandlineistoolong"錯誤,通常是因為命令行長度超限,這是因為IDEA通過命令行或文件將classpath傳遞至JVM,操作系統(tǒng)對命令行長度有限制,解決方法是切換至動態(tài)類路徑,通過修改項目的workspace.xml文件2024-09-09