This project has retired. For details please refer to its Attic page.
SqlStore (Apache Gora 0.2 API)

org.apache.gora.sql.store
Class SqlStore<K,T extends Persistent>

java.lang.Object
  extended by org.apache.gora.store.impl.DataStoreBase<K,T>
      extended by org.apache.gora.sql.store.SqlStore<K,T>
All Implemented Interfaces:
Closeable, DataStore<K,T>, org.apache.hadoop.conf.Configurable, org.apache.hadoop.io.Writable

public class SqlStore<K,T extends Persistent>
extends DataStoreBase<K,T>

A DataStore implementation for RDBMS with a SQL interface. SqlStore uses the JOOQ API and various JDBC drivers to communicate with the DB. Through use of the JOOQ API this SqlStore aims to support numerous SQL database stores namely; DB2 9.7 Derby 10.8 H2 1.3.161 HSQLDB 2.2.5 Ingres 10.1.0 MySQL 5.1.41 and 5.5.8 Oracle XE 10.2.0.1.0 and 11g PostgreSQL 9.0 SQLite with inofficial JDBC driver v056 SQL Server 2008 R8 Sybase Adaptive Server Enterprise 15.5 Sybase SQL Anywhere 12 This DataStore is currently in development, and requires a complete re-write as per GORA-86 Please see https://issues.apache.org/jira/browse/GORA-86


Nested Class Summary
static class SqlStore.DBVendor
          The vendor of the DB
 
Field Summary
protected static String DEFAULT_MAPPING_FILE
           
protected static String DRIVER_CLASS_PROPERTY
          The JDBC Driver class name
protected static String PASSWORD_PROPERTY
          Password to access the database
protected static String URL_PROPERTY
          JDBC Database access URL
protected static String USERNAME_PROPERTY
          User name to access the database
 
Fields inherited from class org.apache.gora.store.impl.DataStoreBase
autoCreateSchema, beanFactory, conf, datumReader, datumWriter, fieldMap, keyClass, persistentClass, properties, schema
 
Constructor Summary
SqlStore()
           
 
Method Summary
 void close()
          Close the DataStore.
 void createSchema()
          Creates the optional schema or table (or similar) in the datastore to hold the objects.
protected  void createSqlTable()
           
 boolean delete(K key)
          Deletes the object with the given key
 long deleteByQuery(Query<K,T> query)
          Deletes all the objects matching the query.
 void deleteSchema()
          Deletes the underlying schema or table (or similar) in the datastore that holds the objects.
 Result<K,T> execute(Query<K,T> query)
          Executes the given query and returns the results.
 void flush()
          Forces the write caches to be flushed.
 T get(K key, String[] requestFields)
          Returns the object corresponding to the given key.
protected  byte[] getBytes()
           
protected  Connection getConnection()
           
protected  String getIdentifier()
           
 List<PartitionQuery<K,T>> getPartitions(Query<K,T> query)
          Partitions the given query and returns a list of PartitionQuerys, which will execute on local data.
 String getSchemaName()
          Returns the schema name given to this DataStore
protected  void initDbMetadata()
           
 void initialize()
           
 Query<K,T> newQuery()
          Constructs and returns a new Query.
 void put(K key, T persistent)
          Inserts the persistent object with the given key.
protected  Object readField()
           
protected  SqlMapping readMapping()
           
 T readObject(ResultSet rs, T persistent, String[] requestFields)
           
 K readPrimaryKey(ResultSet resultSet)
           
 boolean schemaExists()
          Returns whether the schema that holds the data exists in the datastore.
protected  void setBytes()
           
protected  void setField()
          Serializes the field using Avro to a BLOB field
 void setObject(PreparedStatement statement, int index, Object object, org.apache.avro.Schema schema, Column column)
          Sets the object to the preparedStatement by it's schema
protected
<V> void
setObject(PreparedStatement statement, int index, V object, int objectType, Column column)
           
 
Methods inherited from class org.apache.gora.store.impl.DataStoreBase
equals, get, getBeanFactory, getConf, getFieldsToQuery, getKeyClass, getOrCreateConf, getPersistentClass, getSchemaName, initialize, newKey, newPersistent, readFields, setBeanFactory, setConf, setKeyClass, setPersistentClass, truncateSchema, write
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DRIVER_CLASS_PROPERTY

protected static final String DRIVER_CLASS_PROPERTY
The JDBC Driver class name

See Also:
Constant Field Values

URL_PROPERTY

protected static final String URL_PROPERTY
JDBC Database access URL

See Also:
Constant Field Values

USERNAME_PROPERTY

protected static final String USERNAME_PROPERTY
User name to access the database

See Also:
Constant Field Values

PASSWORD_PROPERTY

protected static final String PASSWORD_PROPERTY
Password to access the database

See Also:
Constant Field Values

DEFAULT_MAPPING_FILE

protected static final String DEFAULT_MAPPING_FILE
See Also:
Constant Field Values
Constructor Detail

SqlStore

public SqlStore()
Method Detail

initialize

public void initialize()
                throws IOException
Throws:
IOException

getSchemaName

public String getSchemaName()
Description copied from interface: DataStore
Returns the schema name given to this DataStore

Returns:
schema name

close

public void close()
           throws IOException
Description copied from interface: DataStore
Close the DataStore. This should release any resources held by the implementation, so that the instance is ready for GC. All other DataStore methods cannot be used after this method was called. Subsequent calls of this method are ignored.

Throws:
IOException

createSchema

public void createSchema()
                  throws IOException
Description copied from interface: DataStore
Creates the optional schema or table (or similar) in the datastore to hold the objects. If the schema is already created previously, or the underlying data model does not support or need this operation, the operation is ignored.

Throws:
IOException

deleteSchema

public void deleteSchema()
                  throws IOException
Description copied from interface: DataStore
Deletes the underlying schema or table (or similar) in the datastore that holds the objects. This also deletes all the data associated with the schema.

Throws:
IOException

schemaExists

public boolean schemaExists()
                     throws IOException
Description copied from interface: DataStore
Returns whether the schema that holds the data exists in the datastore.

Returns:
whether schema exists
Throws:
IOException

delete

public boolean delete(K key)
               throws IOException
Description copied from interface: DataStore
Deletes the object with the given key

Parameters:
key - the key of the object
Returns:
whether the object was successfully deleted
Throws:
IOException

deleteByQuery

public long deleteByQuery(Query<K,T> query)
                   throws IOException
Description copied from interface: DataStore
Deletes all the objects matching the query. See also the note on visibility.

Parameters:
query - matching records to this query will be deleted
Returns:
number of deleted records
Throws:
IOException

flush

public void flush()
           throws IOException
Description copied from interface: DataStore
Forces the write caches to be flushed. DataStore implementations may optimize their writing by deferring the actual put / delete operations until this moment. See also the note on visibility.

Throws:
IOException

get

public T get(K key,
             String[] requestFields)
                         throws IOException
Description copied from interface: DataStore
Returns the object corresponding to the given key.

Parameters:
key - the key of the object
requestFields - the fields required in the object. Pass null, to retrieve all fields
Returns:
the Object corresponding to the key or null if it cannot be found
Throws:
IOException

execute

public Result<K,T> execute(Query<K,T> query)
                                       throws IOException
Description copied from interface: DataStore
Executes the given query and returns the results.

Parameters:
query - the query to execute.
Returns:
the results as a Result object.
Throws:
IOException

readPrimaryKey

public K readPrimaryKey(ResultSet resultSet)
                 throws SQLException
Throws:
SQLException

readObject

public T readObject(ResultSet rs,
                    T persistent,
                    String[] requestFields)
                                throws SQLException,
                                       IOException
Throws:
SQLException
IOException

getBytes

protected byte[] getBytes()
                   throws SQLException,
                          IOException
Throws:
SQLException
IOException

readField

protected Object readField()
                    throws SQLException,
                           IOException
Throws:
SQLException
IOException

getPartitions

public List<PartitionQuery<K,T>> getPartitions(Query<K,T> query)
                                                           throws IOException
Description copied from interface: DataStore
Partitions the given query and returns a list of PartitionQuerys, which will execute on local data.

Parameters:
query - the base query to create the partitions for. If the query is null, then the data store returns the partitions for the default query (returning every object)
Returns:
a List of PartitionQuery's
Throws:
IOException

newQuery

public Query<K,T> newQuery()
Description copied from interface: DataStore
Constructs and returns a new Query.

Returns:
a new Query.

put

public void put(K key,
                T persistent)
         throws IOException
Description copied from interface: DataStore
Inserts the persistent object with the given key. If an object with the same key already exists it will silently be replaced. See also the note on visibility.

Throws:
IOException

setObject

public void setObject(PreparedStatement statement,
                      int index,
                      Object object,
                      org.apache.avro.Schema schema,
                      Column column)
               throws SQLException,
                      IOException
Sets the object to the preparedStatement by it's schema

Throws:
SQLException
IOException

setObject

protected <V> void setObject(PreparedStatement statement,
                             int index,
                             V object,
                             int objectType,
                             Column column)
                  throws SQLException,
                         IOException
Throws:
SQLException
IOException

setBytes

protected void setBytes()
                 throws SQLException
Throws:
SQLException

setField

protected void setField()
                 throws IOException,
                        SQLException
Serializes the field using Avro to a BLOB field

Throws:
IOException
SQLException

getConnection

protected Connection getConnection()
                            throws IOException
Throws:
IOException

initDbMetadata

protected void initDbMetadata()
                       throws IOException
Throws:
IOException

getIdentifier

protected String getIdentifier()

createSqlTable

protected void createSqlTable()

readMapping

protected SqlMapping readMapping()
                          throws IOException
Throws:
IOException


Copyright © 2010-2013 The Apache Software Foundation. All Rights Reserved.