DataFlow Apache Beam Java JdbcIO读取参数问题

时间:2018-11-05 13:23:29

标签: apache jdbc google-cloud-platform google-cloud-dataflow apache-beam

我对Apache Beam和Java完全陌生。 在PHP上工作了大约5年,但是最近5年来我一直没有使用Java :),再加上Java中的Apache Beam SDK也很新,所以请耐心等待。 我想实现管道,在该管道中我将从Google PubSub获取数据,将相关字段映射到数组,然后将其检查到MySql Db,以查看消息是否属于一个表,此后,我需要将api调用发送给我们的API这将更新我们的应用数据库中的一些数据。另一个管道将丰富来自Elasticsearch的数据并将其插入BigQuery。

但是从现在起,我一直无法从MySql读取数据,我根本无法使用JdbcIO在PCollection中采用该参数。

我的计划是检查Mysql表中是否存在我从pubsub获得的当前值(值listid)。

到目前为止,这是我的代码,我们将不胜感激。

Pipeline p = Pipeline.create(options);

    org.apache.beam.sdk.values.PCollection<PubsubMessage> messages = p.apply(PubsubIO.readMessagesWithAttributes()
            .fromSubscription("*******"));

    org.apache.beam.sdk.values.PCollection<String> messages2 = messages.apply("GetPubSubEvent",
            ParDo.of(new DoFn<PubsubMessage, String>() {
                @ProcessElement
                public void processElement(ProcessContext c) {
                    Map<String, String> Map = new HashMap<String, String>();
                    PubsubMessage message = c.element();
                    String messageText = new String(message.getPayload(), StandardCharsets.UTF_8);
                    JSONObject jsonObj = new JSONObject(messageText);
                    String requestURL = jsonObj.getJSONObject("httpRequest").getString("requestUrl");
                    String query = requestURL.split("\\?")[1];
                    final Map<String, String> querymap = Splitter.on('&').trimResults().withKeyValueSeparator("=")
                            .split(query);
                    JSONObject querymapJson = new JSONObject(querymap);
                    int subscriberid = 0;
                    int listid = 0;
                    int statid = 0;
                    int points = 0;
                    String stattype = "";
                    String requesttype = "";
                    try {
                        subscriberid = querymapJson.getInt("emp_uid");
                    } catch (Exception e) {
                    }
                    try {
                        listid = querymapJson.getInt("emp_lid");
                    } catch (Exception e) {
                    }
                    try {
                        statid = querymapJson.getInt("emp_statid");
                    } catch (Exception e) {
                    }
                    try {
                        stattype = querymapJson.getString("emp_stattype");
                        Map.put("stattype", stattype);
                    } catch (Exception e) {
                    }
                    try {
                        requesttype = querymapJson.getString("type");
                    } catch (Exception e) {
                    }
                    try {
                        statid = querymapJson.getInt("leadscore");
                    } catch (Exception e) {
                    }

                    Map.put("subscriberid", String.valueOf(subscriberid));
                    Map.put("listid", String.valueOf(listid));
                    Map.put("statid", String.valueOf(statid));
                    Map.put("requesttype", requesttype);
                    Map.put("leadscore", String.valueOf(points));
                    Map.put("requestip", jsonObj.getJSONObject("httpRequest").getString("remoteIp"));
                    System.out.print("Hello from message 1");
                    c.output(Map.toString());
                }
            }));

    org.apache.beam.sdk.values.PCollection<String> messages3 = messages2.apply("Test",
            ParDo.of(new DoFn<String, String>() {
                @ProcessElement
                public void processElement(ProcessContext c) {
                    System.out.println(c.element());
                    System.out.print("Hello from message 2");
                }
            }));
    org.apache.beam.sdk.values.PCollection<KV<String, String>> messages23 = messages2.apply(JdbcIO.<KV<String, String>>read()
            .withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create("org.apache.derby.jdbc.ClientDriver",
                    "jdbc:derby://localhost:1527/beam"))
            .withQuery("select * from artist").withRowMapper(new JdbcIO.RowMapper<KV<String, String>>() {
                public KV<String, String> mapRow(ResultSet resultSet) throws Exception {
                    KV<String, String> kv = KV.of(resultSet.getString("label"), resultSet.getString("name"));
                    return kv;
                }

                @Override
                public KV<String, String> mapRow(java.sql.ResultSet resultSet) throws Exception {
                    KV<String, String> kv = KV.of(resultSet.getString("label"), resultSet.getString("name"));
                    return kv;
                }
            }).withCoder(KvCoder.of(StringUtf8Coder.of(), StringUtf8Coder.of())));


    p.run().waitUntilFinish();

0 个答案:

没有答案