Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 8ABAC200B32 for ; Thu, 9 Jun 2016 02:24:26 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 89532160A54; Thu, 9 Jun 2016 00:24:26 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id AE2B2160A2E for ; Thu, 9 Jun 2016 02:24:25 +0200 (CEST) Received: (qmail 44197 invoked by uid 500); 9 Jun 2016 00:24:24 -0000 Mailing-List: contact dev-help@apex.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@apex.apache.org Delivered-To: mailing list dev@apex.apache.org Received: (qmail 44180 invoked by uid 99); 9 Jun 2016 00:24:24 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Jun 2016 00:24:24 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 075B7180657 for ; Thu, 9 Jun 2016 00:24:24 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -5.446 X-Spam-Level: X-Spam-Status: No, score=-5.446 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id utELMikqV1xT for ; Thu, 9 Jun 2016 00:24:22 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with SMTP id 73D395FB23 for ; Thu, 9 Jun 2016 00:24:21 +0000 (UTC) Received: (qmail 43477 invoked by uid 99); 9 Jun 2016 00:24:20 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Jun 2016 00:24:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 75718DFFAB; Thu, 9 Jun 2016 00:24:20 +0000 (UTC) From: bhupeshchawda To: dev@apex.incubator.apache.org Reply-To: dev@apex.incubator.apache.org References: In-Reply-To: Subject: [GitHub] apex-malhar pull request #282: APEXMALHAR-2066 JdbcPolling,idempotent,partit... Content-Type: text/plain Message-Id: <20160609002420.75718DFFAB@git1-us-west.apache.org> Date: Thu, 9 Jun 2016 00:24:20 +0000 (UTC) archived-at: Thu, 09 Jun 2016 00:24:26 -0000 Github user bhupeshchawda commented on a diff in the pull request: https://github.com/apache/apex-malhar/pull/282#discussion_r66365162 --- Diff: library/src/main/java/com/datatorrent/lib/db/jdbc/JdbcMetaDataUtility.java --- @@ -0,0 +1,344 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.datatorrent.lib.db.jdbc; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datatorrent.lib.util.KeyValPair; + +/** + * A utility class used to retrieve the metadata for a given unique key of a SQL + * table. This class would emit range queries based on a primary index given + * + * @Input - dbName,tableName, primaryKey + * @Output - map + * + */ +public class JdbcMetaDataUtility +{ + private static String DB_DRIVER = "com.mysql.jdbc.Driver"; + private static String DB_CONNECTION = ""; + private static String DB_USER = ""; + private static String DB_PASSWORD = ""; + private static String TABLE_NAME = ""; + private static String KEY_COLUMN = ""; + private static String WHERE_CLAUSE = null; + private static String COLUMN_LIST = null; + + private static Logger LOG = LoggerFactory.getLogger(JdbcMetaDataUtility.class); + + public JdbcMetaDataUtility() + { + + } + + public JdbcMetaDataUtility(String dbConnection, String tableName, String key, String userName, String password) + { + DB_CONNECTION = dbConnection; + DB_USER = userName; + DB_PASSWORD = password; + TABLE_NAME = tableName; + KEY_COLUMN = key; + } + + private static Connection getDBConnection() + { + + Connection dbConnection = null; + + try { + Class.forName(DB_DRIVER); + } catch (ClassNotFoundException e) { + LOG.error("Driver not found", e); + } + + try { + dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); + return dbConnection; + } catch (SQLException e) { + LOG.error("Exception in getting connection handle", e); + } + + return dbConnection; + + } + + private static String generateQueryString() + { + StringBuilder sb = new StringBuilder(); + sb.append("SELECT COUNT(*) as RowCount from " + TABLE_NAME); + + if (WHERE_CLAUSE != null) { + sb.append(" WHERE " + WHERE_CLAUSE); + } + + return sb.toString(); + } + + /** + * Finds the total number of rows in the table + */ + private static long getRecordRange(String query) throws SQLException + { + long rowCount = 0; + Connection dbConnection = null; + PreparedStatement preparedStatement = null; + + try { + dbConnection = getDBConnection(); + preparedStatement = dbConnection.prepareStatement(query); + + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { + rowCount = Long.parseLong(rs.getString("RowCount")); + LOG.info("# Rows - " + rowCount); + } + + } catch (SQLException e) { + LOG.error("Exception in retreiving result set", e); + } finally { + if (preparedStatement != null) { + preparedStatement.close(); + } + if (dbConnection != null) { + dbConnection.close(); + } + } + return rowCount; + } + + /** + * Returns a pair of bounds for each partition of the + * {@link JdbcPollInputOperator}} + */ + private static KeyValPair getQueryBounds(long lower, long upper) throws SQLException + { + Connection dbConnectionLower = null; + Connection dbConnectionUpper = null; + PreparedStatement psLowerBound = null; + PreparedStatement psUpperBound = null; + + StringBuilder lowerBound = new StringBuilder(); + StringBuilder upperBound = new StringBuilder(); + + KeyValPair boundedQUeryPair = null; + + try { + dbConnectionLower = getDBConnection(); + dbConnectionUpper = getDBConnection(); + + /* + * Run this loop only for n-1 partitions. + * By default the last partition will have fewer records, since we are rounding off + * */ + + lowerBound.append("SELECT " + KEY_COLUMN + " FROM " + TABLE_NAME); + upperBound.append("SELECT " + KEY_COLUMN + " FROM " + TABLE_NAME); + + if (WHERE_CLAUSE != null) { + lowerBound.append(" WHERE " + WHERE_CLAUSE); + upperBound.append(" WHERE " + WHERE_CLAUSE); + } + + lowerBound.append(" LIMIT " + (0 + lower) + ",1"); --- End diff -- LIMIT clause might not work with all databases. Need to document this. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---