Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/Documentation/UserGuide/6-JDBC API/1-JDBC API.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
## How to package only jdbc project

In root directory:
> mvn clean package -pl jdbc -am -Dmaven.test.skip=true
```
mvn clean package -pl jdbc -am -Dmaven.test.skip=true
```

## How to install in local maven repository

In root directory:
> mvn clean install -pl jdbc -am -Dmaven.test.skip=true
```
mvn clean install -pl jdbc -am -Dmaven.test.skip=true
```

## Using IoTDB JDBC with Maven

Expand Down Expand Up @@ -99,6 +103,9 @@ public class JDBCExample {
//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());
Expand Down
4 changes: 2 additions & 2 deletions jdbc/src/main/java/org/apache/iotdb/jdbc/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private Constant(){}

public static final String GLOBAL_COLUMN_REQ = "COLUMN";

public static final String GLOBAL_DELTA_OBJECT_REQ = "DELTA_OBEJECT";
public static final String GLOBAL_SHOW_DEVICES_REQ = "SHOW_DEVICES";

public static final String GLOBAL_SHOW_TIMESERIES_REQ = "SHOW_TIMESERIES";

Expand All @@ -46,7 +46,7 @@ private Constant(){}
public static final String CATALOG_COLUMN = "col";
public static final String CATALOG_TIMESERIES = "ts";
public static final String CATALOG_STORAGE_GROUP = "sg";
public static final String CATALOG_DEVICE = "delta";
public static final String CATALOG_DEVICES = "devices";
public static final String COUNT_TIMESERIES = "cntts";
public static final String COUNT_NODE_TIMESERIES = "cnttsbg";
public static final String COUNT_NODES = "cntnode";
Expand Down
25 changes: 13 additions & 12 deletions jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBDatabaseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.sql.*;
import java.util.List;
import java.util.Set;
import org.apache.iotdb.jdbc.IoTDBMetadataResultSet.MetadataType;
import org.apache.iotdb.rpc.IoTDBRPCException;
import org.apache.iotdb.rpc.RpcUtils;
import org.apache.iotdb.service.rpc.thrift.TSFetchMetadataReq;
Expand Down Expand Up @@ -85,12 +86,12 @@ private ResultSet getColumnsFunc(String catalog, String schemaPattern)
} catch (IoTDBRPCException e) {
throw new IoTDBSQLException(e.getMessage());
}
return new IoTDBMetadataResultSet(resp.getColumnsList(), IoTDBMetadataResultSet.MetadataType.COLUMN);
return new IoTDBMetadataResultSet(resp.getColumnsList(), MetadataType.COLUMN);
} catch (TException e) {
throw new TException("Conncetion error when fetching column metadata", e);
}
case Constant.CATALOG_DEVICE:
req = new TSFetchMetadataReq(Constant.GLOBAL_DELTA_OBJECT_REQ);
case Constant.CATALOG_DEVICES:
req = new TSFetchMetadataReq(Constant.GLOBAL_SHOW_DEVICES_REQ);
req.setColumnPath(schemaPattern);
try {
TSFetchMetadataResp resp = client.fetchMetadata(req);
Expand All @@ -99,9 +100,9 @@ private ResultSet getColumnsFunc(String catalog, String schemaPattern)
} catch (IoTDBRPCException e) {
throw new IoTDBSQLException(e.getMessage());
}
return new IoTDBMetadataResultSet(resp.getColumnsList(), IoTDBMetadataResultSet.MetadataType.COLUMN);
return new IoTDBMetadataResultSet(resp.getDevices(), MetadataType.DEVICES);
} catch (TException e) {
throw new TException("Conncetion error when fetching delta object metadata", e);
throw new TException("Conncetion error when fetching device metadata", e);
}
case Constant.CATALOG_STORAGE_GROUP:
req = new TSFetchMetadataReq(Constant.GLOBAL_SHOW_STORAGE_GROUP_REQ);
Expand All @@ -112,8 +113,8 @@ private ResultSet getColumnsFunc(String catalog, String schemaPattern)
} catch (IoTDBRPCException e) {
throw new IoTDBSQLException(e.getMessage());
}
Set<String> showStorageGroup = resp.getShowStorageGroups();
return new IoTDBMetadataResultSet(showStorageGroup, IoTDBMetadataResultSet.MetadataType.STORAGE_GROUP);
Set<String> showStorageGroup = resp.getStorageGroups();
return new IoTDBMetadataResultSet(showStorageGroup, MetadataType.STORAGE_GROUP);
} catch (TException e) {
throw new TException("Conncetion error when fetching storage group metadata", e);
}
Expand All @@ -127,8 +128,8 @@ private ResultSet getColumnsFunc(String catalog, String schemaPattern)
} catch (IoTDBRPCException e) {
throw new IoTDBSQLException(e.getMessage());
}
List<List<String>> showTimeseriesList = resp.getShowTimeseriesList();
return new IoTDBMetadataResultSet(showTimeseriesList, IoTDBMetadataResultSet.MetadataType.TIMESERIES);
List<List<String>> showTimeseriesList = resp.getTimeseriesList();
return new IoTDBMetadataResultSet(showTimeseriesList, MetadataType.TIMESERIES);
} catch (TException e) {
throw new TException("Conncetion error when fetching timeseries metadata", e);
}
Expand All @@ -142,7 +143,7 @@ private ResultSet getColumnsFunc(String catalog, String schemaPattern)
} catch (IoTDBRPCException e) {
throw new IoTDBSQLException(e.getMessage());
}
return new IoTDBMetadataResultSet(resp.getColumnsList().size(), IoTDBMetadataResultSet.MetadataType.COUNT_TIMESERIES);
return new IoTDBMetadataResultSet(resp.getColumnsList().size(), MetadataType.COUNT_TIMESERIES);
} catch (TException e) {
throw new TException("Connection error when fetching timeseries metadata", e);
}
Expand All @@ -153,7 +154,7 @@ private ResultSet getColumnsFunc(String catalog, String schemaPattern)
}

public ResultSet getNodes(String catalog, String schemaPattern, String columnPattern,
String devicePattern, String nodeLevel) throws SQLException {
String devicePattern, int nodeLevel) throws SQLException {
try {
return getNodesFunc(catalog, nodeLevel);
} catch (TException e) {
Expand All @@ -178,7 +179,7 @@ public ResultSet getNodes(String catalog, String schemaPattern, String columnPat
}
}

private ResultSet getNodesFunc(String catalog, String nodeLevel) throws TException, SQLException {
private ResultSet getNodesFunc(String catalog, int nodeLevel) throws TException, SQLException {
TSFetchMetadataReq req;
switch (catalog) {
case Constant.COUNT_NODES:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public class IoTDBMetadataResultSet extends IoTDBQueryResultSet {
public static final String GET_STRING_NODE_PATH = "NODE_PATH";
public static final String GET_STRING_NODE_TIMESERIES_NUM = "NODE_TIMESERIES_NUM";
public static final String GET_STRING_TIMESERIES_NAME = "Timeseries";
public static final String GET_STRING_DEVICES = "DEVICES";
public static final String GET_STRING_TIMESERIES_STORAGE_GROUP = "Storage Group";
public static final String GET_STRING_TIMESERIES_DATATYPE = "DataType";
public static final String GET_STRING_TIMESERIES_ENCODING = "Encoding";
private Iterator<?> columnItr;
private MetadataType type;
private String currentColumn;
private String currentStorageGroup;
private String currentDevice;
private List<String> currentTimeseries;
private List<String> timeseriesNumList;
private List<String> nodesNumList;
Expand Down Expand Up @@ -71,6 +73,12 @@ public IoTDBMetadataResultSet(Object object, MetadataType type) throws SQLExcept
showLabels = new String[]{"Storage Group"};
columnItr = storageGroupSet.iterator();
break;
case DEVICES:
Set<String> devicesSet = (Set<String>) object;
colCount = 1;
showLabels = new String[]{"Device"};
columnItr = devicesSet.iterator();
break;
case TIMESERIES:
List<List<String>> showTimeseriesList = (List<List<String>>) object;
colCount = 4;
Expand Down Expand Up @@ -239,6 +247,9 @@ public boolean next() throws SQLException {
case COLUMN:
currentColumn = (String) columnItr.next();
break;
case DEVICES:
currentDevice = (String) columnItr.next();
break;
case COUNT_TIMESERIES:
timeseriesNum = (String) columnItr.next();
break;
Expand Down Expand Up @@ -300,6 +311,10 @@ public String getString(int columnIndex) throws SQLException {
return getString(GET_STRING_COLUMN);
}
break;
case DEVICES:
if (columnIndex == 1) {
return getString(GET_STRING_DEVICES);
}
case COUNT_TIMESERIES:
if (columnIndex == 1) {
return getString(GET_STRING_TIMESERIES_NUM);
Expand Down Expand Up @@ -333,6 +348,8 @@ public String getString(String columnName) throws SQLException {
return currentTimeseries.get(3);
case GET_STRING_COLUMN:
return currentColumn;
case GET_STRING_DEVICES:
return currentDevice;
case GET_STRING_TIMESERIES_NUM:
return timeseriesNum;
case GET_STRING_NODES_NUM:
Expand Down Expand Up @@ -378,6 +395,6 @@ public boolean wasNull() throws SQLException {
}

public enum MetadataType {
STORAGE_GROUP, TIMESERIES, COLUMN, COUNT_TIMESERIES, COUNT_NODES, COUNT_NODE_TIMESERIES
STORAGE_GROUP, TIMESERIES, COLUMN, DEVICES, COUNT_TIMESERIES, COUNT_NODES, COUNT_NODE_TIMESERIES
}
}
9 changes: 7 additions & 2 deletions jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class IoTDBStatement implements Statement {

private static final String SHOW_TIMESERIES_COMMAND_LOWERCASE = "show timeseries";
private static final String SHOW_STORAGE_GROUP_COMMAND_LOWERCASE = "show storage group";
private static final String SHOW_DEVICES_COMMAND_LOWERCASE = "show devices";
private static final String COUNT_TIMESERIES_COMMAND_LOWERCASE = "count timeseries";
private static final String COUNT_NODES_COMMAND_LOWERCASE = "count nodes";
private static final String METHOD_NOT_SUPPORTED_STRING = "Method not supported";
Expand Down Expand Up @@ -252,6 +253,10 @@ private boolean executeSQL(String sql) throws TException, SQLException {
DatabaseMetaData databaseMetaData = connection.getMetaData();
resultSet = databaseMetaData.getColumns(Constant.CATALOG_STORAGE_GROUP, null, null, null);
return true;
} else if (sqlToLowerCase.equals(SHOW_DEVICES_COMMAND_LOWERCASE)) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
resultSet = databaseMetaData.getColumns(Constant.CATALOG_DEVICES, null, null, null);
return true;
} else if (sqlToLowerCase.startsWith(COUNT_TIMESERIES_COMMAND_LOWERCASE)) {
String[] cmdSplited = sqlToLowerCase.split("\\s+", 4);
if (cmdSplited.length != 3 && !(cmdSplited.length == 4 && cmdSplited[3].startsWith("group by level"))) {
Expand All @@ -265,7 +270,7 @@ private boolean executeSQL(String sql) throws TException, SQLException {
return true;
} else {
String path = cmdSplited[2];
String level = cmdSplited[3].replaceAll(" ", "").substring(13);
int level = Integer.parseInt(cmdSplited[3].replaceAll(" ", "").substring(13));
IoTDBDatabaseMetadata databaseMetadata = (IoTDBDatabaseMetadata) connection.getMetaData();
resultSet = databaseMetadata.getNodes(Constant.COUNT_NODE_TIMESERIES, path, null, null, level);
return true;
Expand All @@ -276,7 +281,7 @@ private boolean executeSQL(String sql) throws TException, SQLException {
throw new SQLException("Error format of \'COUNT NODES LEVEL=<INTEGER>\'");
} else {
String path = cmdSplited[2];
String level = cmdSplited[3].replaceAll(" ", "").substring(6);
int level = Integer.parseInt(cmdSplited[3].replaceAll(" ", "").substring(6));
IoTDBDatabaseMetadata databaseMetaData = (IoTDBDatabaseMetadata) connection.getMetaData();
resultSet = databaseMetaData.getNodes(Constant.COUNT_NODES, path, null, null, level);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void AllColumns() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}
}

Expand Down Expand Up @@ -143,6 +144,7 @@ public void CountTimeseries() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}
}

Expand All @@ -163,7 +165,7 @@ public void CountNodes() throws Exception {
String standard = "count,\n" + "4,\n";
try {
IoTDBDatabaseMetadata metadata = (IoTDBDatabaseMetadata) databaseMetaData;
String level = "3";
int level = 3;
ResultSet resultSet = metadata.getNodes(Constant.COUNT_NODES, "root", null, null, level);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int colCount = resultSetMetaData.getColumnCount();
Expand All @@ -181,6 +183,7 @@ public void CountNodes() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}
}

Expand All @@ -205,7 +208,7 @@ public void CountNodeTimeseries() throws Exception {
+ "root.vehicle.d4,2,\n";
try {
IoTDBDatabaseMetadata metadata = (IoTDBDatabaseMetadata) databaseMetaData;
String level = "3";
int level = 3;
ResultSet resultSet = metadata.getNodes(Constant.COUNT_NODE_TIMESERIES, "root", null, null, level);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int colCount = resultSetMetaData.getColumnCount();
Expand All @@ -223,6 +226,7 @@ public void CountNodeTimeseries() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}
}

Expand All @@ -231,7 +235,7 @@ public void CountNodeTimeseries() throws Exception {
*/
@SuppressWarnings("resource")
@Test
public void device() throws Exception {
public void deviceUnderColumn() throws Exception {
List<String> columnList = new ArrayList<>();
columnList.add("root.vehicle.d0");

Expand All @@ -240,7 +244,42 @@ public void device() throws Exception {
String standard = "column,\n" + "root.vehicle.d0,\n";
try {
ResultSet resultSet = databaseMetaData
.getColumns(Constant.CATALOG_DEVICE, "vehicle", null, null);
.getColumns(Constant.CATALOG_COLUMN, "vehicle", null, null);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int colCount = resultSetMetaData.getColumnCount();
StringBuilder resultStr = new StringBuilder();
for (int i = 1; i < colCount + 1; i++) {
resultStr.append(resultSetMetaData.getColumnName(i)).append(",");
}
resultStr.append("\n");
while (resultSet.next()) {
for (int i = 1; i <= colCount; i++) {
resultStr.append(resultSet.getString(i)).append(",");
}
resultStr.append("\n");
}
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply print out the exception may bury the error and the TestCase will be regarded as PASSED. It's better to replace it with e.printStackTrace(); fail(e.getMessage());.

Assert.fail(e.getMessage());
}
}

/**
* get all devices
*/
@SuppressWarnings("resource")
@Test
public void device() throws Exception {
Set<String> devicesSet = new HashSet<>();
devicesSet.add("root.vehicle.d0");

when(fetchMetadataResp.getDevices()).thenReturn(devicesSet);

String standard = "Device,\n" + "root.vehicle.d0,\n";
try {
ResultSet resultSet = databaseMetaData
.getColumns(Constant.CATALOG_DEVICES, null, null, null);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int colCount = resultSetMetaData.getColumnCount();
StringBuilder resultStr = new StringBuilder();
Expand All @@ -257,6 +296,7 @@ public void device() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}
}

Expand Down Expand Up @@ -292,7 +332,7 @@ public void ShowTimeseriesPath1() throws Exception {
}
});

when(fetchMetadataResp.getShowTimeseriesList()).thenReturn(tslist);
when(fetchMetadataResp.getTimeseriesList()).thenReturn(tslist);

String standard = "Timeseries,Storage Group,DataType,Encoding,\n"
+ "root.vehicle.d0.s0,root.vehicle,INT32,RLE,\n"
Expand All @@ -316,6 +356,7 @@ public void ShowTimeseriesPath1() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}
}

Expand All @@ -335,7 +376,7 @@ public void ShowTimeseriesPath2() throws Exception {
}
});

when(fetchMetadataResp.getShowTimeseriesList()).thenReturn(tslist);
when(fetchMetadataResp.getTimeseriesList()).thenReturn(tslist);

String standard = "DataType,\n" + "INT32,\n";
try (ResultSet resultSet = databaseMetaData
Expand All @@ -352,6 +393,7 @@ public void ShowTimeseriesPath2() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}
}

Expand All @@ -363,7 +405,7 @@ public void ShowTimeseriesPath2() throws Exception {
public void ShowStorageGroup() throws Exception {
Set<String> sgSet = new HashSet<>();
sgSet.add("root.vehicle");
when(fetchMetadataResp.getShowStorageGroups()).thenReturn(sgSet);
when(fetchMetadataResp.getStorageGroups()).thenReturn(sgSet);

String standard = "Storage Group,\n" + "root.vehicle,\n";
try (ResultSet resultSet = databaseMetaData
Expand All @@ -384,7 +426,9 @@ public void ShowStorageGroup() throws Exception {
Assert.assertEquals(resultStr.toString(), standard);
} catch (SQLException e) {
System.out.println(e);
Assert.fail(e.getMessage());
}

}

/**
Expand Down
Loading