简体   繁体   中英

Ignite write-through with Postgres backend data conversion

I'm setting up Apache Ignite with Postgres as a persistent store. In postgres I have JSON and Array columns, so I implemented custom transformer to convert everything that is not primitive to JSON string. Here is the code:

public class PostgresTransformer extends JdbcTypesDefaultTransformer {

    @Override
    public Object getColumnValue(ResultSet rs, int colIdx, Class<?> type) throws SQLException {
        if (type == ArrayOfFloat.class) {
            Array raw = rs.getArray(colIdx);
            ArrayOfFloat obj = new ArrayOfFloat(raw);
            return obj.toString();
        }

        if (type == ArrayOfInt.class) {
            Array raw = rs.getArray(colIdx);
            ArrayOfInt obj = new ArrayOfInt(raw);
            return obj.toString();
        }

        if (type == ArrayOfString.class) {
            Array raw = rs.getArray(colIdx);
            ArrayOfString obj = new ArrayOfString(raw);
            return obj.toString();
        }

        if (type == JSON.class) {
            String raw = rs.getString(colIdx);
            JSON obj = new JSON(raw);
            return obj.toString();
        }

        return super.getColumnValue(rs, colIdx, type);
    }
}

In main-server.xml I also use this corresponding configuration:

<property name="transformer">
     <bean class="com.mycorp.ignite.transformers.PostgresTransformer"/>
</property>

<bean class="org.apache.ignite.cache.store.jdbc.JdbcTypeField">
      <constructor-arg>
            <util:constant static-field="java.sql.Types.OTHER"/>
      </constructor-arg>
      <constructor-arg value="neighbourhood_stats"/>
      <constructor-arg value="com.mycorp.ignite.types.JSON"/>
      <constructor-arg value="neighbourhoodStats"/>
</bean>

This works well for me for read-through. My question is: How to convert it back to valid postgres data type when i'm saving data back to DB via wright-through? There is no setColumnValue method in transformer. Only getColumnValue . Thanks!

I don't think you should use CacheJdbcPojoStore in this case. Just create your own implementation of the CacheStore interface and do all the required conversions there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM