Skip to content

Using the JDBC Data Adapter

stanzikratel edited this page Sep 5, 2014 · 13 revisions

##### Table of Contents **[Configuring Atom Hopper to use JDBC Data Adapter](#configure-ah-jdbc-adapter)**
**[Searching Entries By Timestamp](#search-by-timestamp)**
**[Advanced Filtering by Category with the JDBC Adapter](#advance-filtering)**
**[Building Search Strings Using an LDAP Library](#build-ldap-search)**

###Configuring Atom Hopper to use JDBC Data Adapter###

For better HTTP GET performance make sure to use the JDBC ddl.

Note: Please note that the JDBC Data Adapter is currently in the stage of ongoing development.

The JDBC data adapter ships with Atom hopper version 1.2.3 and above. You can turn on the JDBC data adapter easily following these steps:

  • Make sure JDBC is installed and running where it can be seen by Atom Hopper. The default dbname is atomhopper
  • Go to the location where the Atom Hopper configuration files are located, by default this path is: /etc/atomhopper
  • Edit the application-context.xml file so it looks similar to the following (edit as needed to suit your particular needs):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="atomHopperDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/atomhopper" />
        <property name="username" value="postgres" />
        <property name="password" value="password" />
        <property name="minIdle" value="10" />
        <property name="maxIdle" value="25" />
        <property name="initialSize" value="10" />
        <property name="maxActive" value="50" />
        <property name="validationQuery" value="SELECT 1" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="10000" />
    </bean>

    <bean name="atomHopperJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="atomHopperDataSource"/>
    </bean>

    <bean name="jdbc-feed-publisher" class="org.atomhopper.jdbc.adapter.JdbcFeedPublisher">
        <property name="jdbcTemplate" ref="atomHopperJdbcTemplate" />
    </bean>

    <bean name="jdbc-feed-source" class="org.atomhopper.postgres.adapter.JdbcFeedSource">
        <property name="jdbcTemplate" ref="atomHopperJdbcTemplate" />
    </bean>
</beans>

NOTE: If you are deploying Atom Hopper in Tomcat, you can use Tomcat's JDBC pool by replacing all occurrences of org.apache.commons.dbcp.BasicDataSource to org.apache.tomcat.jdbc.pool.DataSource.

If you are deploying Atom Hopper in a container other than Tomcat, you may want to use the JDBC pool provided by the container by replacing all occurrences of org.apache.commons.dbcp.BasicDataSource to the appropriate class.

Alternatively, you can keep the above configuration as it, using Apache's commons-dbcp pool. You will have to copy the appropriate commons-dbcp-version.jar into the Atom Hopper's WEB-INF/lib directory.

  • Edit the atom-server.cfg.xml file (by default the file is located in: /etc/atomhopper) so it looks similar to the following (edit as needed to suit your particular needs):
<?xml version="1.0" encoding="UTF-8"?>
 
<atom-hopper-config xmlns="http://atomhopper.org/atom/hopper-config/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://atomhopper.org/atom/hopper-config/v1.0 ./../../config/atom-hopper-config.xsd">
    <defaults>
        <author name="Atom Hopper" />
    </defaults>
 
    <host domain="localhost:8080" />
 
    <workspace title="Testing Namespace" resource="/namespace/">
        <categories-descriptor reference="workspace-categories-descriptor" />
 
        <feed title="Testing Feed" resource="/feed">
            <publisher reference="jdbc-feed-publisher" />
            <feed-source reference="jdbc-feed-source" />
        </feed>
    </workspace>
</atom-hopper-config>

The important thing here is to ensure the following:

<publisher reference="jdbc-feed-publisher" />
<feed-source reference="jdbc-feed-source" />

Matches what is specified in the application-context.xml file.

<bean name="jdbc-feed-publisher" class="org.atomhopper.postgres.adapter.JdbcFeedPublisher">
  <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean name="jdbc-feed-source" class="org.atomhopper.postgres.adapter.JdbcFeedSource">
  <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

If Atom Hopper is currently running you will need to restart it for the changes to take effect.

###Searching Entries By Timestamp### As of version 1.2.19, if you would like to get entries starting at a certain timestamp, you can use the **startingAt** query parameter. The parameter takes an ISO 8601 Date and Time format (see http://en.wikipedia.org/wiki/ISO_8601). The parameter value **must** contain a timezone. For example: ```2014-03-10T06:00:00.000Z``` is a valid value for **startingAt** parameter, but ```2014-03-10T10:00:00.000``` is not a valid one.

The startingAt parameter can not be used together with the marker parameter.

If the startingAt parameter is used without a direction parameter, then direction FORWARD is assumed.

Examples:

  • To fetch entries whose timestamp is newer than 2014-03-10 00:00:00.000 UTC, you can use the following URL:
http://localhost:8080/namespace/feed/?startingAt=2014-03-10T00:00:00.000Z
  • To fetch entries whose timestamp is older than 2014-03-10 00:00:00.000 UTC, you can use this:
http://localhost:8080/namespace/feed/?startingAt=2014-03-10T00:00:00.000Z&direction=backward
###Advanced Filtering by Category with the JDBC Adapter###

Note: The advanced search capability in JDBC data adapter ships with Atom hopper version 1.2.8 and above.

Advanced filters (i.e. those requiring AND, OR and NOT and their combinations) by category is supported for LDAP style search parameters.

Single category Filter:

The following is an example of searching on a category entered as CAT1:

http://localhost:8080/namespace/feed/?search=(cat=CAT1)

Multiple Category AND Filter:

The following is an example of searching on a category entered as CAT1 and CAT2:

http://localhost:8080/namespace/feed/?search=(%26(cat=CAT1)(cat=CAT2))

Note: The %26 is the urlencoded value of the & operator. So the decoded search string above is (&(cat=CAT1)(cat=CAT2))

Additionally you can use the word "AND" in place of the & operator in the above call like this:

http://localhost:8080/namespace/feed/?search=(AND(cat=CAT1)(cat=CAT2))

Multiple Category OR Filter:

The following is an example of searching on a category entered as CAT1 or CAT2:

http://localhost:8080/namespace/feed/?search=(%7C(cat=CAT1)(cat=CAT2))

Note: The %7C is the urlencoded value of the | operator. So the decoded search string above is (|(cat=CAT1)(cat=CAT2))

Additionally you can use the word "OR" in place of the | operator in the above call like this:

http://localhost:8080/namespace/feed/?search=(OR(cat=CAT1)(cat=CAT2))

Single Category NOT Filter:

The following is an example of searching on a category entered as not CAT1:

http://localhost:8080/namespace/feed/?search=(!(cat=CAT1))

Additionally you can use the word "NOT" in place of the ! operator in the above call like this:

http://localhost:8080/namespace/feed/?search=(NOT(cat=CAT1))

Multiple Category COMBINED Filter:

The following is an example of searching on a category entered as CAT1 and ( CAT2 or CAT3 ) but not CAT4:

http://localhost:8080/namespace/feed/?search=(%26(cat=CAT1)(%7C(cat=CAT2)(cat=CAT3))(!(cat=CAT4)))

Note: The %26 is the urlencoded value of the & operator and the %7C is the urlencoded value of the | operator. So the decoded search string above is (&(cat=CAT1)(|(cat=CAT2)(cat=CAT3))(!(cat=CAT4)))

Additionally you can use the word "AND" in place of the & operator and the word "OR" in place of the | operator in the above call like this:

http://localhost:8080/namespace/feed/?search=(AND(cat=CAT1)(OR(cat=CAT2)(cat=CAT3))(NOT(cat=CAT4)))
###Building Search Strings Using an LDAP Library### Here's an example of using an [LDAP library](https://www.unboundid.com/products/ldap-sdk/) to create the search strings. **Note:** The string will still need to be url encoded because of the '&' and '|' characters. ``` import com.unboundid.ldap.sdk.Filter;

public static void main(String[] args) {

 Filter cat1 = Filter.createEqualityFilter("cat", "CAT1");
 Filter cat2 = Filter.createEqualityFilter("cat", "CAT2");
 Filter cat3 = Filter.createEqualityFilter("cat", "CAT3");
 Filter cat4 = Filter.createEqualityFilter("cat", "CAT4");

 System.out.println(cat1.toString()); // (cat=CAT1)

 Filter andFilter = Filter.createANDFilter(cat1, cat2);
 Filter orFilter = Filter.createORFilter(cat1, cat2);
 Filter notFilter = Filter.createNOTFilter(cat1);

 System.out.println(andFilter.toString()); // (&(cat=CAT1)(cat=CAT2))
 System.out.println(orFilter.toString()); // (|(cat=CAT1)(cat=CAT2))
 System.out.println(notFilter.toString()); // (!(cat=CAT1))

 Filter orForComplex = Filter.createORFilter(cat2,cat3);
 Filter notForComplex = Filter.createNOTFilter(cat4);
 Filter complex = Filter.createANDFilter(cat1, orForComplex, notForComplex);

 System.out.println(complex.toString()); // (&(cat=CAT1)(|(cat=CAT2)(cat=CAT3))(!(cat=CAT4)))

}