Return-Path: X-Original-To: apmail-apex-dev-archive@minotaur.apache.org Delivered-To: apmail-apex-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5850F1823A for ; Tue, 23 Feb 2016 06:13:54 +0000 (UTC) Received: (qmail 75011 invoked by uid 500); 23 Feb 2016 06:13:32 -0000 Delivered-To: apmail-apex-dev-archive@apex.apache.org Received: (qmail 74868 invoked by uid 500); 23 Feb 2016 06:13:32 -0000 Mailing-List: contact dev-help@apex.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@apex.incubator.apache.org Delivered-To: mailing list dev@apex.incubator.apache.org Received: (qmail 74857 invoked by uid 99); 23 Feb 2016 06:13:31 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 Feb 2016 06:13:31 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 4332AC0B2C for ; Tue, 23 Feb 2016 06:13:31 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.021 X-Spam-Level: X-Spam-Status: No, score=-4.021 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=-0.001] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id 62i1CyrtXm0v for ; Tue, 23 Feb 2016 06:13:30 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id CFC815F1E3 for ; Tue, 23 Feb 2016 06:13:28 +0000 (UTC) Received: (qmail 74846 invoked by uid 99); 23 Feb 2016 06:13:28 -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; Tue, 23 Feb 2016 06:13:28 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id DAABFE05E1; Tue, 23 Feb 2016 06:13:27 +0000 (UTC) From: DT-Priyanka To: dev@apex.incubator.apache.org Reply-To: dev@apex.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-apex-malhar pull request: SPOI-7087 #resolve #comment Ad... Content-Type: text/plain Message-Id: <20160223061327.DAABFE05E1@git1-us-west.apache.org> Date: Tue, 23 Feb 2016 06:13:27 +0000 (UTC) Github user DT-Priyanka commented on a diff in the pull request: https://github.com/apache/incubator-apex-malhar/pull/192#discussion_r53739919 --- Diff: library/src/main/java/com/datatorrent/lib/db/jdbc/JDBCDimensionalOutputOperator.java --- @@ -0,0 +1,462 @@ +/** + * 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.PreparedStatement; +import java.sql.SQLException; + +import java.util.List; +import java.util.Map; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import com.datatorrent.api.Context; + +import com.datatorrent.lib.appdata.gpo.GPOMutable; +import com.datatorrent.lib.appdata.schemas.DimensionalConfigurationSchema; +import com.datatorrent.lib.appdata.schemas.FieldsDescriptor; +import com.datatorrent.lib.appdata.schemas.Type; +import com.datatorrent.lib.db.AbstractPassThruTransactionableStoreOutputOperator; +import com.datatorrent.lib.dimensions.DimensionsDescriptor; +import com.datatorrent.lib.dimensions.DimensionsEvent.Aggregate; +import com.datatorrent.lib.dimensions.DimensionsEvent.EventKey; +import com.datatorrent.lib.dimensions.aggregator.AggregatorRegistry; + +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; + +/** + * This operator writes updates emitted by a {@link DimensionsStoreHDHT} + * operator to a Mysql database. Updates are written to the database in the + * following fashion:
+ *
+ *
    + *
  1. Aggregates are received from an upstream + * {@link AbstractDimensionsComputationFlexibleSingleSchema} operator.
  2. + *
  3. Each aggregate is written to a different table based on its dimension + * combination, time bucket, and corresponding aggregation
  4. + *
+ */ +public class JDBCDimensionalOutputOperator + extends AbstractPassThruTransactionableStoreOutputOperator +{ + protected static int DEFAULT_BATCH_SIZE = 1000; + + @Min(1) + private int batchSize; + private final List tuples; + + private transient int batchStartIdx; + + @NotNull + private Map> tableNames; + @NotNull + private String eventSchema; + @NotNull + private AggregatorRegistry aggregatorRegistry = AggregatorRegistry.DEFAULT_AGGREGATOR_REGISTRY; + private DimensionalConfigurationSchema schema; + + private transient Map> ddIDToAggIDToStatement = Maps.newHashMap(); + + public JDBCDimensionalOutputOperator() + { + tuples = Lists.newArrayList(); + batchSize = DEFAULT_BATCH_SIZE; + batchStartIdx = 0; + store = new JdbcTransactionalStore(); + } + + @Override + public void setup(Context.OperatorContext context) + { + super.setup(context); + + LOG.info("Done setting up super"); + aggregatorRegistry.setup(); + + //Create prepared statements + schema = new DimensionalConfigurationSchema(eventSchema, aggregatorRegistry); + + List keyFDs = schema.getDimensionsDescriptorIDToKeyDescriptor(); + + for (int ddID = 0; ddID < keyFDs.size(); ddID++) { + + LOG.info("ddID {}", ddID); + FieldsDescriptor keyFD = keyFDs.get(ddID); + Int2ObjectMap aggIDToAggFD = schema + .getDimensionsDescriptorIDToAggregatorIDToOutputAggregatorDescriptor().get(ddID); + + Map aggIDToStatement = ddIDToAggIDToStatement.get(ddID); + + if (aggIDToStatement == null) { + aggIDToStatement = Maps.newHashMap(); + ddIDToAggIDToStatement.put(ddID, aggIDToStatement); + } + + for (Map.Entry aggTable : tableNames.get(ddID).entrySet()) { + int aggID = aggregatorRegistry.getIncrementalAggregatorNameToID().get(aggTable.getKey()); + + LOG.info("aggID {}", aggID); + FieldsDescriptor aggFD = aggIDToAggFD.get(aggID); + + List keyNames = keyFD.getFieldList(); + keyNames.remove(DimensionsDescriptor.DIMENSION_TIME_BUCKET); + + LOG.info("List fields {}", keyNames); + List aggregateNames = aggFD.getFieldList(); + LOG.info("List fields {}", aggregateNames); + String tableName = aggTable.getValue(); + + String statementString = buildStatement(tableName, keyNames, aggregateNames); + + try { + aggIDToStatement.put(aggID, store.getConnection().prepareStatement(statementString)); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + } + } + } + + private String buildStatement(String tableName, List keyNames, List aggregateNames) + { + LOG.info("building statement"); + StringBuilder sb = new StringBuilder(); + sb.append("INSERT INTO "); + sb.append(tableName); + sb.append(" ("); + + addList(sb, keyNames); + sb.append(","); + addList(sb, aggregateNames); + + sb.append(") VALUES ("); + + for (int qCounter = 0;; qCounter++) { + sb.append("?"); + + if (qCounter == keyNames.size() + aggregateNames.size() - 1) { + break; + } + + sb.append(","); + } + + sb.append(") ON DUPLICATE KEY UPDATE "); + + addOnDuplicate(sb, aggregateNames); + + return sb.toString(); + } + + private void addOnDuplicate(StringBuilder sb, List names) + { + LOG.info("add Duplicate"); + for (int index = 0;; index++) { + + String name = names.get(index); + sb.append(name); + sb.append("="); + sb.append("VALUES("); + sb.append(name); + sb.append(")"); + + if (index == names.size() - 1) { + break; + } + + sb.append(","); + } + } + + private void addList(StringBuilder sb, List names) + { + for (int index = 0;; index++) { + sb.append(names.get(index)); + + if (index == names.size() - 1) { + break; + } + + sb.append(","); + } + } + + /** + * This sets the table names that corresponds to the dimensions combinations + * specified in your {@link DimensionalConfigurationSchema}. The structure of + * this property is as follows:
+ *
+ *
    + *
  1. The first key is the dimension combination id assigned to a dimension + * combination in your {@link DimensionalConfigurationSchema}.
    + *
    + * The dimensions descriptor id is determined by the following factors: + *
      + *
    • The dimensions combination specified in the + * {@link DimensionalConfigurationSchema}.
    • + *
    • The the Time Buckets defined in your + * {@link DimensionalConfigurationSchema}.
    • + *
    + * The dimensions descriptor id is computed in the following way: + *
      + *
    1. The first dimensions descriptor id is 0
    2. + *
    3. A dimension combination is selected
    4. + *
    5. A time bucket is selected
    6. + *
    7. The current dimension combination and time bucket pair is assigned a + * dimensions descriptor id
    8. + *
    9. The current dimensions descriptor id is incremented
    10. + *
    11. Steps 3 - 5 are repeated until all the time buckets are done
    12. + *
    13. Steps 2 - 6 are repeated until all the dimension combinations are done. + *
    14. + *
    + *
    + * <
  2. + *
  3. The second key is the name of an aggregation being performed for that + * dimensions combination.
  4. + *
  5. The value is the name of the output Mysql table
  6. + *
+ * + * @param tableNames + * The table names that corresponds to the dimensions combinations + * specified in your {@link DimensionalConfigurationSchema}. + */ + public void setTableNames(Map> tableNames) --- End diff -- Why don't we have getters for this and other properties? --- 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. ---