zhisheng17/flink-learning · error · MalformedURLException

invalid elasticsearch hosts format

Error message

invalid elasticsearch hosts format

What it means

ESSinkUtil.getEsAddresses parses a host string into HttpHost addresses. When the host entry contains no ':' delimiter (so no host:port pair can be extracted) it throws MalformedURLException 'invalid elasticsearch hosts format'. The parser expects entries like host:9200 or http://host:9200.

Source

Thrown at flink-learning-monitor/flink-learning-monitor-log/src/main/java/com/zhisheng/log/utils/ESSinkUtil.java:61

     * 解析配置文件的 es hosts
     *
     * @param hosts
     * @return
     * @throws MalformedURLException
     */
    public static List<HttpHost> getEsAddresses(String hosts) throws MalformedURLException {
        String[] hostList = hosts.split(",");
        List<HttpHost> addresses = new ArrayList<>();
        for (String host : hostList) {
            if (host.startsWith("http")) {
                URL url = new URL(host);
                addresses.add(new HttpHost(url.getHost(), url.getPort()));
            } else {
                String[] parts = host.split(":", 2);
                if (parts.length > 1) {
                    addresses.add(new HttpHost(parts[0], Integer.parseInt(parts[1])));
                } else {
                    throw new MalformedURLException("invalid elasticsearch hosts format");
                }
            }
        }
        return addresses;
    }
}

View on GitHub (pinned to d731cee761)

Solutions

  1. Include the port in every host entry, e.g. localhost:9200 or node1:9200,node2:9200.
  2. Remove empty/duplicate separators (trailing commas, spaces) from the hosts list.
  3. If the scheme is included, keep it well-formed (http://host:9200).

Example fix

// before
es.hosts = localhost,es1
// after
es.hosts = localhost:9200,es1:9200
Defensive patterns

Strategy: validation

Validate before calling

for (String host : hostsCsv.split(",")) {
    String h = host.trim();
    if (h.isEmpty() || !h.matches("(https?://)?[^:]+:\\d+")) {
        throw new IllegalArgumentException("ES host must be host:port, got: '" + h + "'");
    }
}

Try / catch

try {
    List<HttpHost> addrs = ESSinkUtil.getEsAddresses(paramTool.get("es.hosts"));
} catch (MalformedURLException e) {
    LOG.error("Bad elasticsearch hosts format: {}", e.getMessage());
}

Prevention

When it happens

Trigger: Passing an elasticsearch hosts config value whose items lack a port — e.g. 'localhost' alone, empty items from a trailing comma, or a malformed URL that URL parsing rejected earlier.

Common situations: Configuring es.hosts=localhost without :9200, whitespace or empty segments in a comma-separated list, IPv6 addresses with colons confusing the split, or a typo dropping the port.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of zhisheng17/flink-learning@d731cee761 (2026-09-06). Data as JSON: /api/errors/f88829c9c0127068. Report an issue: GitHub.