{"record":{"id":"7f818f642c3ab283","repo":"zhisheng17/flink-learning","slug":"invalid-elasticsearch-hosts-format","errorCode":null,"errorMessage":"invalid elasticsearch hosts format","messagePattern":"invalid elasticsearch hosts format","errorType":"exception","errorClass":"MalformedURLException","httpStatus":null,"severity":"error","filePath":"flink-learning-connectors/flink-learning-connectors-es/flink-learning-connectors-es6/src/main/java/com/zhisheng/connectors/es6/utils/ESSinkUtil.java","lineNumber":65,"sourceCode":"     * 解析配置文件的 es hosts\n     *\n     * @param hosts\n     * @return\n     * @throws MalformedURLException\n     */\n    public static List<HttpHost> getEsAddresses(String hosts) throws MalformedURLException {\n        String[] hostList = hosts.split(\",\");\n        List<HttpHost> addresses = new ArrayList<>();\n        for (String host : hostList) {\n            if (host.startsWith(\"http\")) {\n                URL url = new URL(host);\n                addresses.add(new HttpHost(url.getHost(), url.getPort()));\n            } else {\n                String[] parts = host.split(\":\", 2);\n                if (parts.length > 1) {\n                    addresses.add(new HttpHost(parts[0], Integer.parseInt(parts[1])));\n                } else {\n                    throw new MalformedURLException(\"invalid elasticsearch hosts format\");\n                }\n            }\n        }\n        return addresses;\n    }\n}\n","sourceCodeStart":47,"sourceCodeEnd":72,"githubUrl":"https://github.com/zhisheng17/flink-learning/blob/d731cee7618021be56d132cc925102ffff8d75e6/flink-learning-connectors/flink-learning-connectors-es/flink-learning-connectors-es6/src/main/java/com/zhisheng/connectors/es6/utils/ESSinkUtil.java#L47-L72","documentation":"ESSinkUtil.getEsAddresses parses a hosts string like 'host1:9200,host2:9200' into HttpHost instances. Each comma-separated token must be either a full URL or contain 'host:port'. If a token has no port and is not a parseable URL, the method throws MalformedURLException('invalid elasticsearch hosts format').","triggerScenarios":"Calling getEsAddresses with a hosts string containing a bare token without a port, e.g. 'localhost' or 'es1,es2:9200' — any comma-separated element lacking a ':' and not parseable as a URL (no scheme like http://localhost).","commonSituations":"Config typos where the port was forgotten, copying hostnames from cluster discovery output without ports, or using hostname-only strings in flink conf/properties for the ES6 sink.","solutions":["Add the port to every host in the hosts string, e.g. 'localhost:9200'","Prefix hosts with a scheme so they parse as URLs, e.g. 'http://localhost'","Split on commas and validate each element contains ':' or a URL scheme before passing the string to getEsAddresses"],"exampleFix":"// before\ngetEsAddresses(env, \"localhost\");\n// after\ngetEsAddresses(env, \"localhost:9200\");","handlingStrategy":"validation","validationCode":"static boolean isValidEsHosts(String hosts) {\n    if (hosts == null || hosts.isEmpty()) return false;\n    for (String h : hosts.split(\",\")) {\n        String s = h.trim();\n        if (!(s.contains(\":\") || s.startsWith(\"http://\") || s.startsWith(\"https://\"))) return false;\n    }\n    return true;\n}","typeGuard":"boolean isHostWithPort(String host) {\n    return host != null && (host.contains(\":\") || host.matches(\"^https?://.+\"));\n}","tryCatchPattern":"try {\n    List<HttpHost> addresses = ESSinkUtil.getEsAddresses(hosts);\n} catch (MalformedURLException e) {\n    throw new IllegalArgumentException(\"Bad ES hosts config '\" + hosts + \"': every host needs a port, e.g. host:9200\", e);\n}","preventionTips":["Always include the port for every ES host in config","Prefer full URLs (http://host:9200) over bare host:port","Validate the hosts string at application startup, before job submission"],"tags":["config","elasticsearch","url-parsing","flink"],"backgroundTag":"invalid-url-format","analyzedSha":"d731cee7618021be56d132cc925102ffff8d75e6","analyzedAt":"2026-09-06T05:35:08.496Z","contentChangedAt":"2026-09-06T05:35:08.496Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}