Skip to main content
Version: 0.9

SQL Tools

GreptimeDB uses SQL as its main query language and supports many popular SQL tools. This document guides you on how to use SQL tools with GreptimeDB.

Language drivers

It is recommended to use mature SQL drivers to query data.

Java Database Connectivity (JDBC) is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems.

Many databases, such as MySQL or PostgreSQL, have implemented their own drivers based on the JDBC API. Since GreptimeDB supports multiple protocols, we use MySQL as an example to demonstrate how to use JDBC. If you want to use other protocols, just replace the MySQL driver with the corresponding driver.

Installation

If you are using Maven, add the following to your pom.xml dependencies list:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>

Connect to database

The following use MySQL as an example to demonstrate how to connect to GreptimeDB.

public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
Properties prop = new Properties();
prop.load(QueryJDBC.class.getResourceAsStream("/db-connection.properties"));

String dbName = (String) prop.get("db.database-driver");
String dbConnUrl = (String) prop.get("db.url");
String dbUserName = (String) prop.get("db.username");
String dbPassword = (String) prop.get("db.password");

Class.forName(dbName);
Connection dbConn = DriverManager.getConnection(dbConnUrl, dbUserName, dbPassword);

return Objects.requireNonNull(dbConn, "Failed to make connection!");
}

You need a properties file to store the DB connection information. Place it in the resources directory and name it db-connection.properties. The file content is as follows:

# DataSource
db.database-driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:4002/public
db.username=
db.password=

Or you can get the file from here.

Time zone

Set the JDBC time zone by setting URL parameters:

jdbc:mysql://127.0.0.1:4002?connectionTimeZone=Asia/Shanghai&forceConnectionTimeZoneToSession=true
  • connectionTimeZone={LOCAL|SERVER|user-defined-time-zone} specifies the connection time zone.
  • forceConnectionTimeZoneToSession=true sets the session time_zone variable to the value specified in connectionTimeZone.

Raw SQL

It is recommended to use raw SQL to experience the full features of GreptimeDB. The following example shows how to use raw SQL to query data.

try (Connection conn = getConnection()) {
Statement statement = conn.createStatement();

// DESC table;
ResultSet rs = statement.executeQuery("DESC cpu_metric");
LOG.info("Column | Type | Key | Null | Default | Semantic Type ");
while (rs.next()) {
LOG.info("{} | {} | {} | {} | {} | {}",
rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6));
}

// SELECT COUNT(*) FROM cpu_metric;
rs = statement.executeQuery("SELECT COUNT(*) FROM cpu_metric");
while (rs.next()) {
LOG.info("Count: {}", rs.getInt(1));
}

// SELECT * FROM cpu_metric ORDER BY ts DESC LIMIT 5;
rs = statement.executeQuery("SELECT * FROM cpu_metric ORDER BY ts DESC LIMIT 5");
LOG.info("host | ts | cpu_user | cpu_sys");
while (rs.next()) {
LOG.info("{} | {} | {} | {}",
rs.getString("host"),
rs.getTimestamp("ts"),
rs.getDouble("cpu_user"),
rs.getDouble("cpu_sys"));
}
}

For the complete code of the demo, please refer to here.

Query library reference

For more information about how to use the query library, please see the documentation of the corresponding library:

Command line tools

MySQL

You can use the mysql command line tool to connect to the GreptimeDB. Please refer to the MySQL protocol document for connection information.

After you connect to the server, you can use all GreptimeDB SQL commands to interact with the database.

PostgreSQL

You can use the psql command line tool to connect to the GreptimeDB. Please refer to the PostgreSQL protocol document for connection information.

After you connect to the server, you can use all GreptimeDB SQL commands to interact with the database.

GreptimeDB Dashboard

You can run SQL and visualize data in the GreptimeDB Dashboard.

GUI tools

DBeaver

Please refer to the DBeaver Integration Guide.

HTTP API

You can POST SQL queries to the GreptimeDB HTTP API to query data. Please refer to the HTTP API document for more information.