未验证 提交 ae919911 编写于 作者: H huolibo 提交者: GitHub

[TD-11711]<fix>:make schemaless insert as an independent class, not associated...

[TD-11711]<fix>:make schemaless insert as an independent class, not associated with statement (#8974)

* make schemaless insert as an indepondent class

* add schemaless list as param
上级 830c6808
package com.taosdata.jdbc;
import java.sql.*;
public class AbstractStatementWrapper extends AbstractStatement{
protected Statement statement;
public AbstractStatementWrapper(Statement statement) {
this.statement = statement;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
return statement.executeQuery(sql);
}
@Override
public int executeUpdate(String sql) throws SQLException {
return statement.executeUpdate(sql);
}
@Override
public void close() throws SQLException {
statement.close();
}
@Override
public boolean execute(String sql) throws SQLException {
return statement.execute(sql);
}
@Override
public ResultSet getResultSet() throws SQLException {
return statement.getResultSet();
}
@Override
public int getUpdateCount() throws SQLException {
return statement.getUpdateCount();
}
@Override
public Connection getConnection() throws SQLException {
return statement.getConnection();
}
@Override
public boolean isClosed() throws SQLException {
return statement.isClosed();
}
}
...@@ -6,30 +6,30 @@ import com.taosdata.jdbc.rs.RestfulConnection; ...@@ -6,30 +6,30 @@ import com.taosdata.jdbc.rs.RestfulConnection;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.util.List;
/** /**
* this class is an extension of {@link Statement}. e.g.: * This class is for schemaless lines(line/telnet/json) write to tdengine.
* Statement statement = conn.createStatement(); * e.g.:
* SchemalessStatement schemalessStatement = new SchemalessStatement(statement); * SchemalessWriter writer = new SchemalessWriter(connection);
* schemalessStatement.execute(sql); * writer.write(lines, SchemalessProtocolType, SchemalessTimestampType);
* schemalessStatement.insert(lines, SchemalessProtocolType, SchemalessTimestampType);
*/ */
public class SchemalessStatement extends AbstractStatementWrapper { public class SchemalessWriter {
public SchemalessStatement(Statement statement) { protected Connection connection;
super(statement);
public SchemalessWriter(Connection connection) {
this.connection = connection;
} }
/** /**
* batch insert schemaless lines * batch schemaless lines write to db
* *
* @param lines schemaless lines * @param lines schemaless lines
* @param protocolType schemaless type {@link SchemalessProtocolType} * @param protocolType schemaless type {@link SchemalessProtocolType}
* @param timestampType Time precision {@link SchemalessTimestampType} * @param timestampType Time precision {@link SchemalessTimestampType}
* @throws SQLException execute insert exception * @throws SQLException execute exception
*/ */
public void insert(String[] lines, SchemalessProtocolType protocolType, SchemalessTimestampType timestampType) throws SQLException { public void write(String[] lines, SchemalessProtocolType protocolType, SchemalessTimestampType timestampType) throws SQLException {
Connection connection = this.getConnection();
if (connection instanceof TSDBConnection) { if (connection instanceof TSDBConnection) {
TSDBConnection tsdbConnection = (TSDBConnection) connection; TSDBConnection tsdbConnection = (TSDBConnection) connection;
tsdbConnection.getConnector().insertLines(lines, protocolType, timestampType); tsdbConnection.getConnector().insertLines(lines, protocolType, timestampType);
...@@ -41,14 +41,27 @@ public class SchemalessStatement extends AbstractStatementWrapper { ...@@ -41,14 +41,27 @@ public class SchemalessStatement extends AbstractStatementWrapper {
} }
/** /**
* only one insert * only one line writes to db
* *
* @param line schemaless line * @param line schemaless line
* @param protocolType schemaless type {@link SchemalessProtocolType} * @param protocolType schemaless type {@link SchemalessProtocolType}
* @param timestampType Time precision {@link SchemalessTimestampType} * @param timestampType Time precision {@link SchemalessTimestampType}
* @throws SQLException execute insert exception * @throws SQLException execute exception
*/
public void write(String line, SchemalessProtocolType protocolType, SchemalessTimestampType timestampType) throws SQLException {
write(new String[]{line}, protocolType, timestampType);
}
/**
* batch schemaless lines write to db with list
*
* @param lines schemaless list
* @param protocolType schemaless type {@link SchemalessProtocolType}
* @param timestampType Time precision {@link SchemalessTimestampType}
* @throws SQLException execute exception
*/ */
public void insert(String line, SchemalessProtocolType protocolType, SchemalessTimestampType timestampType) throws SQLException { public void write(List<String> lines, SchemalessProtocolType protocolType, SchemalessTimestampType timestampType) throws SQLException {
insert(new String[]{line}, protocolType, timestampType); String[] strings = lines.toArray(new String[0]);
write(strings, protocolType, timestampType);
} }
} }
...@@ -10,6 +10,8 @@ import org.junit.Before; ...@@ -10,6 +10,8 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.sql.*; import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SchemalessInsertTest { public class SchemalessInsertTest {
private final String dbname = "test_schemaless_insert"; private final String dbname = "test_schemaless_insert";
...@@ -27,10 +29,8 @@ public class SchemalessInsertTest { ...@@ -27,10 +29,8 @@ public class SchemalessInsertTest {
"st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000", "st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000",
"st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000"}; "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000"};
// when // when
try (Statement statement = conn.createStatement(); SchemalessWriter writer = new SchemalessWriter(conn);
SchemalessStatement schemalessStatement = new SchemalessStatement(statement)) { writer.write(lines, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO_SECONDS);
schemalessStatement.insert(lines, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO_SECONDS);
}
// then // then
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
...@@ -62,10 +62,9 @@ public class SchemalessInsertTest { ...@@ -62,10 +62,9 @@ public class SchemalessInsertTest {
}; };
// when // when
try (Statement statement = conn.createStatement();
SchemalessStatement schemalessStatement = new SchemalessStatement(statement)) { SchemalessWriter writer = new SchemalessWriter(conn);
schemalessStatement.insert(lines, SchemalessProtocolType.TELNET, SchemalessTimestampType.NOT_CONFIGURED); writer.write(lines, SchemalessProtocolType.TELNET, SchemalessTimestampType.NOT_CONFIGURED);
}
// then // then
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
...@@ -114,10 +113,8 @@ public class SchemalessInsertTest { ...@@ -114,10 +113,8 @@ public class SchemalessInsertTest {
"]"; "]";
// when // when
try (Statement statement = conn.createStatement(); SchemalessWriter writer = new SchemalessWriter(conn);
SchemalessStatement schemalessStatement = new SchemalessStatement(statement)) { writer.write(json, SchemalessProtocolType.JSON, SchemalessTimestampType.NOT_CONFIGURED);
schemalessStatement.insert(json, SchemalessProtocolType.JSON, SchemalessTimestampType.NOT_CONFIGURED);
}
// then // then
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
...@@ -135,6 +132,33 @@ public class SchemalessInsertTest { ...@@ -135,6 +132,33 @@ public class SchemalessInsertTest {
statement.close(); statement.close();
} }
@Test
public void telnetListInsert() throws SQLException {
// given
List<String> list = new ArrayList<>();
list.add("stb0_0 1626006833 4 host=host0 interface=eth0");
list.add("stb0_1 1626006833 4 host=host0 interface=eth0");
list.add("stb0_2 1626006833 4 host=host0 interface=eth0 id=\"special_name\"");
// when
SchemalessWriter writer = new SchemalessWriter(conn);
writer.write(list, SchemalessProtocolType.TELNET, SchemalessTimestampType.NOT_CONFIGURED);
// then
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("show tables");
Assert.assertNotNull(rs);
ResultSetMetaData metaData = rs.getMetaData();
Assert.assertTrue(metaData.getColumnCount() > 0);
int rowCnt = 0;
while (rs.next()) {
rowCnt++;
}
Assert.assertEquals(list.size(), rowCnt);
rs.close();
statement.close();
}
@Before @Before
public void before() { public void before() {
String host = "127.0.0.1"; String host = "127.0.0.1";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册