根据oracle的字段来建立ElasticSearch的Mapping
public class Start { private static Logger log = LoggerFactory.getLogger(Start.class); private static TransportClient client = null; static{ try { client = getClient(); } catch (UnknownHostException e) { e.printStackTrace(); } } public static void main(String[] args) throws MalformedURLException, ClassNotFoundException { DoMapping(); } private static void DoMapping() { String template = "{ " +" ""+Constant.INDEX+"":{ " +" "properties":{ " + " {#} " + " } " + " } " +"}"; String fieldString = GetOracleMapping(); template = template.replace("{#}",fieldString); client.admin().indices().prepareCreate(Constant.INDEX).addMapping(Constant.INDEX, template).get(); System.out.println(template); log.info("创建ElasticSearch Mapping完成!!!"); } private static TransportClient getClient() throws UnknownHostException { Settings settings = Settings.settingsBuilder().put("cluster.name", Constant.CLUSTER).build(); TransportClient client = TransportClient.builder().settings(settings).build(); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Constant.ESHOST), Constant.ESPORT)); return client; } public static String GetOracleMapping() { StringBuilder fieldstring = new StringBuilder(); Connection con = null; PreparedStatement pre = null; ResultSet result = null; try { Class.forName(Constant.DBDRIVER); log.info("开始尝试连接数据库!"); con = DriverManager.getConnection(Constant.DBURL, Constant.DBUSER, Constant.DBPASSWD); log.info("连接成功!"); String sql = "select * from "+Constant.DBTAB+" where rownum=?"; pre = con.prepareStatement(sql); pre.setInt(1, 1); result = pre.executeQuery(); ResultSetMetaData d = result.getMetaData(); int c = d.getColumnCount(); for (int i = 1; i <= c; i++) { fieldstring.append(" "" + d.getColumnName(i).toLowerCase() + "": { "); fieldstring.append(" "type": "" + GetElasticSearchMappingType(d.getColumnTypeName(i)) + " "); if (i == c) { fieldstring.append(" } "); } else { fieldstring.append(" }, "); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); log.info("数据库连接已关闭!"); } catch (Exception e) { e.printStackTrace(); } } return fieldstring.toString(); } private static String GetElasticSearchMappingType(String dbtype) { String es = "string"; switch (dbtype) { case "DATE": es = "date" "+" ,"format":"yyyy-MM-dd" "+" ,"null_value":"1900-01-01""; break; case "NUMBER": es = "double" "+" ,"null_value":0.00"; break; default: es = "string" "+" ,"null_value":"""; break; } return es; } }
其中读取配置文件的内容
db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@192.168.49.140:1521:DBDATA db.user=ms_orders db.passwd=ms1_orders db.table=t_order es.host=192.168.46.154 es.port=9300 cluster.name=dinpayEs es.index=t_orders