Anda di halaman 1dari 14

MySQL Reader/Writer

FORMAT NOTES: This format is not supported by FME Base Edition.

Overview
MySQL1 is a well known DataBase Management System (DBMS) that provides various types of tables for different database applications. Version 4.1.0 and above MySQL
databases are spatially extended to incorporate the Open GIS Consortium Simple Features Specification for simple geometric features. The MySQL reader and writer plugin provides the Feature Manipulation Engine (FME) with the ability to read and write
both attribute and geometric data stored in a MySQL database. There are two versions
of the reader and writer, MySQL which includes the spatial extension and supports geometry features, and MySQL_DB which is an attribute only version which ignores geometry. Currently the table types that can be read are MyISAM, InnoDB and MEMORY.
Note that only MyISAM tables currently support geometry columns.
The MySQL reader and writer are specifically designed to handle the geometric portions
of the data when present and are optimized for non-geometry or attribute processing
when no spatial columns are present. The MySQL reader and writer communicate directly with the MySQL C API interface for maximum throughput.
This chapter assumes familiarity with MySQL, the table types, column types, OGC geometry types, available server daemons, indexing mechanisms and connection parameters.
Please note that MySQL functionality that only exists in the MySQL road map and not
in practice such as integration of the Spatial Reference Systems (SRS), spatial metadata tables, spatial operators, server side cursors and views were considered but are
not currently integrated into the MySQL reader for FME.
FME has been "MySQL Network Certified" against the MySQL Pro certified binary
version 4.1.10a. MySQL Network is the only MySQL version certified for FME; users are
encouraged to migrate to MySQL Network. For more information, please see the
MySQL home page at: http://www.mysql.com/

1. MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries.

Safe Software Inc.

1261

MySQL Reader/Writer

MySQL Quick Facts


Format Type Identifier

MYSQL

Reader/Writer

Both

Dataset Type

Database

Feature Type

Table name

Typical File Extensions

None

Automated Translation Support

Yes

User-Defined Attributes

Yes

Coordinate System Support

No

Generic Color Support

No

Spatial Index

Always

Schema Required

Yes

Transaction Support

Yes

Geometry Type Attribute


Geometry Support

mysql_type

Geometry

Supported?

Geometry

Supported?

aggregate

yes

polygon

yes

circles

no

donut polygon

yes

circular arc

no

line

yes

elliptical arc

no

point

yes

ellipses

no

text

no

none

yes

3D

no

Reader Overview
The FME considers a MySQL dataset to be a database containing a collection of relational tables, and a table to be an FME feature type with each row corresponding to at
least one FME feature. In the case of geometry collections, a single row may become
more than one FME feature.
Tables schemas must be defined in the FME mapping file before they can be read and
can consist of both geometric and primary column types, however, only one geometry
column is read per table while others are discarded.
Arbitrary WHERE clauses and joins are fully supported as well as an entire arbitrary SQL
SELECT statement. Support for @SQL and @Relate functions has also been added and
further information is available for these functions in the FME Functions, Factories and
Transformers document available from the Safe Software website.
The basic reading process involves opening a connection to the database, querying
metadata, and querying data. The data is read by submitting SQL queries and parsing
the returned result sets. Currently, result sets are either entire fetched to the client
machine or fetched one at a time from.
If NULL geometries are read, they are treated as non-geometry features and the attributes are preserved.

1262

FME Readers and Writers

MySQL Reader/Writer

Reader Directives

Reader Directives
The suffixes listed are prefixed by the current <ReaderKeyword> in a mapping file. By
default, the <ReaderKeyword> for the MySQL reader is MYSQL_IN.
DATASET
Required/Optional: Required
This specifies the name of the MySQL database. The database must exist in the DBMS.
This can be verified by executing the query SHOW DATABASES in the MySQL query interpreter.
Example:
MYSQL_DATASET testdb
HOST
Required/Optional: Required
This specifies the machine running the MySQL DBMS as either an IP address or host
name. The database must have proper permissions and be set up to accept TCP/IP connections if connecting from a remote machine.
MYSQL_IN_HOST myserver

PORT
Required/Optional: Required
When connecting remotely, this specifies the TCP/IP post on which to connect to the
DBMS service. The default port is 3306.
MYSQL_IN_PORT 3306

USER_NAME
Required/Optional: Required
The name of user who will access the database. The named user must exist with appropriate MySQL permissions. The default user name is mysql.
MYSQL_IN_USER_NAME mysql
PASSWORD
Required/Optional: Optional
The password of the user accessing the database. This parameter is optional when connecting with a username that has a blank password associated with it.
MYSQL_IN_PASSWORD secret

Safe Software Inc.

1263

Reader Directives

MySQL Reader/Writer

DEF
Required/Optional: Required
The syntax of the definition is:
MYSQL_DEF <tableName>
[mysql_where_clause
<whereClause>]
[<fieldName> <fieldType>] +

\
\

OR
MYSQL <queryName>
[mysql_sql_statement

\
<sqlQuery>]

The <tableName> must match the name of an existing MySQL table in the database.
This will be used as the feature type of all the features read from the table. The exception to this rule is when using the sql_statement keyword. In this case, the DEF name
may be any valid alphabetic identifier; it does not have to be an existing table name
rather, it is an identifier for the custom SQL query. The feature type of all the features
returned from the SQL query are given the query name.
The <fieldType> of each field must be given, but it is not verified against the database
definition for the field. In effect, it is ignored. The exception to this is the geometry
field type which is not placed in the DEF.
The definition allows specification of separate search parameters for each table. If any
of the per table configuration parameters are given, they will override, for that table,
whatever global values have been specified by the reader keywords listed above. If any
of these parameters is not specified, the global values will be used.
The following table summarizes the definition line configuration parameters:

1264

Parameter

Contents

where_clause

This specifies the SQL WHERE clause applied to the attributes of the layers features to limit the set of features
returned. If this is not specified, then all the tuples are returned. This keyword will be ignored if the sql_statement is
present.

sql_statement

This specifies an SQL SELECT query to be used as the


source for the results. If this is specified, the MySQL reader
will execute the query, and use the resulting rows as the
features instead of reading from the table <queryName>.
All returned features will have a feature type of <queryName>, and attributes for all columns selected by the
query.
The where_clause and all parameters which specify a spatial constraint are ignored if sql_statement is supplied.
If selecting a geometry column from a MySQL table, the
MySQL function AsBinary(<geometry column name>)
should be used to return the geometry in binary format.
This is necessary for performance since the MySQL reader
does not parse the WKT geometry strings but instead takes
WKB strings and converts them to FME geometries.

FME Readers and Writers

MySQL Reader/Writer

Reader Directives

If no <whereClause> is specified, all rows in the table will be read and returned as individual features. If a <whereClause> is specified, only those rows that are selected by
the clause will be read. Note that the <whereClause> does not include the word WHERE.
The MySQL reader allows one to use the sql_statement parameter to specify an arbitrary SQL SELECT query on the DEF line. If this is specified, FME will execute the query,
and use each row of data returned from the query to define at least one feature. Each
of these features will be given the feature type named in the DEF line, and will contain
attributes for every column returned by the SELECT. In this case, all DEF line parameters regarding a WHERE clause or spatial querying are ignored, as it is possible to embed
this information directly in the text of the <sqlQuery>.
The following example selects rows from the table ROADS, placing the resulting data
into FME features with a feature type of MYROADS. Imagine that ROADS defines the geometry for the roads, and has a numeric field named ID, a text field named NAME and
a geometry column named GEOM.
MYSQL_DEF MYROADS
sql_statementSELECT id, name, AsBinary(geom) FROM ROADS

IDs
Required/Optional: Optional
This optional specification is used to limit the available and defined database tables
files that will be read. If no IDs are specified, then all defined and available tables are
read. The syntax of the IDs keyword is:
MYSQL_IDs <featureType1>
<featureType2>
<featureTypeN>

\
\

The feature types must match those used in DEF lines.


The example below selects only the ROADS table for input during a translation:
MYSQL_IDs ROADS
MINX, MINY, MAXX, MAXY
Required/Optional: Optional
These keywords when used together specify the spatial extent of the feature retrieval.
Only features that interact with the bounding box defined by these keyword values are
returned. If this is not supplied, all features will be returned. If either min value is
greater than the corresponding max value, the values will be swapped. If less than the
entire set of four values are supplied, the supplied values will be ignored and all features will be returned.
Note: Not supported in MySQL_DB.

The syntax of the directives is:


MYSQL_IN_MINX
MYSQL_IN_MINY
MYSQL_IN_MAXX
MYSQL_IN_MAXY

Safe Software Inc.

<minX>
<minY>
<maxX>
<maxY>

1265

Reader Directives

MySQL Reader/Writer

The example below selects a small area for extraction:


MYSQL_IN_MINX
MYSQL_IN_MINY
MYSQL_IN_MAXX
MYSQL_IN_MAXY

25.6
59.0
79.2
124.5

SEARCH_ENVELOPE
Required/Optional: Optional
This keyword is used to specify the spatial extent of the feature retrieval. Only features
that interact with the bounding box defined by these keyword values are returned. If
this is not supplied, all features will be returned. If either min value is greater than the
corresponding max value, the values will be swapped. If less than the entire set of four
values are supplied, the supplied values will be ignored and all features will be returned.
Note: Not supported in MySQL_DB.

This keyword supersedes the use of the MYSQL_IN_MINX, MYSQL_IN_MINY,


MYSQL_IN_MAXX, MYSQL_IN_MAXY keywords which are only used as a secondary fallback when this keyword is empty.
The syntax of the MYSQL_IN_SEARCH_ENVELOPE directive is:
MYSQL_IN_SEARCH_ENVELOPE <minX> <minY> <maxX> <maxY>
The example below selects a small area for extraction:
MYSQL_IN_SEARCH_ENVELOPE 25.6 59.0 79.2 124.5
FETCH_ALL_ROWS
Required/Optional: Optional
This keyword is used to specify that the entire result set of the main feature query
should be retrieved into a large buffer in client memory. Otherwise, the default is to be
less memory intensive and to retrieve one row at a time from the server, caching them
in a smaller buffer. This allows for large datasets to be processed by default without
the possibility of running out of client memory. This keyword can be set to YES to improve the performance of smaller queries.
RETRIEVE_ALL_SCHEMAS
Required/Optional: Optional
This specification is only applicable when generating a mapping file, generating a workspace or when retrieving schemas in a FME Objects application.
When set to Yes, indicates to the reader to return all the schemas of the tables in the
database.
If this specification is missing then it is assumed to be No.
Range: YES | NO
Default: NO

1266

FME Readers and Writers

MySQL Reader/Writer

Writer Directives

RETRIEVE_ALL_TABLE_NAMES
Required/Optional: Optional
This specification is only applicable when generating a mapping file, generating a workspace or when retrieving schemas in a FME Objects application.
Similar to RETRIEVE_ALL_SCHEMAS: this optional specification is used to tell the reader
to only retrieve the table names of all the tables in the source database. If
RETRIEVE_ALL_SCHEMAS is also set to Yes, then RETRIEVE_ALL_SCHEMAS takes precedence. If this value is not specified, then it is assumed to be No.
Range: YES | NO
Default: NO

Writer Overview
The MySQL writer module stores both geometry and attributes into an MySQL database. The MySQL writer provides the following capabilities:
Transaction Support: The MySQL writer provides transaction support that eases
the data loading process. Occasionally, a data load operation terminates prematurely due to data difficulties. The transaction support provides a mechanism for
reloading corrected data without data loss or duplication. Performance can also be
improved by reducing transactional overhead for multiple small queries such as
inserts.
Index Creation: The MySQL writer can set up and populate indexes as part of the
loading process. By default, a spatial R-Tree index is created on the primary geometry column of a table and not on any other columns. Additional columns can be
individually indexed. Composite column indexes are not supported at this time.
Insert Binding: By default, the MySQL writer uses prepared statements and query
parameter binding ensure speedy data loading.

Writer Directives
The suffixes shown are prefixed by the current <WriterKeyword> in a mapping file. By
default, the <WriterKeyword> for the MySQL writer is MYSQL_OUT.
DATASET/DATABASE, HOST, PORT, USER_NAME, PASSWORD
These directives operate in the same manner as they do for the MySQL reader. The
remaining writer-specific directives are discussed in the following sections.
DEF
Required/Optional: Optional
Each MySQL table must be defined before it can be written. The general form of a
MySQL definition statement is:
MYSQL_DEF <tableName>
[mysql_type <mysql_type>]

Safe Software Inc.

\
\

1267

Writer Directives

MySQL Reader/Writer

[mysql_mode (inherit_from_writer|insert|update|delete)]
[mysql_table_type <mysql_table_type>]
[mysql_geometry_column <column>]
[mysql_create_spatial_index (yes|no)]
[mysql_overwrite_table (yes|no|trunc)]
[<fieldName> <fieldType>][,<indexType>]*

\
\
\
\
\

The table definition allows control of the table that will be created. If the table already
exists, the majority of the mysql_ parameters will be ignored and need not be given.
If the fields and types are listed, they must match those in the database.
If the table does not exist, then the field names and types are used to first create the
table. In any case, if a <fieldType> is given, it may be any field type supported by the
target database.
The configuration parameters present on the definition line are described in the following table:
Parameter

Contents

tableName

The name of the table to be written. If a table with the specified


name exists, it will be overwritten if either the
mysql_overwrite_table DEF line parameter set to YES or if the
global writer keyword type mysql_out_overwrite is set to YES.
Otherwise the table will be appended. Valid values for table names
include any character string devoid of SQL-offensive characters and
less than 32 characters in length.

mysql_type

The type of geometric entity stored within the feature. The valid values for the type are listed below:
mysql_point
mysql_line
mysql_area
mysql_geoemtrycollection
mysql_none

mysql_mode

The the default operation mode of the feature type in terms of the
types of SQL statements sent to the database.Valid values are INSERT, UPDATE, DELETE and INHERIT_FROM_WRITER. Note that
INSERT mode allows for only INSERT operations where as UPDATE
and DELETE can be overwritten at the feature levels.
INHERIT_FROM_WRITER simply indicates to take this value from
the writer level and not to override it at the feature type level.
Default: INHERIT_FROM_WRITER

mysql_table_type

The type of table to be created. The valid values for the type are listed below:
MEMORY
InnoDB
MYISAM
Please refer to the MySQL manual for further information on these
table types.
Default: MyISAM

1268

FME Readers and Writers

MySQL Reader/Writer

Writer Directives

Parameter

Contents

mysql_geometry_
column

This specifies the name of the column to be created that will hold
the geometry when creating a new MySQL table. If this value is set
to blank no geometry column will be created and an attribute only
table will be written. This may invalidate other geometry related parameters. Not supported in MySQL_DB.
Default: geom

mysql_create_spatial_in
dex

This specifies that a spatial index be created on the geometry column named in the above parameter. If no geometry column exists,
this parameter is ignored. If this keyword is set, the geometry column will be designated as NOT NULL which is required for spatial
index creation and attempts to insert NULL geometries will result in
those features not being added to the table. If you want a table that
contains a geometry column that may contain NULL geometries,
simply set this option to NO and the geometry column will not be
specified as NO NULL and NULL geometries may be inserted. However, this means that no spatial index will be created either. It is
recommended that the spatial and nonspatial data be separated into
distinct tables and efforts made to ensure the geometries to be inserted are NOT NULL where possible to improve the overall spatial
performance of the database. Not supported in MySQL_DB.
Default: YES

mysql_overwrite_
table

This specifies that if the table exists by this name, it should be overwritten by this definition.
Default: NO

fieldName

The name of the field to be written. Valid values for field name include any character string devoid of SQL-offensive characters and
less than 63 characters in length.

fieldType

The type of a column in a table. The valid values for the field type
are listed below:
int
smallint
tinyint
mediumint
bigint
decimal(width, precision)
float(width, precision)
double(width, precision)
char(width)
varchar(width)
date
time
datetime
timestamp
year
tinyblob
blob
mediumblob
longblob
tinytext
text
mediumtext
longtext

Safe Software Inc.

1269

Writer Directives

MySQL Reader/Writer

Parameter

Contents

indexType

The type of index to create on the given field. The valid values for
the index type are listed below:
BTREE (default attribute index)
PRIKEY(primary key)

START_TRANSACTION
Required/Optional: Optional
This statement tells the MySQL writer module when to start actually writing features
into the database. The MySQL writer does not write any features until the feature number of features are skipped, and then it begins writing the following features. Normally,
the value specified is zero a non-zero value is usually only specified when a data load
operation is being resumed after failing partway through.
Example:
MYSQL_OUT_START_TRANSACTION 0
TRANSACTION_INTERVAL
Required/Optional: Optional
This statement informs the FME about the number of features to be placed in each
transaction before a transaction is committed to the database. Setting the transaction
interval 0 results in the enabling of auto commit transaction mode.
If the MYSQL_OUT_TRANSACTION_INTERVAL statement is not specified, then a value of
1000 is used as the transaction interval.
Example:
MYSQL_OUT_TRANSACTION_INTERVAL 2000
WRITER_MODE
Required/Optional: Optional
This keyword informs the MySQL writer which SQL operations will be performed by default by this writer. This operation can be set to INSERT, UPDATE or DELETE. The default
writer level value for this operation can be overwritten at the feature type or table level. The corresponding feature type def parameter name is called MYSQL_MODE. It has
the same valid options as the writer level mode and additionally the value
INHERIT_FROM_WRITER which causes the writer level mode to be inherited by the feature type as the default for features contained in that table.
The operation can be set specifically for individual feature as well. Note that when the
writer mode is set to INSERT this prevents the mode from being interpreted from individual features and all features are inserted unless otherwise marked as UPDATE or
DELETE features. These are skipped.
If the MYSQL_WRITER_MODE statement is not specified, then a value of INSERT is given.
Example:

1270

FME Readers and Writers

MySQL Reader/Writer

No Coordinates

MYSQL_OUT_WRITER_MODE INSERT

Feature Representation
Features read from MySQL consist of a series of attribute values and geometry. The
feature type of each feature is as defined on its DEF line. The geometry object model
in MySQL follows the OGIS Simple Features Specification 1.1. For more information see
http://www.opengis.org.
Features written to the database have the destination table as their feature type, and
attributes as defined on the DEF line.
Features read from, or written to, MySQL also have an attribute for each column in the
database table. The feature attribute name will be the same as the source or destination column name. The attribute and column names are case-insensitive as opposed to
database and table names whose case sensitivity depends on the underlying operating
system.

No Coordinates
mysql_type: mysql_none
Features with no coordinates are tagged with this value when reading or writing to or
from MySQL. Note also that this is not a valid OGIS Simple Features geometry type.
These features have a dimension of -1.

Points
mysql_type: mysql_point
Features tagged with this value consist of a single point. Both singular points and aggregates of points are supported. Point aggregates will become multipoints while singular points will become points in MySQL. Points have a dimension of 0.

Line
mysql_type: mysql_lines
Linear features are tagged with this value when reading or writing to or from MySQL.
A linestring consists of one or more ordered two-point line segments. Line aggregates
will become multilinestrings while singular lines will become linestrings in MySQL.
Linestrings have a dimension of 1.

Area
mysql_type: mysql_area
Area or polygon features are tagged with this value when reading or writing to or from
MySQL. Both single-part and aggregate area features are supported. An area feature
may be either a polygon or a donut polygon. Note that NO checking is done to ensure
that the area features adhere to the geometry rules of MySQL as they are loaded. Area
aggregates will become multipolygons while singular areas will become polygons in
MySQL. Areas have a dimension of 2.

Safe Software Inc.

1271

GeometryCollections

MySQL Reader/Writer

GeometryCollections
mysql_type: mysql_geometrycollection
Aggregates containing heterogeneous collections of point, line and polygon features
are processed through FME as single features when reading or writing to or from
MySQL. Thus by default no geometry_collections are produced.
When reading geometry collection features from MySQL, the features in the collection
are split out into individual FME features and are tagged with the collection number and
item number to identify their origin. This results in a less accurate representation of
the geometry actually contained in the MySQL table, but greatly simplifies the processing of such data and its writing to several other formats. When writing back to MySQL,
the mysql_type can be manually set to mysql_geometrycollection and they will be
rewritten as collections.

Troubleshooting
Problems sometimes arise when attempting to connect to a MySQL database. This is
almost always due to a misconfiguration in the users environment. The following suggestions can often help detect and overcome such problems.
Ensure you can connect to the database with the host, port, database, user name,
and password using the mysql client. See MySQL documentation for proper security
and connection information, and for the usage of the mysql client.
If you try to list the tables and nothing happens, check the log file. There may have
been an underlying error that didnt generate a dialog. Usually this means a parameter does not exist or permissions are not sufficient to access the requested
resource.
In most cases, the MYSQL_DATABASE keyword should be left with blank values, with
the MYSQL_DATASET keyword containing the name of the MySQL database.
When using a UNIX operating system, the environment variables MYSQL_HOST,
MYSQL_TCP_PORT, USER and MYSQL_PWD can be used to specify the MySQL connection parameters.
If you receive an error like Lost connection to MySQL server during query, you may
have to increase your maximum allowed packet size server variable. This can be
done by editing the my .ini file in the MySQL server installation directory. The line
to edit, or add if not present, is:
max_allowed_packets=8M
The default is 1M, meaning maximum packet size is 1 MB. We recommend 8MB
(8M) or more if you are moving large geometric features.
Various other issues may arise depending on the values of the various option files
permitted for a MySQL database and for specific servers.
Lastly, try reading up on common MySQL problems at http://dev.mysql.com/
doc/mysql/en/problems.html

1272

FME Readers and Writers

MySQL Reader/Writer

Translation Errors in Workbench

Translation Errors in Workbench


There is a known issue with the MySQL Writer and Workbench Feature Type Properties.

The Feature Type Properties for


the MySQL Writer are shown at
right.
The Database User field should
be left blank. Entering a username
in the field may cause the FME
translation to fail. However, even
if the translation is successful,
MySQL will not be able to read the
resulting table.

Safe Software Inc.

1273

Translation Errors in Workbench

1274

MySQL Reader/Writer

FME Readers and Writers

Anda mungkin juga menyukai