Java Code Examples for org.apache.kafka.common.config.ConfigDef#ConfigKey

The following examples show how to use org.apache.kafka.common.config.ConfigDef#ConfigKey . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClickHouseConfigDef.java    From kafka-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * 所有参数非空校验
 */
public static Map<String, ConfigValue> emptyValidate(Map<String, String> connectorConfigs, ConfigDef configDef) {
    Map<String, ConfigValue> configValues = new HashMap<>(16);
    for (ConfigDef.ConfigKey configKey : configDef.configKeys().values()) {
        ConfigValue configValue = new ConfigValue(configKey.name);
        String value = connectorConfigs.get(configKey.name);
        if (configKey.importance != ConfigDef.Importance.LOW) {
            if (StringUtils.isEmpty(value)) {
                configValue.addErrorMessage(String.format("%s不能为空", configKey.name));
            }
        }
        configValue.value(value);
        configValues.put(configKey.name, configValue);
    }
    return configValues;
}
 
Example 2
Source File: ConfigKeyComparator.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(ConfigDef.ConfigKey k1, ConfigDef.ConfigKey k2) {
  // first take anything with no default value (therefore required)
  if (!k1.hasDefault() && k2.hasDefault())
    return -1;
  else if (!k2.hasDefault() && k1.hasDefault())
    return 1;

  // then sort by importance
  int cmp = k1.importance.compareTo(k2.importance);
  if (cmp == 0)
    // then sort in alphabetical order
    return k1.name.compareTo(k2.name);
  else
    return cmp;
}
 
Example 3
Source File: MarkdownFormatter.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
private static List<ConfigDef.ConfigKey> getSortedList(Map<String, ConfigDef.ConfigKey> configKeys) {
  List<ConfigDef.ConfigKey> configs = new ArrayList<ConfigDef.ConfigKey>(configKeys.values());
  Collections.sort(configs, new Comparator<ConfigDef.ConfigKey>() {
    public int compare(ConfigDef.ConfigKey k1, ConfigDef.ConfigKey k2) {
      // first take anything with no default value (therefore required)
      if (!k1.hasDefault() && k2.hasDefault())
        return -1;
      else if (!k2.hasDefault() && k1.hasDefault())
        return 1;

      // then sort by importance
      int cmp = k1.importance.compareTo(k2.importance);
      if (cmp == 0)
        // then sort in alphabetical order
        return k1.name.compareTo(k2.name);
      else
        return cmp;
    }
  });
  return configs;
}
 
Example 4
Source File: Cli.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
private ConfigDef.Type parseProducerProperty(String parsedProperty) {
  ConfigDef.Type type;
  ConfigDef.ConfigKey configKey = PRODUCER_CONFIG_DEF.configKeys().get(parsedProperty);
  if (configKey == null) {
    throw new IllegalArgumentException(String.format(
        "Invalid producer property: '%s'",
        parsedProperty
    ));
  }
  type = configKey.type;
  return type;
}
 
Example 5
Source File: Cli.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
private ConfigDef.Type parseConsumerProperty(String parsedProperty) {
  ConfigDef.Type type;
  ConfigDef.ConfigKey configKey = CONSUMER_CONFIG_DEF.configKeys().get(parsedProperty);
  if (configKey == null) {
    throw new IllegalArgumentException(String.format(
        "Invalid consumer property: '%s'",
        parsedProperty
    ));
  }
  type = configKey.type;
  return type;
}
 
Example 6
Source File: AwsLambdaSinkConnectorConfig.java    From kafka-connect-aws-lambda with Apache License 2.0 5 votes vote down vote up
private static ConfigDef getConfig() {
  Map<String, ConfigDef.ConfigKey> everything = new HashMap<>(conf().configKeys());
  ConfigDef visible = new ConfigDef();
  for (ConfigDef.ConfigKey key : everything.values()) {
    visible.define(key);
  }
  return visible;
}
 
Example 7
Source File: RestSourceConnectorConfig.java    From kafka-connect-rest with Apache License 2.0 5 votes vote down vote up
private static ConfigDef getConfig() {
  Map<String, ConfigDef.ConfigKey> everything = new HashMap<>(conf().configKeys());
  ConfigDef visible = new ConfigDef();
  for (ConfigDef.ConfigKey key : everything.values()) {
    visible.define(key);
  }
  return visible;
}
 
Example 8
Source File: MarkdownFormatter.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
static String getDefaultValue(ConfigDef.ConfigKey def) {
  if (def.hasDefault()) {
    if (def.defaultValue == null)
      return "null";
    else if (def.type == ConfigDef.Type.STRING && def.defaultValue.toString().isEmpty())
      return "\"\"";
    else
      return def.defaultValue.toString();
  } else
    return "";
}
 
Example 9
Source File: ConfigKeyBuilderTest.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void minimal() {
  final ConfigDef.ConfigKey actual = ConfigKeyBuilder.of("testing", ConfigDef.Type.STRING)
      .build();
  assertEquals("testing", actual.name, "name should match.");
  assertEquals(ConfigDef.Type.STRING, actual.type, "type should match.");

  new ConfigDef().define(actual).toEnrichedRst();
}
 
Example 10
Source File: ConfigKeyBuilderTest.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void build() {
  final ConfigDef.ConfigKey actual = ConfigKeyBuilder.of("testing", ConfigDef.Type.STRING)
      .documentation("Testing")
      .importance(ConfigDef.Importance.HIGH)
      .build();
  assertEquals("testing", actual.name, "name should match.");
  assertEquals(ConfigDef.Type.STRING, actual.type, "type should match.");
  assertEquals("Testing", actual.documentation, "documentation should match.");
  assertEquals(ConfigDef.Importance.HIGH, actual.importance, "importance should match.");

  new ConfigDef().define(actual).toEnrichedRst();

}
 
Example 11
Source File: KafkaConfigModelGenerator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private static List<String> validList(ConfigDef.ConfigKey key) {
    try {
        Field f = ConfigDef.ValidList.class.getDeclaredField("validString");
        f.setAccessible(true);
        ConfigDef.ValidString itemValidator = (ConfigDef.ValidString) f.get(key.validator);
        List<String> validItems = enumer(itemValidator);
        return validItems;
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: KafkaConfigModelGenerator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private static ConfigModel range(ConfigDef.ConfigKey key, ConfigModel descriptor) {
    String str = key.validator.toString();
    try {
        Pattern rangePattern = Pattern.compile("\\[([0-9.e+-]+|\\.\\.\\.),?([0-9.e+-]+|\\.\\.\\.)?\\]", Pattern.CASE_INSENSITIVE);
        Matcher matcher = rangePattern.matcher(str);
        if (matcher.matches()) {
            if (matcher.groupCount() == 2) {
                String maxStr = matcher.group(2);
                if (maxStr.contains("e") || maxStr.contains(".") && !maxStr.contains("..")) {
                    descriptor.setMaximum(Double.parseDouble(maxStr));
                } else if (!"...".equals(maxStr)) {
                    descriptor.setMaximum(Long.parseLong(maxStr));
                }

                String minStr = matcher.group(1);
                if (minStr.contains("e") || minStr.contains(".") && !minStr.contains("..")) {
                    descriptor.setMinimum(Double.parseDouble(minStr));
                } else if (!"...".equals(minStr)) {
                    descriptor.setMinimum(Long.parseLong(minStr));
                }

            } else if (matcher.groupCount() != 1) {
                throw new IllegalStateException(str);
            }
        } else {
            throw new IllegalStateException(str);
        }
        return descriptor;
    } catch (RuntimeException e) {
        throw new RuntimeException("For string: " + str, e);
    }
}
 
Example 13
Source File: ConfigKeyBuilder.java    From connect-utils with Apache License 2.0 4 votes vote down vote up
public ConfigDef.ConfigKey build() {
  Preconditions.checkState(!Strings.isNullOrEmpty(this.name), "name must be specified.");
  return new ConfigDef.ConfigKey(this.name, this.type, this.defaultValue, this.validator, this.importance, this.documentation, this.group, this.orderInGroup, this.width, this.displayName, this.dependents, this.recommender, this.internalConfig);
}
 
Example 14
Source File: MarkdownFormatter.java    From connect-utils with Apache License 2.0 4 votes vote down vote up
public static String toMarkdown(ConfigDef configDef) {

    List<ConfigDef.ConfigKey> sortedConfigs = getSortedList(configDef.configKeys());
    String[] headers = new String[]{
        "Name", "Description", "Type", "Default", "Valid Values", "Importance"
    };

    List<List<String>> rows = new ArrayList<>();
    rows.add(Arrays.asList(headers));

    for (ConfigDef.ConfigKey def : sortedConfigs) {
      List<String> row = new ArrayList<>(headers.length);
      for (int i = 0; i < headers.length; i++) {
        String value;
        switch (i) {
          case 0: //Name
            value = def.name;
            break;
          case 1:
            value = null == def.documentation ? "" : def.documentation;
            break;
          case 2:
            value = def.type.toString().toLowerCase();
            break;
          case 3:
            String defaultValue = getDefaultValue(def);
            value = null == defaultValue ? "" : defaultValue;
            break;
          case 4:
            String validValues = def.validator != null ? def.validator.toString() : "";
            value = null == validValues ? "" : validValues;
            break;
          case 5:
            value = def.importance.toString().toLowerCase();
            break;
          default:
            throw new IllegalArgumentException("There are more headers than columns.");
        }
        row.add(value);
      }
      rows.add(row);
    }

    return markdownTable(rows);
  }
 
Example 15
Source File: KafkaConfigModelGenerator.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
private static Map<String, ConfigModel> configs() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    ConfigDef def = brokerConfigs();
    Map<String, String> dynamicUpdates = brokerDynamicUpdates();
    Method getConfigValueMethod = def.getClass().getDeclaredMethod("getConfigValue", ConfigDef.ConfigKey.class, String.class);
    getConfigValueMethod.setAccessible(true);

    Method sortedConfigs = ConfigDef.class.getDeclaredMethod("sortedConfigs");
    sortedConfigs.setAccessible(true);

    List<ConfigDef.ConfigKey> keys = (List) sortedConfigs.invoke(def);
    Map<String, ConfigModel> result = new TreeMap<>();
    for (ConfigDef.ConfigKey key : keys) {
        String configName = String.valueOf(getConfigValueMethod.invoke(def, key, "Name"));
        Type type = parseType(String.valueOf(getConfigValueMethod.invoke(def, key, "Type")));
        Scope scope = parseScope(dynamicUpdates.getOrDefault(key.name, "read-only"));
        ConfigModel descriptor = new ConfigModel();
        descriptor.setType(type);
        descriptor.setScope(scope);

        if (key.validator instanceof ConfigDef.Range) {
            descriptor = range(key, descriptor);
        } else if (key.validator instanceof ConfigDef.ValidString) {
            descriptor.setValues(enumer(key.validator));
        } else if (key.validator instanceof ConfigDef.ValidList) {
            descriptor.setItems(validList(key));
        } else if (key.validator instanceof ApiVersionValidator$) {
            Iterator<ApiVersion> iterator = ApiVersion$.MODULE$.allVersions().iterator();
            LinkedHashSet<String> versions = new LinkedHashSet<>();
            while (iterator.hasNext()) {
                ApiVersion next = iterator.next();
                ApiVersion$.MODULE$.apply(next.shortVersion());
                versions.add(Pattern.quote(next.shortVersion()) + "(\\.[0-9]+)*");
                ApiVersion$.MODULE$.apply(next.version());
                versions.add(Pattern.quote(next.version()));
            }
            descriptor.setPattern(String.join("|", versions));
        } else if (key.validator != null) {
            throw new IllegalStateException(key.validator.getClass().toString());
        }
        result.put(configName, descriptor);
    }
    return result;
}