首页 > 其他分享 >Apache IoTDB开发系统之JDBC

Apache IoTDB开发系统之JDBC

时间:2023-09-10 21:01:13浏览次数:35  
标签:JDBC resultSet System IoTDB statement sql Apache root out

Dependencies

  • JDK >= 1.8
  • Maven >= 3.1

Package only JDBC projects

在根目录中执行以下命令:

mvn clean package -pl jdbc -am -DskipTests

如何在本地 maven 存储库中安装

在根目录中:

mvn clean install -pl jdbc -am -Dmaven.test.skip=true

将 IoTDB JDBC 与 Maven 结合使用

<dependencies>
    <dependency>
      <groupId>org.apache.iotdb</groupId>
      <artifactId>iotdb-jdbc</artifactId>
      <version>0.10.0</version>
    </dependency>
</dependencies>

举例

在本文中我将向大家分享如何打开数据库连接、执行 SQL 查询和显示结果的示例。

要求包含包含数据库编程所需的 JDBC 类的包。

注意:为了更快地插入,建议在会话中使用 insertTablet()。

import java.sql.*;
import org.apache.iotdb.jdbc.IoTDBSQLException;

public class JDBCExample {
  /**
   * Before executing a SQL statement with a Statement object, you need to create a Statement object using the createStatement() method of the Connection object.
   * After creating a Statement object, you can use its execute() method to execute a SQL statement
   * Finally, remember to close the 'statement' and 'connection' objects by using their close() method
   * For statements with query results, we can use the getResultSet() method of the Statement object to get the result set.
   */
  public static void main(String[] args) throws SQLException {
    Connection connection = getConnection();
    if (connection == null) {
      System.out.println("get connection defeat");
      return;
    }
    Statement statement = connection.createStatement();
    //Create storage group
    try {
      statement.execute("SET STORAGE GROUP TO root.demo");
    }catch (IoTDBSQLException e){
      System.out.println(e.getMessage());
    }


    //Show storage group
    statement.execute("SHOW STORAGE GROUP");
    outputResult(statement.getResultSet());

    //Create time series
    //Different data type has different encoding methods. Here use INT32 as an example
    try {
      statement.execute("CREATE TIMESERIES root.demo.s0 WITH DATATYPE=INT32,ENCODING=RLE;");
    }catch (IoTDBSQLException e){
      System.out.println(e.getMessage());
    }
    //Show time series
    statement.execute("SHOW TIMESERIES root.demo");
    outputResult(statement.getResultSet());
    //Show devices
    statement.execute("SHOW DEVICES");
    outputResult(statement.getResultSet());
    //Count time series
    statement.execute("COUNT TIMESERIES root");
    outputResult(statement.getResultSet());
    //Count nodes at the given level
    statement.execute("COUNT NODES root LEVEL=3");
    outputResult(statement.getResultSet());
    //Count timeseries group by each node at the given level
    statement.execute("COUNT TIMESERIES root GROUP BY LEVEL=3");
    outputResult(statement.getResultSet());


    //Execute insert statements in batch
    statement.addBatch("insert into root.demo(timestamp,s0) values(1,1);");
    statement.addBatch("insert into root.demo(timestamp,s0) values(1,1);");
    statement.addBatch("insert into root.demo(timestamp,s0) values(2,15);");
    statement.addBatch("insert into root.demo(timestamp,s0) values(2,17);");
    statement.addBatch("insert into root.demo(timestamp,s0) values(4,12);");
    statement.executeBatch();
    statement.clearBatch();

    //Full query statement
    String sql = "select * from root.demo";
    ResultSet resultSet = statement.executeQuery(sql);
    System.out.println("sql: " + sql);
    outputResult(resultSet);

    //Exact query statement
    sql = "select s0 from root.demo where time = 4;";
    resultSet= statement.executeQuery(sql);
    System.out.println("sql: " + sql);
    outputResult(resultSet);

    //Time range query
    sql = "select s0 from root.demo where time >= 2 and time < 5;";
    resultSet = statement.executeQuery(sql);
    System.out.println("sql: " + sql);
    outputResult(resultSet);

    //Aggregate query
    sql = "select count(s0) from root.demo;";
    resultSet = statement.executeQuery(sql);
    System.out.println("sql: " + sql);
    outputResult(resultSet);

    //Delete time series
    statement.execute("delete timeseries root.demo.s0");

    //close connection
    statement.close();
    connection.close();
  }

  public static Connection getConnection() {
    // JDBC driver name and database URL
    String driver = "org.apache.iotdb.jdbc.IoTDBDriver";
    String url = "jdbc:iotdb://127.0.0.1:6667/";

    // Database credentials
    String username = "root";
    String password = "root";

    Connection connection = null;
    try {
      Class.forName(driver);
      connection = DriverManager.getConnection(url, username, password);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return connection;
  }

  /**
   * This is an example of outputting the results in the ResultSet
   */
  private static void outputResult(ResultSet resultSet) throws SQLException {
    if (resultSet != null) {
      System.out.println("--------------------------");
      final ResultSetMetaData metaData = resultSet.getMetaData();
      final int columnCount = metaData.getColumnCount();
      for (int i = 0; i < columnCount; i++) {
        System.out.print(metaData.getColumnLabel(i + 1) + " ");
      }
      System.out.println();
      while (resultSet.next()) {
        for (int i = 1; ; i++) {
          System.out.print(resultSet.getString(i));
          if (i < columnCount) {
            System.out.print(", ");
          } else {
            System.out.println();
            break;
          }
        }
      }
      System.out.println("--------------------------\n");
    }
  }
}

标签:JDBC,resultSet,System,IoTDB,statement,sql,Apache,root,out
From: https://blog.51cto.com/u_15123639/7428045

相关文章

  • 就archlinux系统中apache 无法启动php的原因
    原文连接背景,在archlinux里面想使用apache作为服务器启动php服务,根据ArchWiki 的配置,并不成功检验原因:sudosystemctlstatushttpd结果:systemctlstatusapachesystemd[1]:StartedApacheWebServerhttpd[1444]:httpd:Syntaxerroronline542of/etc/httpd......
  • Java实现关系型数据库工具类JdbcUtils系列九:通用DAO
    Java实现关系型数据库工具类JdbcUtils系列九:通用DAO一、创建对应数据库表的实体类二、数据库连接池Druid工具类三、DAO类四、BaseDAO五、DatabaseInfoDao六、通用DAO测试类一、创建对应数据库表的实体类数据库表结构CREATETABLE`databaseInfo`(`id`bigint(11)NOTNULLAU......
  • flume报错:java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration
    flume报错:java.lang.NoClassDefFoundError:org/apache/hadoop/conf/ConfigurationFailedtostartagentbecausedependencieswerenotfoundinclasspath.Errorfollows.java.lang.NoClassDefFoundError:org/apache/hadoop/conf/Configurationatorg.apache.flume.sink......
  • 关于IDEA里面连接数据库找不到org.apache.hive.jdbc.Driver的问题
    问题描述昨天就很顺利地连接上了,今天直接找不到我的class了,吓出一身冷汗;问题解决后来发现导入的jar包的路径不太对,突然想起来jar包的位置被我移动了,但是IDEA里面并没有改变原来的位置,找不到jar包的问题,重新将jar包的路径浏览一遍即可解决;......
  • Springboot项目中pom.xml配置文件无法解析下载oracl数据库解决办法(Cannot resolve com
    网上说是因Oracle的版权问题,导致maven下载不下来ojdbc各个版本的jar包。就会报错Cannotresolvecom.oracle:ojdbc6:11.2.0.1.0 经过一番百度,找到了一个适用的解决方法,如下操作即可:1.在终端或客户端机器上找到oracle安装驱动目录:例如:E:\myorcl\product\11.2.0\dbhome_1\j......
  • Flink 1.17教程:输出算子之输出到MySQL(JDBC)
    输出到MySQL(JDBC)写入数据的MySQL的测试步骤如下。(1)添加依赖添加MySQL驱动:mysqlmysql-connector-java8.0.27官方还未提供flink-connector-jdbc的1.17.0的正式依赖,暂时从apachesnapshot仓库下载,pom文件中指定仓库路径:apache-snapshotsapachesnapshotshttps://repository.a......
  • JDBC
    JDBC一、JDBC简介JDBC就是由sun公司定义的一套操作所有关系型数据库的规则(接口),而数据库厂商需要实现这套接口,厂商的实现类在引入的数据库驱动jar包中。JDBC(JavaDataBaseConnectivity),是Java连接数据库的技术。是一种执行SQL的API,可以为多种关系型数据库提供......
  • Apache HTTPD-换行解析漏洞(CVE-2017-15715)
    目录ApacheHTTPD-换行解析漏洞(CVE-2017-15715)1.1、漏洞描述1.2、漏洞等级1.3、影响版本1.4、漏洞复现1、基础环境2、漏洞扫描3、漏洞验证1.5、深度利用GetShell1.6、修复建议ApacheHTTPD-换行解析漏洞(CVE-2017-15715)说明内容漏洞编号CVE-2017-15715漏洞名称Apac......
  • Apache HTTPD-未知后缀名解析
    目录ApacheHTTPD-未知后缀名解析ApacheHTTPD-未知后缀名解析upload-labs/Pass-07上传1.php文件<?php@eval($_REQUEST[6868]);phpinfo();?>访问/upload/1.php.jaychou蚁剑连接......
  • 报org.apache.axis cannot be resolved to a type且Syntax error on token "enum", cl
    一位专门负责导数据和单点登录模块的同事最近提交了一些代码,但由于他出差了,代码同步下来却发现报如下的错误:org.apache.axiscannotberesolvedtoatype且Syntaxerrorontoken"enum",classexpected错误查看出错文件发现代码中凡是出现“oper.setStyle(org.apache.axis.enum......