初识 IoTdb 时间序列数据库集成到Java中, JavaAPI
Posted sunhaoyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识 IoTdb 时间序列数据库集成到Java中, JavaAPI相关的知识,希望对你有一定的参考价值。
一,获取maven依赖
<!-- https://mvnrepository.com/artifact/cn.edu.tsinghua/iotdb-jdbc --> <dependency> <groupId>cn.edu.tsinghua</groupId> <artifactId>iotdb-jdbc</artifactId> <version>0.6.0</version> </dependency>
可以自己去网上找依赖: https://mvnrepository.com/artifact/cn.edu.tsinghua/iotdb-jdbc/0.6.0
二,针对 上一篇文章上创建的组和时序 进行添加数据和查询数据简单写个测试工具类:
package com.czxk.cjc.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; /** * * @author sunzhenyang * */ public class IoTdbUtil { private final String IOTDB_DRIVER = "cn.edu.tsinghua.iotdb.jdbc.TsfileDriver"; public static Connection connection = null; String url = "jdbc:tsfile://192.168.1.120:6667/"; //:JDBC 要连接的地址 String username = "root"; //:用户名,默认为 root String password ="root";//:密码,默认为 root /** * 初始化参数 * @param url * @param username * @param password * @throws ClassNotFoundException * @throws SQLException */ public IoTdbUtil(String url,String username,String password) throws ClassNotFoundException, SQLException { if(!"".equals(StringTools.emptyRetStr(url))) this.url = url; if(!"".equals(StringTools.emptyRetStr(username))) this.username = username; if(!"".equals(StringTools.emptyRetStr(password))) this.password = password; this.createConnection(); } /** * 创建连接 * @throws ClassNotFoundException * @throws SQLException */ private void createConnection() throws ClassNotFoundException, SQLException { try { //注册 IoTDB 的 JDBC 驱动 Class.forName(IOTDB_DRIVER); //使用 DriverManager 连接 IoTDB connection = DriverManager.getConnection(url, username, password); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } } /** * 执行查询 SQL * @param groupName 组名 * @param timeName 时间序列名 * @throws SQLException */ public void executeSelect(String groupName,String timeName) throws SQLException { // 查询测控 sgcc 集团 wf03 子站 wt01 设备温度 Statement statement = null; ResultSet resultSet = null; try { statement = connection.createStatement(); //statement.execute("select status from root.ln.wf01"); statement.execute("select "+timeName+" from " + groupName); resultSet = statement.getResultSet(); while(resultSet.next()){ //结果集下标从 1 开始 System.out.println(String.format("status %s, value %s", resultSet.getString(1), resultSet.getString(2))); } } catch (SQLException ex){ // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } finally { if(statement != null) { statement.close(); } } } /** * 执行查询 SQL * @param sql 传入SQL * @throws SQLException */ public void executeSelect(String sql) throws SQLException { PreparedStatement preparedStatement = connection.prepareStatement(sql); try { //ResultSet resultSet = preparedStatement.executeQuery("select * from root "); ResultSet resultSet = preparedStatement.executeQuery(sql); ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); while(resultSet.next()){ StringBuilder builder = new StringBuilder(); for (int i = 1; i <= resultSetMetaData.getColumnCount();i++) { builder.append(resultSet.getString(i)).append(","); } System.out.println(builder); } } catch (Exception ex) { }finally { preparedStatement.close(); } } /** * 关闭连接 * @throws SQLException */ public void closes() throws SQLException { if(connection != null) { connection.close(); } } /** * 测试 * @param args * @throws SQLException * @throws ClassNotFoundException */ public static void main(String[] args) throws ClassNotFoundException, SQLException { IoTdbUtil it = new IoTdbUtil(null, null, null); try { //测试添加数据 String sql = "insert into root.cjc(timestamp,cjcdata1,cjcdata2) values(?,?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setLong(1, 123456L); preparedStatement.setString(2, "haha哈哈"); preparedStatement.setString(3, "haha哈哈"); preparedStatement.execute(); //清除上一次的数据新加数据 继续添加 preparedStatement.clearParameters(); preparedStatement.setLong(1, 654321L); preparedStatement.setString(2, "haha哈哈"); preparedStatement.setString(3, "haha哈哈"); preparedStatement.execute(); preparedStatement.close(); //获取刚添加的数据 it.executeSelect("select * from root.cjc"); } catch (Exception e) { e.printStackTrace(); }finally { it.closes(); } } }
执行main方法后 输出:
123456,haha哈哈,haha哈哈,
654321,haha哈哈,haha哈哈,
以上是关于初识 IoTdb 时间序列数据库集成到Java中, JavaAPI的主要内容,如果未能解决你的问题,请参考以下文章