apache/beam · error · InvalidTableException
MongoDb location must be in the following format: 'mongodb:/
Error message
MongoDb location must be in the following format: 'mongodb://(username:password@)?localhost:27017/database/collection' but was: ${location} What it means
MongoDbTable's constructor validates the table 'location' string against a regex requiring the form mongodb://[user:pass@]host:port/database/collection. If the location does not match, InvalidTableException is thrown during table resolution, before any connection attempt.
Source
Thrown at sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/mongodb/MongoDbTable.java:100
// Should match: mongodb://username:password@localhost:27017/database/collection
@VisibleForTesting
final Pattern locationPattern =
Pattern.compile(
"(?<credsHostPort>mongodb://(?<usernamePassword>.*(?<password>:.*)?@)?.+:\\d+)/(?<database>.+)/(?<collection>.+)");
@VisibleForTesting final String dbCollection;
@VisibleForTesting final String dbName;
@VisibleForTesting final String dbUri;
MongoDbTable(Table table) {
super(table.getSchema());
String location = table.getLocation();
Matcher matcher = locationPattern.matcher(location);
if (!matcher.matches()) {
throw new InvalidTableException(
"MongoDb location must be in the following format:"
+ " 'mongodb://(username:password@)?localhost:27017/database/collection'"
+ " but was: "
+ location);
}
this.dbUri = matcher.group("credsHostPort"); // "mongodb://localhost:27017"
this.dbName = matcher.group("database");
this.dbCollection = matcher.group("collection");
}
@Override
public PCollection<Row> buildIOReader(PBegin begin) {
// Read MongoDb Documents
PCollection<Document> readDocuments =
MongoDbIO.read()
.withUri(dbUri)
.withDatabase(dbName)
.withCollection(dbCollection)View on GitHub (pinned to 12126d8942)
Solutions
- Rewrite LOCATION as 'mongodb://host:port/database/collection', including both database and collection.
- For SRV URIs, resolve the actual host list and use a plain mongodb:// host:port URI the regex accepts.
- Escape or remove credentials issues by either including user:password@ fully or omitting credentials entirely (not partially).
Example fix
// before CREATE EXTERNAL TABLE t (...) LOCATION 'mongodb+srv://cluster.example.net/db.coll' // after CREATE EXTERNAL TABLE t (...) LOCATION 'mongodb://user:pass@host1:27017/db.coll'
Defensive patterns
Strategy: validation
Validate before calling
Pattern p = Pattern.compile("^mongodb://([a-zA-Z0-9_.:-]+@)?[a-zA-Z0-9_.:-]+:[0-9]+/[a-zA-Z0-9_]+/[a-zA-Z0-9_]+$");
if (!p.matcher(location).matches()) throw new IllegalArgumentException("Bad MongoDb LOCATION: " + location); Try / catch
try { new MongoDbTable(table); } catch (InvalidTableException e) { /* surface a formatted-location hint to the user */ } Prevention
- Always include scheme, host:port, database, and collection
- Avoid mongodb+srv:// in Beam SQL locations
- Template the LOCATION string from config instead of hand-writing it
When it happens
Trigger: Creating a MongoDb external table whose LOCATION is missing 'mongodb://', lacks a database/collection path segment, uses an unsupported scheme (mongodb+srv://), or contains characters the regex rejects.
Common situations: Pasting a MongoDB Atlas SRV connection string (mongodb+srv://) which the legacy regex does not accept; omitting the database or collection from the URI; quoting/whitespace issues in the DDL.
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
- Cannot create a filter for an unsupported node: ${node}
- Encountered an unexpected node kind: ${node.getKind()}
- Was expecting a RexCall or a boolean RexInputRef, but receiv
- Predicate node '${node.getClass().getSimpleName()}' should b
- Encountered an unexpected node type: ${node.getClass().getSi
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/48058aaed6f1eaec.
Report an issue: GitHub.