apache/druid · error · IllegalArgumentException

Key[ ] should be a long, was[ ]

Error message

Key[%s] should be a long, was[%s]

What it means

MapUtils.getLong found the key but its string representation could not be parsed by Long.parseLong. It throws IAE wrapping the NumberFormatException, naming the key and the actual value so the malformed entry is easy to locate.

Solutions

  1. Fix the value to a plain integer literal (e.g. "10737418240" instead of "10GB")
  2. Convert units yourself before storing: 10L * 1024 * 1024 * 1024
  3. Parse with a helper that understands unit suffixes if such values are expected
  4. Validate the config at startup with clear errors for each bad entry

Example fix

// before
props.put("druid.segment.cache.sizeBytes", "5GB");
long size = MapUtils.getLong(props, "druid.segment.cache.sizeBytes", 0L);
// after
props.put("druid.segment.cache.sizeBytes", Long.toString(5L * 1024 * 1024 * 1024));
long size = MapUtils.getLong(props, "druid.segment.cache.sizeBytes", 0L);
Defensive patterns

Strategy: validation

Validate before calling

Object v = props.get(key);
if (v != null && !v.toString().trim().matches("-?\\d+")) {
  throw new IllegalArgumentException("Key " + key + " must be a whole number (long), got: " + v);
}

Type guard

static boolean isLongString(Object v) {
  return v != null && v.toString().trim().matches("-?\\d+");
}

Try / catch

try {
  return MapUtils.getLong(props, key, defaultVal);
} catch (IllegalArgumentException e) {
  log.error("Invalid long value for %s", key, e);
  return defaultVal;
}

Prevention

When it happens

Trigger: Calling MapUtils.getLong(map, key, defaultValue) where the stored value's toString() is not a valid long (e.g. "10.5", "8GB", "1e9", empty string, boolean).

Common situations: Byte-size strings like "10GB" in a field expecting raw bytes, decimal numbers, scientific notation, or stray whitespace/characters in properties files.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/fda8085b391bb247. Report an issue: GitHub.

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/java/util/common/MapUtils.java:84

  }

  public static long getLong(Map<String, Object> in, String key, Long defaultValue)
  {
    Object retVal = in.get(key);

    if (retVal == null) {
      if (defaultValue == null) {
        throw new IAE("Key[%s] is required in map[%s]", key, in);
      }

      return defaultValue;
    }

    try {
      return Long.parseLong(retVal.toString());
    }
    catch (NumberFormatException e) {
      throw new IAE(e, "Key[%s] should be a long, was[%s]", key, retVal);
    }
  }

}

View on GitHub (pinned to 9b90983fd2)