1212import com .google .common .collect .Maps ;
1313import org .apache .calcite .sql .JoinType ;
1414import org .apache .commons .collections .CollectionUtils ;
15+ import org .apache .commons .lang3 .StringUtils ;
1516import org .apache .http .HttpHost ;
1617import org .apache .http .auth .AuthScope ;
1718import org .apache .http .auth .UsernamePasswordCredentials ;
4041 * @author yinxi
4142 * @date 2020/1/13 - 1:00
4243 */
43- public class Elasticsearh6AllReqRow extends AllReqRow {
44+ public class Elasticsearch6AllReqRow extends AllReqRow {
45+
46+ private static final Logger LOG = LoggerFactory .getLogger (Elasticsearch6AllReqRow .class );
4447
45- private static final Logger LOG = LoggerFactory .getLogger (Elasticsearh6AllReqRow .class );
46- private static final int CONN_RETRY_NUM = 3 ;
4748 private AtomicReference <Map <String , List <Map <String , Object >>>> cacheRef = new AtomicReference <>();
4849
49- public Elasticsearh6AllReqRow (SideInfo sideInfo ) {
50+ public Elasticsearch6AllReqRow (SideInfo sideInfo ) {
5051 super (sideInfo );
5152 }
5253
@@ -155,44 +156,55 @@ private void loadData(Map<String, List<Map<String, Object>>> tmpCache) throws IO
155156 Elasticsearch6SideTableInfo tableInfo = (Elasticsearch6SideTableInfo ) sideInfo .getSideTableInfo ();
156157 RestHighLevelClient rhlClient = null ;
157158
158- try {
159- for (int i = 0 ; i < CONN_RETRY_NUM ; i ++) {
160- try {
161- rhlClient = getClient (tableInfo .getAddress (), tableInfo .isAuthMesh (), tableInfo .getUserName (), tableInfo .getPassword ());
162- break ;
163- } catch (Exception e ) {
164- if (i == CONN_RETRY_NUM - 1 ) {
165- throw new RuntimeException ("" , e );
166- }
167-
168- try {
169- String connInfo = "address: " + tableInfo .getAddress () + ";userName:" + tableInfo .getUserName () + ",pwd:" + tableInfo .getPassword ();
170- LOG .warn ("get conn fail, wait for 5 sec and try again, connInfo:" + connInfo );
171- Thread .sleep (5 * 1000 );
172- } catch (InterruptedException e1 ) {
173- LOG .error ("" , e1 );
174- }
175- }
176- }
159+ try {
160+ rhlClient = getClient (tableInfo .getAddress (), tableInfo .isAuthMesh (), tableInfo .getUserName (), tableInfo .getPassword (), tableInfo .getTimeout ());
177161
178162 // load data from tableA
179- SearchSourceBuilder searchSourceBuilder = Elasticsearch6AllSideInfo . searchSourceBuilder ;
163+ SearchSourceBuilder searchSourceBuilder = tableInfo . getSearchSourceBuilder () ;
180164 searchSourceBuilder .size (getFetchSize ());
181- SearchRequest searchRequest = new SearchRequest (tableInfo .getIndex ());
182- searchRequest .types (tableInfo .getEsType ());
165+ SearchRequest searchRequest = new SearchRequest ();
166+
167+ // determine existence of index
168+ String index = tableInfo .getIndex ().trim ();
169+ if (!StringUtils .isEmpty (index )){
170+ // strip leading and trailing spaces from a string
171+ String [] indexes = StringUtils .split (index , "," );
172+ for (int i =0 ; i < indexes .length ; i ++ ){
173+ indexes [i ] = indexes [i ].trim ();
174+ }
175+
176+ searchRequest .indices (indexes );
177+
178+ }
179+
180+ // determine existence of type
181+ String type = tableInfo .getEsType ().trim ();
182+ if (!StringUtils .isEmpty (type )){
183+ // strip leading and trailing spaces from a string
184+ String [] types = StringUtils .split (type , "," );
185+ for (int i =0 ; i < types .length ; i ++ ){
186+ types [i ] = types [i ].trim ();
187+ }
188+
189+ searchRequest .types (types );
190+ }
191+
192+ // add query condition
183193 searchRequest .source (searchSourceBuilder );
184194
195+ // get query reults
185196 SearchResponse searchResponse = rhlClient .search (searchRequest );
186197 SearchHit [] searchHits = searchResponse .getHits ().getHits ();
187- String [] sideFieldNames = sideInfo .getSideSelectFields ().split ("," );
188- String [] fields = sideInfo .getSideTableInfo ().getFieldTypes ();
198+
199+ String [] sideFieldNames = StringUtils .split (sideInfo .getSideSelectFields ().trim (), "," );
200+ String [] sideFieldTypes = sideInfo .getSideTableInfo ().getFieldTypes ();
189201
190202 Map <String , Object > oneRow = Maps .newHashMap ();
191203 for (SearchHit searchHit : searchHits ) {
192204 for (String fieldName : sideFieldNames ){
193205 Object object = searchHit .getSourceAsMap ().get (fieldName .trim ());
194206 int fieldIndex = sideInfo .getSideTableInfo ().getFieldList ().indexOf (fieldName .trim ());
195- object = SwitchUtil .getTarget (object , fields [fieldIndex ]);
207+ object = SwitchUtil .getTarget (object , sideFieldTypes [fieldIndex ]);
196208 oneRow .put (fieldName .trim (), object );
197209 }
198210
@@ -212,11 +224,11 @@ private void loadData(Map<String, List<Map<String, Object>>> tmpCache) throws IO
212224
213225 }
214226
215- public RestHighLevelClient getClient (String esAddress , Boolean isAuthMesh , String userName , String password ) {
227+ public RestHighLevelClient getClient (String esAddress , Boolean isAuthMesh , String userName , String password , Integer timeout ) {
216228 List <HttpHost > httpHostList = new ArrayList <>();
217- String [] address = esAddress .split ("," );
229+ String [] address = StringUtils .split (esAddress , "," );
218230 for (String addr : address ) {
219- String [] infoArray = addr .split (":" );
231+ String [] infoArray = StringUtils .split (addr , ":" );
220232 int port = 9200 ;
221233 String host = infoArray [0 ].trim ();
222234 if (infoArray .length > 1 ) {
@@ -226,6 +238,11 @@ public RestHighLevelClient getClient(String esAddress, Boolean isAuthMesh, Strin
226238 }
227239
228240 RestClientBuilder restClientBuilder = RestClient .builder (httpHostList .toArray (new HttpHost [httpHostList .size ()]));
241+
242+ if (timeout != null ) {
243+ restClientBuilder .setMaxRetryTimeoutMillis (timeout * 1000 );
244+ }
245+
229246 if (isAuthMesh ) {
230247 // 进行用户和密码认证
231248 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
@@ -236,6 +253,23 @@ public RestHighLevelClient getClient(String esAddress, Boolean isAuthMesh, Strin
236253
237254 RestHighLevelClient rhlClient = new RestHighLevelClient (restClientBuilder );
238255
256+ if (LOG .isInfoEnabled ()) {
257+ LOG .info ("Pinging Elasticsearch cluster via hosts {} ..." , httpHostList );
258+ }
259+
260+ try {
261+ if (!rhlClient .ping ()) {
262+ throw new RuntimeException ("There are no reachable Elasticsearch nodes!" );
263+ }
264+ } catch (IOException e ){
265+ LOG .warn ("" , e );
266+ }
267+
268+
269+ if (LOG .isInfoEnabled ()) {
270+ LOG .info ("Created Elasticsearch RestHighLevelClient connected to {}" , httpHostList .toString ());
271+ }
272+
239273 return rhlClient ;
240274
241275 }
0 commit comments