• 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
  • 相关阅读:
    53个Python面试问题
    ycsb对hbase性能测试的研究
    zookeeper的安装(图文详解。。。来点击哦!)
    Zookeeper概论(对zookeeper的概论、原理、架构等的理解)
    Hadoop集群搭建安装过程(三)(图文详解---尽情点击!!!)
    Hadoop集群搭建安装过程(二)(图文详解---尽情点击!!!)
    Hadoop集群搭建安装过程(一)(图文详解---尽情点击!!!)
    Linux虚拟机安装(CentOS 6.5,图文详解,需要自查)
    Hive中的排序和分组(对map和reduce的影响,值得一看!)
    HDFS的工作原理(读和写操作)
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6610956.html
Copyright © 2020-2023  润新知