Package weka.core.converters
Class DatabaseSaver
java.lang.Object
weka.core.converters.AbstractSaver
weka.core.converters.DatabaseSaver
- All Implemented Interfaces:
Serializable
,CapabilitiesHandler
,CapabilitiesIgnorer
,BatchConverter
,DatabaseConverter
,IncrementalConverter
,Saver
,EnvironmentHandler
,OptionHandler
,RevisionHandler
public class DatabaseSaver
extends AbstractSaver
implements BatchConverter, IncrementalConverter, DatabaseConverter, OptionHandler, EnvironmentHandler
Writes to a database (tested with MySQL, InstantDB,
HSQLDB).
Valid options are:
-url <JDBC URL> The JDBC URL to connect to. (default: from DatabaseUtils.props file)
-user <name> The user to connect with to the database. (default: none)
-password <password> The password to connect with to the database. (default: none)
-T <table name> The name of the table. (default: the relation name)
-truncate Truncate (i.e. delete any data) in table before inserting
-P Add an ID column as primary key. The name is specified in the DatabaseUtils file ('idColumn'). The DatabaseLoader won't load this column.
-custom-props <file> The custom properties file to use instead of default ones, containing the database parameters. (default: none)
-i <input file name> Input file in arff format that should be saved in database.
- Version:
- $Revision: 12418 $
- Author:
- Stefan Mutter (mutter@cs.waikato.ac.nz)
- See Also:
-
Field Summary
Fields inherited from interface weka.core.converters.Saver
BATCH, INCREMENTAL, NONE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns the tip text for this property.void
cancel()
Cancels the incremental saving process and tries to drop the table if the write mode is CANCEL.void
Opens a connection to the database.The tip text for this property.boolean
Gets whether or not a primary key will be generated automatically.Returns the Capabilities of this saver.Returns the custom properties file in use, if any.String[]
Gets the setting.Returns the database password.boolean
Gets whether or not the relation name is used as name of the table.Returns the revision string.Gets the table's name.boolean
Get whether to truncate (i.e.getUrl()
Gets the database URL.getUser()
Gets the database user.Returns a string describing this Saver.Lists the available options.static void
Main method.Returns the tip text for this property.Returns the tip text fo this property.void
Resets the Saver ready to save a new data set.void
setAutoKeyGeneration
(boolean flag) En/Dis-ables the automatic generation of a primary key.void
setCustomPropsFile
(File value) Sets the custom properties file to use.void
Sets the database url using the DatabaseUtils file.void
setDestination
(String url) Sets the database url.void
setDestination
(String url, String userName, String password) Sets the database url.void
Set the environment variables to use.void
setOptions
(String[] options) Sets the options.void
setPassword
(String password) Sets the database password.void
setRelationForTableName
(boolean flag) En/Dis-ables that the relation name is used for the name of the table (default enabled).void
setTableName
(String tn) Sets the table's name.void
setTruncate
(boolean t) Set whether to truncate (i.e.void
Sets the database URL.void
Sets the database user.Returns the tip text for this property.Returns the tip text for this property.Returns the tip text for this property.Returns the tip text for this property.void
Writes a Batch of instances.void
writeIncremental
(Instance inst) Saves an instances incrementally.Methods inherited from class weka.core.converters.AbstractSaver
doNotCheckCapabilitiesTipText, filePrefix, getDoNotCheckCapabilities, getFileExtension, getInstances, getWriteMode, resetStructure, retrieveDir, setDestination, setDestination, setDir, setDirAndPrefix, setDoNotCheckCapabilities, setFile, setFilePrefix, setInstances, setRetrieval, setStructure
-
Constructor Details
-
DatabaseSaver
Constructor.- Throws:
Exception
- throws Exception if property file cannot be read
-
-
Method Details
-
main
Main method.- Parameters:
options
- should contain the options of a Saver.
-
setEnvironment
Set the environment variables to use.- Specified by:
setEnvironment
in interfaceEnvironmentHandler
- Parameters:
env
- the environment variables to use
-
resetOptions
public void resetOptions()Resets the Saver ready to save a new data set.- Overrides:
resetOptions
in classAbstractSaver
-
cancel
public void cancel()Cancels the incremental saving process and tries to drop the table if the write mode is CANCEL.- Overrides:
cancel
in classAbstractSaver
-
globalInfo
Returns a string describing this Saver.- Returns:
- a description of the Saver suitable for displaying in the explorer/experimenter gui
-
getTableName
@OptionMetadata(displayName="Table name", description="Sets the name of the table", displayOrder=4) public String getTableName()Gets the table's name.- Returns:
- the table's name
-
setTableName
Sets the table's name.- Parameters:
tn
- the name of the table
-
tableNameTipText
Returns the tip text for this property.- Returns:
- the tip text for this property
-
getTruncate
@OptionMetadata(displayName="Truncate table", description="Truncate (i.e. drop and recreate) table if it already exists", displayOrder=6) public boolean getTruncate()Get whether to truncate (i.e. drop and recreate) the table if it already exits. If false, then new data is appended to the table.- Returns:
- true if the table should be truncated first (if it exists).
-
setTruncate
public void setTruncate(boolean t) Set whether to truncate (i.e. drop and recreate) the table if it already exits. If false, then new data is appended to the table.- Parameters:
t
- true if the table should be truncated first (if it exists).
-
truncateTipText
Returns the tip text for this property.- Returns:
- the tip text for this property
-
getAutoKeyGeneration
@OptionMetadata(displayName="Automatic primary key", description="If set to true, a primary key column is generated automatically (containing the row number as INTEGER). The name of the key is read from DatabaseUtils (idColumn) This primary key can be used for incremental loading (requires an unique key). This primary key will not be loaded as an attribute.", displayOrder=7) public boolean getAutoKeyGeneration()Gets whether or not a primary key will be generated automatically.- Returns:
- true if a primary key column will be generated, false otherwise
-
setAutoKeyGeneration
public void setAutoKeyGeneration(boolean flag) En/Dis-ables the automatic generation of a primary key.- Parameters:
flag
- flag for automatic key-genereration
-
autoKeyGenerationTipText
Returns the tip text for this property.- Returns:
- tip text for this property
-
getRelationForTableName
@OptionMetadata(displayName="Use relation name", description="If set to true, the relation name will be used as name for the database table. Otherwise the user has to provide a table name.", displayOrder=5) public boolean getRelationForTableName()Gets whether or not the relation name is used as name of the table.- Returns:
- true if the relation name is used as the name of the table, false otherwise
-
setRelationForTableName
public void setRelationForTableName(boolean flag) En/Dis-ables that the relation name is used for the name of the table (default enabled).- Parameters:
flag
- if true the relation name is used as table name
-
relationForTableNameTipText
Returns the tip text fo this property.- Returns:
- the tip text for this property
-
getUrl
@OptionMetadata(displayName="Database URL", description="The URL of the database", displayOrder=1) public String getUrl()Gets the database URL.- Specified by:
getUrl
in interfaceDatabaseConverter
- Returns:
- the URL
-
setUrl
Sets the database URL.- Specified by:
setUrl
in interfaceDatabaseConverter
- Parameters:
url
- the URL
-
urlTipText
Returns the tip text for this property.- Returns:
- the tip text for this property
-
getUser
Gets the database user.- Specified by:
getUser
in interfaceDatabaseConverter
- Returns:
- the user name
-
setUser
@OptionMetadata(displayName="Username", description="The user name for the database", displayOrder=2) public void setUser(String user) Sets the database user.- Specified by:
setUser
in interfaceDatabaseConverter
- Parameters:
user
- the user name
-
userTipText
Returns the tip text for this property.- Returns:
- the tip text for this property
-
getPassword
@OptionMetadata(displayName="Password", description="The database password", displayOrder=3) @PasswordProperty public String getPassword()Returns the database password.- Returns:
- the database password
-
setPassword
Sets the database password.- Specified by:
setPassword
in interfaceDatabaseConverter
- Parameters:
password
- the password
-
passwordTipText
Returns the tip text for this property.- Returns:
- the tip text for this property
-
getCustomPropsFile
@OptionMetadata(displayName="DB config file", description="The custom properties that the user can use to override the default ones.", displayOrder=8) @FilePropertyMetadata(fileChooserDialogType=0, directoriesOnly=false) public File getCustomPropsFile()Returns the custom properties file in use, if any.- Returns:
- the custom props file, null if none used
-
setCustomPropsFile
Sets the custom properties file to use.- Parameters:
value
- the custom props file to load database parameters from, use null or directory to disable custom properties.
-
customPropsFileTipText
The tip text for this property.- Returns:
- the tip text
-
setDestination
Sets the database url.- Parameters:
url
- the database urluserName
- the user namepassword
- the password
-
setDestination
Sets the database url.- Parameters:
url
- the database url
-
setDestination
public void setDestination()Sets the database url using the DatabaseUtils file. -
getCapabilities
Returns the Capabilities of this saver.- Specified by:
getCapabilities
in interfaceCapabilitiesHandler
- Overrides:
getCapabilities
in classAbstractSaver
- Returns:
- the capabilities of this object
- See Also:
-
connectToDatabase
public void connectToDatabase()Opens a connection to the database. -
writeIncremental
Saves an instances incrementally. Structure has to be set by using the setStructure() method or setInstances() method. When a structure is set, a table is created.- Specified by:
writeIncremental
in interfaceSaver
- Overrides:
writeIncremental
in classAbstractSaver
- Parameters:
inst
- the instance to save- Throws:
IOException
- throws IOEXception.
-
writeBatch
Writes a Batch of instances.- Specified by:
writeBatch
in interfaceSaver
- Specified by:
writeBatch
in classAbstractSaver
- Throws:
IOException
- throws IOException
-
getOptions
Gets the setting.- Specified by:
getOptions
in interfaceOptionHandler
- Returns:
- the current setting
-
setOptions
Sets the options. Valid options are:-url <JDBC URL> The JDBC URL to connect to. (default: from DatabaseUtils.props file)
-user <name> The user to connect with to the database. (default: none)
-password <password> The password to connect with to the database. (default: none)
-T <table name> The name of the table. (default: the relation name)
-truncate Truncate (i.e. delete any data) in table before inserting
-P Add an ID column as primary key. The name is specified in the DatabaseUtils file ('idColumn'). The DatabaseLoader won't load this column.
-custom-props <file> The custom properties file to use instead of default ones, containing the database parameters. (default: none)
-i <input file name> Input file in arff format that should be saved in database.
- Specified by:
setOptions
in interfaceOptionHandler
- Parameters:
options
- the options- Throws:
Exception
- if options cannot be set
-
listOptions
Lists the available options.- Specified by:
listOptions
in interfaceOptionHandler
- Returns:
- an enumeration of the available options
-
getRevision
Returns the revision string.- Specified by:
getRevision
in interfaceRevisionHandler
- Returns:
- the revision
-