Package com.mongodb.spark.sql.connector
Class MongoCatalog
- java.lang.Object
-
- com.mongodb.spark.sql.connector.MongoCatalog
-
- All Implemented Interfaces:
CatalogPlugin
,SupportsNamespaces
,TableCatalog
public class MongoCatalog extends java.lang.Object implements TableCatalog, SupportsNamespaces
Spark Catalog methods for working with namespaces (databases) and tables (collections).
-
-
Field Summary
-
Fields inherited from interface org.apache.spark.sql.connector.catalog.SupportsNamespaces
PROP_COMMENT, PROP_LOCATION, PROP_OWNER
-
Fields inherited from interface org.apache.spark.sql.connector.catalog.TableCatalog
OPTION_PREFIX, PROP_COMMENT, PROP_EXTERNAL, PROP_LOCATION, PROP_OWNER, PROP_PROVIDER
-
-
Constructor Summary
Constructors Constructor Description MongoCatalog()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
alterNamespace(java.lang.String[] namespace, NamespaceChange... changes)
Altering databases is currently not supported, soalterNamespace
will always throw an exception.Table
alterTable(Identifier identifier, TableChange... changes)
Altering collections is currently not supported, soalterTable
will always throw an exception.void
createNamespace(java.lang.String[] namespace, java.util.Map<java.lang.String,java.lang.String> metadata)
Create a database.Table
createTable(Identifier identifier, StructType schema, Transform[] partitions, java.util.Map<java.lang.String,java.lang.String> properties)
Create a collection.boolean
dropNamespace(java.lang.String[] namespace)
Drop a database.boolean
dropTable(Identifier identifier)
Drop a collection.void
initialize(java.lang.String name, CaseInsensitiveStringMap options)
Initializes the MongoCatalog.java.lang.String[][]
listNamespaces()
List namespaces (databases).java.lang.String[][]
listNamespaces(java.lang.String[] namespace)
List namespaces (databases).Identifier[]
listTables(java.lang.String[] namespace)
List the collections in a namespace (database).java.util.Map<java.lang.String,java.lang.String>
loadNamespaceMetadata(java.lang.String[] namespace)
Currently only returns an empty map if the namespace (database) exists.Table
loadTable(Identifier identifier)
Load collection.java.lang.String
name()
boolean
namespaceExists(java.lang.String[] namespace)
Test whether a namespace (database) exists.void
renameTable(Identifier oldIdentifier, Identifier newIdentifier)
Renames a collection.boolean
tableExists(Identifier identifier)
Test whether a collection exists.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.spark.sql.connector.catalog.CatalogPlugin
defaultNamespace
-
Methods inherited from interface org.apache.spark.sql.connector.catalog.TableCatalog
invalidateTable, purgeTable
-
-
-
-
Method Detail
-
initialize
public void initialize(java.lang.String name, CaseInsensitiveStringMap options)
Initializes the MongoCatalog.- Specified by:
initialize
in interfaceCatalogPlugin
- Parameters:
name
- the name used to identify and load this catalogoptions
- a case-insensitive string map of configuration
-
name
public java.lang.String name()
- Specified by:
name
in interfaceCatalogPlugin
- Returns:
- the catalog name
-
listNamespaces
public java.lang.String[][] listNamespaces()
List namespaces (databases).- Specified by:
listNamespaces
in interfaceSupportsNamespaces
- Returns:
- an array of namespace (database) names
-
listNamespaces
public java.lang.String[][] listNamespaces(java.lang.String[] namespace) throws org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException
List namespaces (databases).As MongoDB only supports top level databases, this will return all databases if the database (namespace array) is empty or any empty array if the database exists.
- Specified by:
listNamespaces
in interfaceSupportsNamespaces
- Parameters:
namespace
- the optional database array- Returns:
- an empty array if the database exists
- Throws:
org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException
- If the namespace (database) does not exist
-
loadNamespaceMetadata
public java.util.Map<java.lang.String,java.lang.String> loadNamespaceMetadata(java.lang.String[] namespace) throws org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException
Currently only returns an empty map if the namespace (database) exists.- Specified by:
loadNamespaceMetadata
in interfaceSupportsNamespaces
- Parameters:
namespace
- (database) a multi-part namespace- Returns:
- a string map of properties for the given namespace
- Throws:
org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException
- If the namespace (database) does not exist
-
namespaceExists
public boolean namespaceExists(java.lang.String[] namespace)
Test whether a namespace (database) exists.- Specified by:
namespaceExists
in interfaceSupportsNamespaces
- Parameters:
namespace
- (database) a multi-part namespace- Returns:
- true if the namespace (database) exists, false otherwise
-
createNamespace
public void createNamespace(java.lang.String[] namespace, java.util.Map<java.lang.String,java.lang.String> metadata) throws org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException
Create a database.As databases can be created automatically when creating a collection in MongoDB this method only checks to ensure that the database doesn't already exist.
- Specified by:
createNamespace
in interfaceSupportsNamespaces
- Parameters:
namespace
- (database) a multi-part namespacemetadata
- a string map of properties for the given namespace- Throws:
org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException
- If the namespace (database) already existsjava.lang.UnsupportedOperationException
- If create is not a supported operation
-
alterNamespace
public void alterNamespace(java.lang.String[] namespace, NamespaceChange... changes)
Altering databases is currently not supported, soalterNamespace
will always throw an exception.- Specified by:
alterNamespace
in interfaceSupportsNamespaces
- Parameters:
namespace
- (database) a multi-part namespacechanges
- a collection of changes to apply to the namespace- Throws:
java.lang.UnsupportedOperationException
- If namespace (database) properties are not supported
-
dropNamespace
public boolean dropNamespace(java.lang.String[] namespace)
Drop a database.- Specified by:
dropNamespace
in interfaceSupportsNamespaces
- Parameters:
namespace
- (database) a multi-part namespace- Returns:
- true if the namespace (database) was dropped
-
listTables
public Identifier[] listTables(java.lang.String[] namespace)
List the collections in a namespace (database).Note: Will only return collections and not views.
- Specified by:
listTables
in interfaceTableCatalog
- Parameters:
namespace
- (database) a multi-part namespace- Returns:
- an array of Identifiers for tables (collections)
-
tableExists
public boolean tableExists(Identifier identifier)
Test whether a collection exists.- Specified by:
tableExists
in interfaceTableCatalog
- Parameters:
identifier
- a collection identifier- Returns:
- true if the collection exists, false otherwise
-
loadTable
public Table loadTable(Identifier identifier) throws org.apache.spark.sql.catalyst.analysis.NoSuchTableException
Load collection.- Specified by:
loadTable
in interfaceTableCatalog
- Parameters:
identifier
- a collection identifier- Returns:
- the table's metadata
- Throws:
org.apache.spark.sql.catalyst.analysis.NoSuchTableException
- If the collection doesn't exist or is a view
-
createTable
public Table createTable(Identifier identifier, StructType schema, Transform[] partitions, java.util.Map<java.lang.String,java.lang.String> properties) throws org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException
Create a collection.- Specified by:
createTable
in interfaceTableCatalog
- Parameters:
identifier
- a collection identifierschema
- the schema of the new table, as a struct typepartitions
- transforms to use for partitioning data in the tableproperties
- a string map of collection properties- Returns:
- metadata for the new table
- Throws:
org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException
- If a collection or view already exists for the identifierjava.lang.UnsupportedOperationException
- If a requested partition transform is not supported
-
alterTable
public Table alterTable(Identifier identifier, TableChange... changes) throws org.apache.spark.sql.catalyst.analysis.NoSuchTableException
Altering collections is currently not supported, soalterTable
will always throw an exception.- Specified by:
alterTable
in interfaceTableCatalog
- Parameters:
identifier
- a collection identifierchanges
- changes to apply to the table- Returns:
- will throw an exception as altering collections is not supported.
- Throws:
org.apache.spark.sql.catalyst.analysis.NoSuchTableException
- If the collection doesn't exist or is a viewjava.lang.IllegalArgumentException
- If any change is rejected by the implementation.
-
dropTable
public boolean dropTable(Identifier identifier)
Drop a collection.- Specified by:
dropTable
in interfaceTableCatalog
- Parameters:
identifier
- a collection identifier- Returns:
- true if a collection was deleted, false if no collection exists for the identifier
-
renameTable
public void renameTable(Identifier oldIdentifier, Identifier newIdentifier) throws org.apache.spark.sql.catalyst.analysis.NoSuchTableException, org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException
Renames a collection.- Specified by:
renameTable
in interfaceTableCatalog
- Parameters:
oldIdentifier
- the collection identifier of the existing collection to renamenewIdentifier
- the new collection identifier of the table- Throws:
org.apache.spark.sql.catalyst.analysis.NoSuchTableException
- If the collection to rename doesn't exist or is a vieworg.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException
- If the new collection name already exists or is a view
-
-