apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From DT-Priyanka <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: SPOI-7087 #resolve #comment Ad...
Date Tue, 23 Feb 2016 06:05:43 GMT
Github user DT-Priyanka commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/192#discussion_r53739520
  
    --- 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: <br/>
    + * <br/>
    + * <ol>
    + * <li>Aggregates are received from an upstream
    + * {@link AbstractDimensionsComputationFlexibleSingleSchema} operator.</li>
    + * <li>Each aggregate is written to a different table based on its dimension
    + * combination, time bucket, and corresponding aggregation</li>
    + * </ol>
    + */
    +public class JDBCDimensionalOutputOperator
    +    extends AbstractPassThruTransactionableStoreOutputOperator<Aggregate, JdbcTransactionalStore>
    +{
    +  protected static int DEFAULT_BATCH_SIZE = 1000;
    +
    +  @Min(1)
    +  private int batchSize;
    +  private final List<Aggregate> tuples;
    +
    +  private transient int batchStartIdx;
    +
    +  @NotNull
    +  private Map<Integer, Map<String, String>> tableNames;
    +  @NotNull
    +  private String eventSchema;
    +  @NotNull
    +  private AggregatorRegistry aggregatorRegistry = AggregatorRegistry.DEFAULT_AGGREGATOR_REGISTRY;
    +  private DimensionalConfigurationSchema schema;
    +
    +  private transient Map<Integer, Map<Integer, PreparedStatement>> 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<FieldsDescriptor> keyFDs = schema.getDimensionsDescriptorIDToKeyDescriptor();
    +
    +    for (int ddID = 0; ddID < keyFDs.size(); ddID++) {
    +
    +      LOG.info("ddID {}", ddID);
    +      FieldsDescriptor keyFD = keyFDs.get(ddID);
    +      Int2ObjectMap<FieldsDescriptor> aggIDToAggFD = schema
    +          .getDimensionsDescriptorIDToAggregatorIDToOutputAggregatorDescriptor().get(ddID);
    +
    +      Map<Integer, PreparedStatement> aggIDToStatement = ddIDToAggIDToStatement.get(ddID);
    +
    +      if (aggIDToStatement == null) {
    +        aggIDToStatement = Maps.newHashMap();
    +        ddIDToAggIDToStatement.put(ddID, aggIDToStatement);
    +      }
    +
    +      for (Map.Entry<String, String> aggTable : tableNames.get(ddID).entrySet())
{
    +        int aggID = aggregatorRegistry.getIncrementalAggregatorNameToID().get(aggTable.getKey());
    +
    +        LOG.info("aggID {}", aggID);
    +        FieldsDescriptor aggFD = aggIDToAggFD.get(aggID);
    +
    +        List<String> keyNames = keyFD.getFieldList();
    +        keyNames.remove(DimensionsDescriptor.DIMENSION_TIME_BUCKET);
    +
    +        LOG.info("List fields {}", keyNames);
    +        List<String> 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<String> keyNames, List<String>
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<String> 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<String> names)
    +  {
    +    for (int index = 0;; index++) {
    +      sb.append(names.get(index));
    +
    +      if (index == names.size() - 1) {
    --- End diff --
    
    why not use condition in for loop itself, that would be easy to read.


---
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.
---

Mime
View raw message