简体   繁体   English

如何检查 Snowflake 中的表是否为空并获得响应 - Java,SpringBoot

[英]How to check if table in Snowflake is empty and get response for it - Java, SpringBoot

I am using Snowflake database with Spring Boot in my project.我在我的项目中使用带有 Spring Boot 的 Snowflake 数据库。

I have written a query in Java that queries the Snowflake temporary table and then copies that data in CVS format to S3.我在 Java 中编写了一个查询,该查询查询雪花临时表,然后将该数据以 CVS 格式复制到 S3。

Everything works fine until there is a user with no Snowflake data.一切正常,直到有没有雪花数据的用户。 Then the temporary table that was created is empty and no data is being copied in S3 which gives an error from S3 that objectKey is not found然后创建的临时表是空的,并且没有在 S3 中复制任何数据,这会从 S3 给出一个错误,即objectKey is not found

So I was thinking to first create temporary table and then to check it if is empty.所以我想先创建临时表,然后检查它是否为空。 If is not empty, copy data to S3 if it is, return error.如果不为空,则将数据复制到S3,如果是,则返回错误。

I have this code for creating temporary table我有这个用于创建临时表的代码

public void getUser(String userId, String dbName) {

String q = "CREATE TEMPORARY TABLE \"TEST_DATABASE\".\"PUBLIC\".\"USER_TABLE_TEMP\" AS SELECT \"USERID\", \"FIRSTNAME\", \"LASTNAME\" from \"TEST_DATABASE\".\"PUBLIC\".\"USER\"";

jdbcTemplatePerBrand.values().forEach(tab -> tab.query(q, s -> {}));

}

Then I was thinking to add this part of code to check if table is empty:然后我想添加这部分代码来检查表是否为空:

String ifTableEmpty = "SELECT CASE WHEN EXISTS(SELECT 1 FROM \"TEST_DATABASE\".\"PUBLIC\".\"USER_TABLE_TEMP\") THEN 0 ELSE 1 END AS IsEmpty;\n";

jdbcTemplatePerBrand.get(brandAfterMigration).query(ifTableEmpty, s -> {});

if(ifTableEmpty.equals("0")){
 System.out.println("");
}

From above query I am not getting any result in way of 0 or 1 (Which I would like).从上面的查询中,我没有以 0 或 1 的方式得到任何结果(我想要)。

When I run a query to check if the table is empty in Snowflake, I get this result:当我运行查询以检查 Snowflake 中的表是否为空时,我得到以下结果:

在此处输入图像描述

But I don't know how to get that result back to my code.但我不知道如何将结果返回到我的代码中。

You can query the INFORMATION_SCHEMA tables:您可以查询 INFORMATION_SCHEMA 表:

SELECT ROW_COUNT
    FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_SCHEMA = '<schema_name>'
     AND TABLE_NAME = '<table_name>';

Or use SHOW TABLES and get the result using result_scan:或者使用 SHOW TABLES 并使用 result_scan 获取结果:

SHOW TABLES LIKE 'pattern';

SELECT rows
  FROM TABLE (result_scan (last_query_id ()));

Reference: INFORMATION SCHEMA.TABLES , SHOW TABLES , RESULT_SCAN参考: INFORMATION SCHEMA.TABLESSHOW TABLESRESULT_SCAN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM