我按以下方式获取数据库中所有表的行数。
public Map<String,Long> getGpTableCount() throws SQLException {
Map<String, String> gpTableMap = getScopeTableList();
Set<String> gpTableSet = gpTableMap.keySet(); // Gets all table names
gpCountMap = new HashMap<String, Long>();
Iterator<String> keySetIterator_gpTables = gpTableSet.iterator();
Connection gpAnalyticsCon = (Connection) DbManager.getGpConnection();
while(keySetIterator_gpTables.hasNext()) {
String gpSchemaTableName = keySetIterator_gpTables.next(); // Ex: dbname.tablename
String[] gpparts = gpSchemaTableName.split("\\."); // dbname, tablename
String gpTable = gpparts[1];
String gpCountQuery = "select '" + gpTable + "' as TableName, count(*) as Count, source_system_name from " + gpSchemaTableName + " group by source_system_name order by source_system_name";
// resultSet(1): TableName, resultSet(2): count, resultSet(3): source_system_name
try {
PreparedStatement gp_pstmnt = gpAnalyticsCon.prepareStatement(gpCountQuery);
ResultSet gpCountRs = gp_pstmnt.executeQuery();
while(gpCountRs.next()) {
System.out.println("Tablename: " + gpCountRs.getString(1) + " Source System Name: " + gpCountRs.getString(3) + ", Count: " + gpCountRs.getLong(2));
gpCountMap.put(gpTable + ": " + gpCountRs.getString(3), gpCountRs.getLong(2));
}
} catch(SQLException e) {
System.out.println("Table: " + gpTable + " not found on GP for the source system: ");
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
gpAnalyticsCon.close();
return gpCountMap;
}
我在这里面临的问题是,如果我想获取特定source_system_name的计数并将查询更改为String gpCountQuery = "select '" + gpTable + "' as TableName, count(*) as Count, source_system_name from " + gpSchemaTableName + " where source_system_name in ('ABCD','EFGH') group by source_system_name order by source_system_name";
列中没有值&quot; source_system_name&#39;我正在获取专栏&#34; source_system_name&#34;不存在例外,可以在下面看到。
Table: some_table_name not found on GP for the source system:
org.postgresql.util.PSQLException: ERROR: column "source_system_name" does not exist
Position: 64
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:116)
at com.recordcount.dao.GpHiveRecords.getGpTableCount(GpHiveRecords.java:69)
我在查询中给出了source_system_name&#39;&#39;直接条款。 由于在执行查询之前没有检查,因此会导致异常。
有没有人让我知道如何在执行count查询之前在代码中添加一个条件来检查表中是否包含给定source_system_name的值,并且仅当它存在时才获取它的计数。