improve java log

This commit is contained in:
sheyanjie-qq 2024-08-12 11:18:25 +08:00
parent c1727ef2ae
commit 89244490ee
7 changed files with 39 additions and 24 deletions

View File

@ -37,7 +37,9 @@ public class ConsumerLoopFull {
config.setProperty("value.deserializer.encoding", "UTF-8");
try {
return new TaosConsumer<>(config);
TaosConsumer<ResultBean> consumer= new TaosConsumer<>(config);
System.out.println("Create consumer successfully, host: " + config.getProperty("bootstrap.servers") + ", groupId: " + config.getProperty("group.id") + ", clientId: " + config.getProperty("client.id"));
return consumer;
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
@ -98,7 +100,6 @@ public class ConsumerLoopFull {
consumer.seekToBeginning(assignment);
System.out.println("assignment seek to beginning successfully");
System.out.println("beginning assignment: " + JSON.toJSONString(assignment));
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
@ -127,6 +128,7 @@ public class ConsumerLoopFull {
if (!records.isEmpty()) {
// after processing the data, commit the offset manually
consumer.commitSync();
System.out.println("commit offset manually successfully.");
}
}
} catch (SQLException ex) {
@ -147,6 +149,7 @@ public class ConsumerLoopFull {
try {
// unsubscribe the consumer
consumer.unsubscribe();
System.out.println("unsubscribe consumer successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
@ -158,6 +161,7 @@ public class ConsumerLoopFull {
finally {
// close the consumer
consumer.close();
System.out.println("consumer closed successfully.");
}
// ANCHOR_END: unsubscribe_data_code_piece
}

View File

@ -30,17 +30,11 @@ public class JdbcCreatDBDemo {
// create database
int rowsAffected = stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS power");
// you can check rowsAffected here
assert rowsAffected == 0;
// use database
rowsAffected = stmt.executeUpdate("USE power");
// you can check rowsAffected here
assert rowsAffected == 0;
System.out.println("Create database power successfully, rowsAffected: " + rowsAffected);
// create table
rowsAffected = stmt.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
rowsAffected = stmt.executeUpdate("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
// you can check rowsAffected here
assert rowsAffected == 0;
System.out.println("Create stable power.meters successfully, rowsAffected: " + rowsAffected);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info

View File

@ -39,7 +39,7 @@ public class JdbcInsertDataDemo {
"(NOW + 1a, 10.30000, 218, 0.25000) ";
int affectedRows = stmt.executeUpdate(insertQuery);
// you can check affectedRows here
System.out.println("inserted into " + affectedRows + " rows to power.meters successfully.");
System.out.println("Successfully inserted " + affectedRows + " rows to power.meters.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert data to power.meters, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());

View File

@ -25,22 +25,32 @@ public class JdbcReqIdDemo {
System.out.println("get connection starting...");
// ANCHOR: with_reqid
long reqId = 3L;
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
// Create a statement that allows specifying a request ID
AbstractStatement aStmt = (AbstractStatement) connection.createStatement()) {
try (ResultSet rs = aStmt.executeQuery("SELECT ts, current, location FROM power.meters limit 1", 3L)) {
while (rs.next()) {
Timestamp timestamp = rs.getTimestamp(1);
System.out.println("timestamp = " + timestamp);
try (ResultSet resultSet = aStmt.executeQuery("SELECT ts, current, location FROM power.meters limit 1", reqId)) {
Timestamp ts;
float current;
String location;
while (resultSet.next()) {
ts = resultSet.getTimestamp(1);
current = resultSet.getFloat(2);
// we recommend using the column name to get the value
location = resultSet.getString("location");
// you can check data here
System.out.printf("ts: %s, current: %f, location: %s %n", ts, current, location);
}
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to execute sql with reqId, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
System.out.println("Failed to execute sql with reqId: " + reqId + ", url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw ex;
} catch (Exception ex){
System.out.println("Failed to execute sql with reqId, url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage());
System.out.println("Failed to execute sql with reqId: " + reqId + ", url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage());
throw ex;
}
// ANCHOR_END: with_reqid

View File

@ -24,7 +24,7 @@ public class ParameterBindingBasicDemo {
init(conn);
String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
String sql = "INSERT INTO ? USING power.meters TAGS(?,?) VALUES (?,?,?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
@ -65,6 +65,8 @@ public class ParameterBindingBasicDemo {
}
// execute column
pstmt.columnDataExecuteBatch();
// you can check exeResult here
System.out.println("Successfully inserted " + (numOfSubTable * numOfRow) + " rows to power.meters.");
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info

View File

@ -21,7 +21,7 @@ public class WSParameterBindingBasicDemo {
try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) {
init(conn);
String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
String sql = "INSERT INTO ? USING power.meters TAGS(?,?) VALUES (?,?,?,?)";
try (TSWSPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement.class)) {
@ -44,7 +44,7 @@ public class WSParameterBindingBasicDemo {
}
int [] exeResult = pstmt.executeBatch();
// you can check exeResult here
System.out.println("insert " + exeResult.length + " rows.");
System.out.println("Successfully inserted " + exeResult.length + " rows to power.meters.");
}
}
} catch (SQLException ex) {
@ -61,7 +61,7 @@ public class WSParameterBindingBasicDemo {
try (Statement stmt = conn.createStatement()) {
stmt.execute("CREATE DATABASE IF NOT EXISTS power");
stmt.execute("USE power");
stmt.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
stmt.execute("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
}
}
}

View File

@ -10,6 +10,7 @@ import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
// ANCHOR: consumer_demo
public class WsConsumerLoopFull {
@ -34,7 +35,9 @@ public class WsConsumerLoopFull {
config.setProperty("value.deserializer.encoding", "UTF-8");
try {
return new TaosConsumer<>(config);
TaosConsumer<ResultBean> consumer= new TaosConsumer<>(config);
System.out.println("Create consumer successfully, host: " + config.getProperty("bootstrap.servers") + ", groupId: " + config.getProperty("group.id") + ", clientId: " + config.getProperty("client.id"));
return consumer;
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
@ -95,7 +98,6 @@ public class WsConsumerLoopFull {
consumer.seekToBeginning(assignment);
System.out.println("assignment seek to beginning successfully");
System.out.println("beginning assignment: " + JSON.toJSONString(assignment));
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
@ -124,6 +126,7 @@ public class WsConsumerLoopFull {
if (!records.isEmpty()) {
// after processing the data, commit the offset manually
consumer.commitSync();
System.out.println("commit offset manually successfully.");
}
}
} catch (SQLException ex) {
@ -144,6 +147,7 @@ public class WsConsumerLoopFull {
try {
// unsubscribe the consumer
consumer.unsubscribe();
System.out.println("unsubscribe consumer successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
@ -155,6 +159,7 @@ public class WsConsumerLoopFull {
finally {
// close the consumer
consumer.close();
System.out.println("consumer closed successfully.");
}
// ANCHOR_END: unsubscribe_data_code_piece
}