ignite系列之19-数据源扩展Postgresql
storeFactory.setDialect(dialect);
storeFactory.setDataSourceFactory(new DefaultDataSourceFactory((Object)null));
storeFactory.setCacheName(igniteTable.tableName());
import javax.cache.configuration.Factory;
public class DefaultDataSourceFactory<T> implements Factory<T> {
private T dataSource;
public DefaultDataSourceFactory(T dataSource) {
this.dataSource = dataSource;
}
public T create() {
return this.dataSource;
}
}
数据源pg
public static PGSimpleDataSource getPostgresqlDataSource(DataSource dataSource) {
PGSimpleDataSource realDs = null;
if (null != dataSource) {
realDs = new PGSimpleDataSource();
DataSource realDataSource = ((ItemDataSource)dataSource).getDataSource();
String userName = null;
String pwd = null;
String url = null;
if (realDataSource instanceof HikariDataSource) {
userName = ((HikariDataSource)realDataSource).getUsername();
pwd = ((HikariDataSource)realDataSource).getPassword();
url = ((HikariDataSource)realDataSource).getJdbcUrl();
}
realDs.setURL(url);
realDs.setUser(userName);
realDs.setPassword(pwd);
}
return realDs;
}
扩展dialect
import java.util.Collection; import org.apache.commons.lang3.StringUtils; import org.apache.ignite.cache.store.jdbc.dialect.BasicJdbcDialect; import org.apache.ignite.internal.util.typedef.C1; import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.internal.SB; public class PostgresqlDialect extends BasicJdbcDialect { private static final long serialVersionUID = 0L; private static final String WHERE = " where "; public PostgresqlDialect() { } public String escape(String ident) { return '"' + ident + '"'; } public String loadCacheSelectRangeQuery(String fullTblName, Collection<String> keyCols) { String cols = mkString(keyCols, ","); return String.format("SELECT %s FROM (SELECT %s, row_number() over(order by %s) AS rn FROM %s ) as temp WHERE mod(rn, ?) = 0", cols, cols, cols, fullTblName) } public boolean hasMerge() { return true; } public String mergeQuery(String fullTblName, Collection<String> keyCols, Collection<String> uniqCols) { Collection<String> cols = F.concat(false, keyCols, uniqCols); String primaryKeys = mkString(keyCols, ","); String updPart = mkString(uniqCols, new C1<String, String>() { public String apply(String col) { return String.format("%s = EXCLUDED.%s", col, col); } }, "", ", ", ""); return String.format("INSERT INTO %s (%s) VALUES (%s) ON conflict(%s) DO UPDATE SET %s", fullTblName, mkString(cols, ", "), repeat("?", cols.size(), "", ",", ""), primaryKeys, updPart); } }
本文来自博客园,作者:life_start,转载请注明原文链接:https://www.cnblogs.com/yangh2016/p/18348823