• ElastcSearch的Mapping映射建立


    根据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
  • 相关阅读:
    android之字符串的一些转码
    android之界面一些操作
    日期的一些处理
    android数据的4种存储方式
    第四周学习情况
    第四周作业2
    第四周作业
    第三章学习情况
    第二章学习情况(2020.02.24-2020.03.01)
    个人情况介绍+《人月神话》读后感+本周学习情况(2020.02.17-2020.02.23)
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6610956.html
Copyright © 2020-2023  润新知