java 连接操作Elasticsearch(一)
- 导入Elasticsearch依赖包
<!-- Elasticsearch6.4.3 依赖 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport-netty4-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency>
- properties配置文件
elasticsearch.ip=es库ip elasticsearch.port=9300(es对外连接端口) elasticsearch.cluster.name=elasticsearch elasticsearch.shards=3 elasticsearch.replicas=1 elasticsearch.pool=3 elasticsearch.regenerateIndexEnabled=false elasticsearch.syncDataEnabled=true elasticsearch.sniff=true
- 获取连接
public static Connection getConn(){ Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties"); // 使用properties对象加载输入流 try { properties.load(in); } catch (IOException e1) { e1.printStackTrace(); } String url = properties.getProperty("postgresql.url"); String driver = properties.getProperty("postgresql.driver"); String user = properties.getProperty("postgresql.user"); String password = properties.getProperty("postgresql.password"); try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); //获取连接 } catch (ClassNotFoundException e){ e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }
- 获取单例客户端
public static TransportClient getSingleClient() { Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties"); // 使用properties对象加载输入流 try { properties.load(in); } catch (IOException e1) { e1.printStackTrace(); } String host = properties.getProperty("elasticsearch.ip"); String cluster_name = properties.getProperty("elasticsearch.cluster.name"); int port = Integer.parseInt(properties.getProperty("elasticsearch.port")); /* * int shards = * Integer.parseInt(properties.getProperty("elasticsearch.shards")); int * replicas = * Integer.parseInt(properties.getProperty("elasticsearch.replicas")); * String sniff = properties.getProperty("elasticsearch.sniff"); */ if (client == null) { synchronized (TransportClient.class) { } if (client == null) { try { /** * 配置信息 client.transport.sniff 增加嗅探机制,找到ES集群 * thread_pool.search.size 增加线程池个数,暂时设为3 */ Settings esSetting = Settings.builder().put("cluster.name", cluster_name).build(); // 配置信息Settings自定义 client = new PreBuiltTransportClient(esSetting); TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(host), port); client.addTransportAddress(transportAddress); } catch (Exception e) { LOGGER.error("elasticsearch TransportClient create error!!!", e); } } } return client; }
- 获取es库客户端
@SuppressWarnings("resource") public static ElasticsearchClient getEsClient(){ Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties"); // 使用properties对象加载输入流 try { properties.load(in); } catch (IOException e1) { e1.printStackTrace(); } String host = properties.getProperty("elasticsearch.ip"); int port = Integer.parseInt(properties.getProperty("elasticsearch.port")); try { es_client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port)); } catch (Exception e) { e.printStackTrace(); } return es_client; }
小白技术分享