{"record":{"id":"d1a6769e1e921e2e","repo":"zhisheng17/flink-learning","slug":"invalid-elasticsearch-hosts-format-d1a676","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-es7/src/main/java/com/zhisheng/connectors/es7/util/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-es7/src/main/java/com/zhisheng/connectors/es7/util/ESSinkUtil.java#L47-L72","documentation":"The ES7 ESSinkUtil.getEsAddresses mirrors the ES6 version: it splits the hosts string on commas and requires each token to be a URL or 'host:port'. A token without a port and not parseable as a URL throws MalformedURLException('invalid elasticsearch hosts format').","triggerScenarios":"Passing a hosts string like 'es-node' or 'a,b:9200' to getEsAddresses for the Elasticsearch 7 sink — any comma-separated element without a ':' separator that also fails new URL(host).","commonSituations":"Forgetting the 9200 port in application config, hostname-only entries from Kubernetes service names, or migrating config from other ES clients that accept bare hostnames.","solutions":["Append the port to each host, e.g. 'es-node:9200'","Use full URLs with scheme, e.g. 'http://es-node:9200'","Pre-validate the hosts string (each comma-separated part must contain ':' or a scheme) before calling getEsAddresses"],"exampleFix":"// before\n\"elasticsearch.hosts\": \"es-master\"\n// after\n\"elasticsearch.hosts\": \"es-master:9200\"","handlingStrategy":"validation","validationCode":"static boolean isValidEs7Hosts(String hosts) {\n    if (hosts == null || hosts.isBlank()) return false;\n    return Arrays.stream(hosts.split(\",\"))\n        .allMatch(h -> h.contains(\":\") || h.startsWith(\"http\"));\n}","typeGuard":"boolean hasPortOrScheme(String host) {\n    return host != null && host.chars().filter(c -> c == ':').count() >= 1;\n}","tryCatchPattern":"try {\n    List<HttpHost> addresses = ESSinkUtil.getEsAddresses(hosts);\n} catch (MalformedURLException e) {\n    LOG.error(\"Invalid elasticsearch.hosts '{}': use host:port or http://host:port\", hosts, e);\n    throw e;\n}","preventionTips":["Keep ports in the hosts config; ES default is 9200","Validate the config value when loading properties, before building the sink","Document the expected format (comma-separated host:port) in your config template"],"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-14T05:17:10.506Z"}