tidb mysql 查询_tidb、mysql连写两个字段名相同值不同的条件查不出数据

一、现象

今天想写个python脚步拼装sql,拼装好的sql条件中advertiser_id连续出现两次,其值不同,然后去tidb查询不正常,如下

# 这些写显示adv241418839104数据

select advertiser_id

from r_day_stat_ad_effect

where bucket_id='total' and advertiser_id in ('adv241418839104','adv2516121497984') and order_id!='total' and ad_id!='total' and mid='total' and slot_id='total' and app_id='total' and publisher_id='total' and country_code='total'

and day>20200128 and day<=20200227 and advertiser_id not in ('adv2516121497984') and test=0 group by advertiser_id;

# 没数据,即不显示adv241418839104 也不显示adv241418839104

select advertiser_id

from r_day_stat_ad_effect

where bucket_id='total' and advertiser_id in ('adv241418839104') and order_id!='total' a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Flink中使用KeyBy操作并不会改变数据的分区方式,只是将相同的键(Key)的数据分到同一个TaskManager中处理。如果您需要在KeyBy之后进行条件查询TiDB表的数据,可以使用Flink的`RichFlatMapFunction`或`RichMapFunction`,在函数的`open`方法中初始化TiDB连接,并在`flatMap`或`map`方法中执行查询操作。 以下是一个示例代码,演示了如何使用`RichFlatMapFunction`和TiDB连接器从TiDB表中查询数据: ```java import org.apache.flink.api.common.functions.RichFlatMapFunction; import org.apache.flink.configuration.Configuration; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; import org.apache.flink.table.api.EnvironmentSettings; import org.apache.flink.table.api.TableResult; import org.apache.flink.connector.jdbc.JdbcConnectionOptions; import org.apache.flink.connector.jdbc.JdbcSink; import org.apache.flink.connector.jdbc.JdbcStatementBuilder; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.table.api.Table; import org.apache.flink.types.Row; import org.apache.flink.shaded.guava18.com.google.common.collect.ImmutableList; import java.sql.*; import java.util.Properties; public class QueryTiDBDataWithKeyBy { public static void main(String[] args) { // set up the execution environment StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); StreamTableEnvironment tableEnv = StreamTableEnvironment.create( env, EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build() ); // configure TiDB properties Properties properties = new Properties(); properties.setProperty("database-url", "jdbc:mysql://<tidb_ip>:<tidb_port>/<database_name>"); properties.setProperty("username", "<tidb_username>"); properties.setProperty("password", "<tidb_password>"); properties.setProperty("driver", "com.mysql.jdbc.Driver"); // register TiDB table tableEnv.executeSql( "CREATE TABLE my_table (\n" + " id BIGINT,\n" + " name STRING,\n" + " age INT\n" + ") WITH (\n" + " 'connector' = 'jdbc',\n" + " 'properties' = '" + properties.toString() + "',\n" + " 'table-name' = 'my_table'\n" + ")" ); // KeyBy operation DataStream<Row> stream = tableEnv.toDataStream(tableEnv.from("my_table")) .keyBy(row -> row.getField("age")); // flatMap with TiDB query DataStream<Row> result = stream.flatMap(new RichFlatMapFunction<Row, Row>() { private Connection connection; private PreparedStatement statement; @Override public void open(Configuration parameters) throws Exception { super.open(parameters); connection = DriverManager.getConnection("jdbc:mysql://<tidb_ip>:<tidb_port>/<database_name>", "<tidb_username>", "<tidb_password>"); statement = connection.prepareStatement("SELECT * FROM my_table WHERE age > ?"); } @Override public void flatMap(Row row, Collector<Row> collector) throws Exception { statement.setInt(1, (int) row.getField(0)); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { collector.collect(Row.of(resultSet.getLong(1), resultSet.getString(2), resultSet.getInt(3))); } } @Override public void close() throws Exception { super.close(); statement.close(); connection.close(); } }); // print the result to console result.print(); try { env.execute(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们首先设置了TiDB连接属性,并在Flink中注册了TiDB表。然后,我们使用`keyBy`操作将数据按照年龄分组,并在`flatMap`方法中使用TiDB连接器从表中查询数据。最后,我们将结果打印到控制台。 请注意,在上面的示例中,我们使用了`RichFlatMapFunction`,这是因为在`open`方法中需要初始化TiDB连接,而在`flatMap`方法中需要执行查询操作。如果您使用`FlatMapFunction`,则无法在`open`方法中初始化TiDB连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值