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

  1. Rewrite LOCATION as 'mongodb://host:port/database/collection', including both database and collection.
  2. For SRV URIs, resolve the actual host list and use a plain mongodb:// host:port URI the regex accepts.
  3. 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

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


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/48058aaed6f1eaec. Report an issue: GitHub.