2929import com .dtstack .flink .sql .side .mongo .utils .MongoUtil ;
3030import com .mongodb .BasicDBObject ;
3131import com .mongodb .Block ;
32+ import com .mongodb .ConnectionString ;
3233import com .mongodb .MongoCredential ;
3334import com .mongodb .ServerAddress ;
3435import com .mongodb .async .SingleResultCallback ;
3536import com .mongodb .async .client .MongoClient ;
36- import com .mongodb .async . client . MongoClientSettings ;
37+ import com .mongodb .MongoClientSettings ;
3738import com .mongodb .async .client .MongoClients ;
3839import com .mongodb .async .client .MongoCollection ;
3940import com .mongodb .async .client .MongoDatabase ;
@@ -70,13 +71,11 @@ public class MongoAsyncReqRow extends AsyncReqRow {
7071
7172 private static final Logger LOG = LoggerFactory .getLogger (MongoAsyncReqRow .class );
7273
73- private final static int DEFAULT_MAX_DB_CONN_POOL_SIZE = 20 ;
74-
7574 private transient MongoClient mongoClient ;
7675
7776 private MongoDatabase db ;
7877
79- private MongoSideTableInfo MongoSideTableInfo ;
78+ private MongoSideTableInfo mongoSideTableInfo ;
8079
8180 public MongoAsyncReqRow (RowTypeInfo rowTypeInfo , JoinInfo joinInfo , List <FieldInfo > outFieldInfoList , SideTableInfo sideTableInfo ) {
8281 super (new MongoAsyncSideInfo (rowTypeInfo , joinInfo , outFieldInfoList , sideTableInfo ));
@@ -85,71 +84,48 @@ public MongoAsyncReqRow(RowTypeInfo rowTypeInfo, JoinInfo joinInfo, List<FieldIn
8584 @ Override
8685 public void open (Configuration parameters ) throws Exception {
8786 super .open (parameters );
88- MongoSideTableInfo = (MongoSideTableInfo ) sideInfo .getSideTableInfo ();
89- connMongoDB ();
87+ mongoSideTableInfo = (MongoSideTableInfo ) sideInfo .getSideTableInfo ();
88+ connMongoDb ();
9089 }
9190
92- public void connMongoDB () throws Exception {
93- MongoCredential mongoCredential ;
94- String [] servers = MongoSideTableInfo .getAddress ().split ("," );
95- String host ;
96- Integer port ;
97- String [] hostAndPort ;
98- List <ServerAddress > lists = new ArrayList <>();
99- for (String server : servers ) {
100- hostAndPort = server .split (":" );
101- host = hostAndPort [0 ];
102- port = Integer .parseInt (hostAndPort [1 ]);
103- lists .add (new ServerAddress (host , port ));
104- }
105- ClusterSettings clusterSettings = ClusterSettings .builder ().hosts (lists ).build ();
106- ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings .builder ()
107- .maxSize (DEFAULT_MAX_DB_CONN_POOL_SIZE )
91+ public void connMongoDb () throws Exception {
92+ String address = mongoSideTableInfo .getAddress ();
93+ ConnectionString connectionString = new ConnectionString (address );
94+
95+ MongoClientSettings settings = MongoClientSettings .builder ()
96+ .applyConnectionString (connectionString )
10897 .build ();
109- if (!StringUtils .isEmpty (MongoSideTableInfo .getUserName ()) || !StringUtils .isEmpty (MongoSideTableInfo .getPassword ())) {
110- mongoCredential = MongoCredential .createCredential (MongoSideTableInfo .getUserName (), MongoSideTableInfo .getDatabase (),
111- MongoSideTableInfo .getPassword ().toCharArray ());
112- MongoClientSettings settings = MongoClientSettings .builder ().credential (mongoCredential )
113- .clusterSettings (clusterSettings )
114- .connectionPoolSettings (connectionPoolSettings )
115- .build ();
116- mongoClient = MongoClients .create (settings );
117- } else {
118- MongoClientSettings settings = MongoClientSettings .builder ().clusterSettings (clusterSettings )
119- .connectionPoolSettings (connectionPoolSettings )
120- .build ();
121- mongoClient = MongoClients .create (settings );
122- }
123- db = mongoClient .getDatabase (MongoSideTableInfo .getDatabase ());
98+ mongoClient = MongoClients .create (settings );
99+ db = mongoClient .getDatabase (mongoSideTableInfo .getDatabase ());
124100 }
125101
126102 @ Override
127103 public void asyncInvoke (CRow input , ResultFuture <CRow > resultFuture ) throws Exception {
128104 CRow inputCopy = new CRow (input .row (), input .change ());
129- BasicDBObject basicDBObject = new BasicDBObject ();
105+ BasicDBObject basicDbObject = new BasicDBObject ();
130106 for (int i = 0 ; i < sideInfo .getEqualFieldList ().size (); i ++) {
131107 Integer conValIndex = sideInfo .getEqualValIndex ().get (i );
132108 Object equalObj = inputCopy .row ().getField (conValIndex );
133109 if (equalObj == null ) {
134110 dealMissKey (inputCopy , resultFuture );
135111 return ;
136112 }
137- basicDBObject .put (sideInfo .getEqualFieldList ().get (i ), equalObj );
113+ basicDbObject .put (sideInfo .getEqualFieldList ().get (i ), equalObj );
138114 }
139115 try {
140116 // 填充谓词
141117 sideInfo .getSideTableInfo ().getPredicateInfoes ().stream ().map (info -> {
142118 BasicDBObject filterCondition = MongoUtil .buildFilterObject (info );
143119 if (null != filterCondition ) {
144- basicDBObject .append (info .getFieldName (), filterCondition );
120+ basicDbObject .append (info .getFieldName (), filterCondition );
145121 }
146122 return info ;
147123 }).count ();
148124 } catch (Exception e ) {
149125 LOG .info ("add predicate infoes error " , e );
150126 }
151127
152- String key = buildCacheKey (basicDBObject .values ());
128+ String key = buildCacheKey (basicDbObject .values ());
153129 if (openCache ()) {
154130 CacheObj val = getFromCache (key );
155131 if (val != null ) {
@@ -171,7 +147,7 @@ public void asyncInvoke(CRow input, ResultFuture<CRow> resultFuture) throws Exce
171147 }
172148 }
173149 AtomicInteger atomicInteger = new AtomicInteger (0 );
174- MongoCollection dbCollection = db .getCollection (MongoSideTableInfo .getTableName (), Document .class );
150+ MongoCollection dbCollection = db .getCollection (mongoSideTableInfo .getTableName (), Document .class );
175151 List <Document > cacheContent = Lists .newArrayList ();
176152 Block <Document > printDocumentBlock = new Block <Document >() {
177153 @ Override
@@ -197,7 +173,7 @@ public void onResult(final Void result, final Throwable t) {
197173 }
198174 }
199175 };
200- dbCollection .find (basicDBObject ).forEach (printDocumentBlock , callbackWhenFinished );
176+ dbCollection .find (basicDbObject ).forEach (printDocumentBlock , callbackWhenFinished );
201177 }
202178
203179 @ Override
0 commit comments