Пример #1
0
  public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {

    meta = (GaInputStepMeta) smi;
    data = (GaInputStepData) sdi;

    if (first) {

      first = false;

      data.outputRowMeta = new RowMeta();
      meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);

      // stores the indices where to look for the key fields in the input rows
      data.conversionMeta = new ValueMetaInterface[meta.getFieldsCount()];

      for (int i = 0; i < meta.getFieldsCount(); i++) {

        // get output and from-string conversion format for each field
        ValueMetaInterface returnMeta = data.outputRowMeta.getValueMeta(i);

        ValueMetaInterface conversionMeta;

        conversionMeta =
            ValueMetaFactory.cloneValueMeta(returnMeta, ValueMetaInterface.TYPE_STRING);
        conversionMeta.setConversionMask(meta.getConversionMask()[i]);
        conversionMeta.setDecimalSymbol("."); // google analytics is en-US
        conversionMeta.setGroupingSymbol(null); // google analytics uses no grouping symbol

        data.conversionMeta[i] = conversionMeta;
      }
    }

    // generate output row, make it correct size
    Object[] outputRow = RowDataUtil.allocateRowData(data.outputRowMeta.size());

    List<String> entry = getNextDataEntry();

    if (entry != null
        && (meta.getRowLimit() <= 0
            || getLinesWritten() < meta.getRowLimit())) { // another record to
      // fill the output fields with look up data
      for (int i = 0, j = 0; i < meta.getFieldsCount(); i++) {
        String fieldName = environmentSubstitute(meta.getFeedField()[i]);
        Object dataObject;
        String type = environmentSubstitute(meta.getFeedFieldType()[i]);

        // We handle fields differently depending on whether its a Dimension/Metric, Data Source
        // Property, or
        // Data Source Field. Also the API doesn't exactly match the concepts anymore (see
        // individual comments below),
        // so there is quite a bit of special processing.
        if (GaInputStepMeta.FIELD_TYPE_DATA_SOURCE_PROPERTY.equals(type)) {
          // Account name has to be handled differently, it's in the Accounts API not Profiles API
          if (GaInputStepMeta.PROPERTY_DATA_SOURCE_ACCOUNT_NAME.equals(fieldName)) {
            // We expect a single account name, and already fetched it during init
            dataObject = accountName;
          } else {
            dataObject = data.feed.getProfileInfo().get(removeClassifier(fieldName));
          }
        } else if (GaInputStepMeta.FIELD_TYPE_DATA_SOURCE_FIELD.equals(type)) {
          // Get tableId or tableName
          if (GaInputStepMeta.FIELD_DATA_SOURCE_TABLE_ID.equals(fieldName)) {
            dataObject = data.feed.getProfileInfo().get(removeClassifier(fieldName));
          } else {
            // We only have two Data Source Fields and they're hard-coded, so we handle tableName in
            // this else-clause
            // since tableId was done in the if-clause. We have to handle the two differently
            // because tableName is
            // actually the profile name in this version (v3) of the Google Analytics API.
            dataObject = data.feed.getProfileInfo().getProfileName();
          }
        } else if (GaInputStepMeta.DEPRECATED_FIELD_TYPE_CONFIDENCE_INTERVAL.equals(type)) {
          dataObject = null;
          if (log.isRowLevel()) {
            logRowlevel(
                BaseMessages.getString(
                    PKG,
                    "GoogleAnalytics.Warn.FieldTypeNotSupported",
                    GaInputStepMeta.DEPRECATED_FIELD_TYPE_CONFIDENCE_INTERVAL));
          }
        } else {
          // Assume it's a Dimension or Metric, we've covered the rest of the cases above.
          dataObject = entry.get(j++);
        }
        outputRow[i] =
            data.outputRowMeta.getValueMeta(i).convertData(data.conversionMeta[i], dataObject);
      }

      // copy row to possible alternate rowset(s)
      putRow(data.outputRowMeta, outputRow);

      // Some basic logging
      if (checkFeedback(getLinesWritten())) {
        if (log.isBasic()) {
          logBasic("Linenr " + getLinesWritten());
        }
      }
      return true;

    } else {
      setOutputDone();
      return false;
    }
  }
Пример #2
0
  public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (GaInputStepMeta) smi;
    data = (GaInputStepData) sdi;

    if (!super.init(smi, sdi)) {
      return false;
    }

    // Look for deprecated field types and log error(s) for them
    String[] types = environmentSubstitute(meta.getFeedFieldType());
    if (types != null) {
      for (String type : types) {
        if (GaInputStepMeta.DEPRECATED_FIELD_TYPE_CONFIDENCE_INTERVAL.equals(type)) {
          logError(
              BaseMessages.getString(
                  PKG,
                  "GoogleAnalytics.Warn.FieldTypeNotSupported",
                  GaInputStepMeta.DEPRECATED_FIELD_TYPE_CONFIDENCE_INTERVAL));
        }
      }
    }

    String appName = environmentSubstitute(meta.getGaAppName());
    String serviceAccount = environmentSubstitute(meta.getOAuthServiceAccount());
    String OAuthKeyFile = environmentSubstitute(meta.getOAuthKeyFile());

    if (log.isDetailed()) {
      logDetailed(
          BaseMessages.getString(PKG, "GoogleAnalyticsDialog.AppName.Label") + ": " + appName);
      logDetailed(
          BaseMessages.getString(PKG, "GoogleAnalyticsDialog.OauthAccount.Label")
              + ": "
              + serviceAccount);
      logDetailed(
          BaseMessages.getString(PKG, "GoogleAnalyticsDialog.KeyFile.Label") + ": " + OAuthKeyFile);
    }

    try {
      // Create an Analytics object, and fetch what we can for later (account name, e.g.)
      analytics =
          GoogleAnalyticsApiFacade.createFor(appName, serviceAccount, OAuthKeyFile).getAnalytics();
      // There is necessarily an account name associated with this, so any NPEs or other exceptions
      // mean bail out
      accountName =
          analytics.management().accounts().list().execute().getItems().iterator().next().getName();
    } catch (TokenResponseException tre) {
      Exception exceptionToLog = tre;
      if (tre.getDetails() != null && tre.getDetails().getError() != null) {
        exceptionToLog =
            new IOException(
                BaseMessages.getString(
                    PKG, "GoogleAnalytics.Error.OAuth2.Auth", tre.getDetails().getError()),
                tre);
      }
      logError(BaseMessages.getString(PKG, "GoogleAnalytics.Error.AccessingGaApi"), exceptionToLog);
      return false;
    } catch (Exception e) {
      logError(BaseMessages.getString(PKG, "GoogleAnalytics.Error.AccessingGaApi"), e);
      return false;
    }
    return true;
  }