2020-06-28 18:20:38 +08:00
|
|
|
|
package cn.ac.iie.utils;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.ac.iie.config.ApplicationConfig;
|
|
|
|
|
|
import com.arangodb.ArangoCollection;
|
|
|
|
|
|
import com.arangodb.ArangoCursor;
|
|
|
|
|
|
import com.arangodb.ArangoDB;
|
|
|
|
|
|
import com.arangodb.ArangoDatabase;
|
|
|
|
|
|
import com.arangodb.entity.DocumentCreateEntity;
|
|
|
|
|
|
import com.arangodb.entity.ErrorEntity;
|
|
|
|
|
|
import com.arangodb.entity.MultiDocumentEntity;
|
|
|
|
|
|
import com.arangodb.model.AqlQueryOptions;
|
|
|
|
|
|
import com.arangodb.model.DocumentCreateOptions;
|
|
|
|
|
|
import com.arangodb.util.MapBuilder;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.Collection;
|
2020-08-10 18:37:53 +08:00
|
|
|
|
import java.util.List;
|
2020-06-28 18:20:38 +08:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
public class ArangoDBConnect {
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ArangoDBConnect.class);
|
|
|
|
|
|
private static ArangoDB arangoDB = null;
|
|
|
|
|
|
private static ArangoDBConnect conn = null;
|
|
|
|
|
|
static {
|
|
|
|
|
|
getArangoDB();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void getArangoDB(){
|
|
|
|
|
|
arangoDB = new ArangoDB.Builder()
|
|
|
|
|
|
.maxConnections(ApplicationConfig.THREAD_POOL_NUMBER)
|
|
|
|
|
|
.host(ApplicationConfig.ARANGODB_HOST, ApplicationConfig.ARANGODB_PORT)
|
|
|
|
|
|
.user(ApplicationConfig.ARANGODB_USER)
|
|
|
|
|
|
.password(ApplicationConfig.ARANGODB_PASSWORD)
|
|
|
|
|
|
.build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static synchronized ArangoDBConnect getInstance(){
|
|
|
|
|
|
if (null == conn){
|
|
|
|
|
|
conn = new ArangoDBConnect();
|
|
|
|
|
|
}
|
|
|
|
|
|
return conn;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-29 19:06:23 +08:00
|
|
|
|
private ArangoDatabase getDatabase(){
|
2020-06-28 18:20:38 +08:00
|
|
|
|
return arangoDB.db(ApplicationConfig.ARANGODB_DB_NAME);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-09 17:41:43 +08:00
|
|
|
|
public void clean(){
|
2020-06-28 18:20:38 +08:00
|
|
|
|
try {
|
|
|
|
|
|
if (arangoDB != null){
|
|
|
|
|
|
arangoDB.shutdown();
|
|
|
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
LOG.error(e.getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public <T> ArangoCursor<T> executorQuery(String query,Class<T> type){
|
|
|
|
|
|
ArangoDatabase database = getDatabase();
|
|
|
|
|
|
Map<String, Object> bindVars = new MapBuilder().get();
|
2020-06-29 19:06:23 +08:00
|
|
|
|
AqlQueryOptions options = new AqlQueryOptions()
|
|
|
|
|
|
.ttl(ApplicationConfig.ARANGODB_TTL);
|
2020-06-28 18:20:38 +08:00
|
|
|
|
try {
|
|
|
|
|
|
return database.query(query, bindVars, options, type);
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
LOG.error(e.getMessage());
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}finally {
|
|
|
|
|
|
bindVars.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-10 18:37:53 +08:00
|
|
|
|
public <T> void overwrite(List<T> docOverwrite, String collectionName){
|
2020-06-28 18:20:38 +08:00
|
|
|
|
ArangoDatabase database = getDatabase();
|
|
|
|
|
|
try {
|
|
|
|
|
|
ArangoCollection collection = database.collection(collectionName);
|
|
|
|
|
|
if (!docOverwrite.isEmpty()){
|
|
|
|
|
|
DocumentCreateOptions documentCreateOptions = new DocumentCreateOptions();
|
|
|
|
|
|
documentCreateOptions.overwrite(true);
|
2020-06-29 19:06:23 +08:00
|
|
|
|
documentCreateOptions.silent(true);
|
2020-06-28 18:20:38 +08:00
|
|
|
|
MultiDocumentEntity<DocumentCreateEntity<T>> documentCreateEntityMultiDocumentEntity = collection.insertDocuments(docOverwrite, documentCreateOptions);
|
|
|
|
|
|
Collection<ErrorEntity> errors = documentCreateEntityMultiDocumentEntity.getErrors();
|
|
|
|
|
|
for (ErrorEntity errorEntity:errors){
|
|
|
|
|
|
LOG.error("写入arangoDB异常:"+errorEntity.getErrorMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
2020-06-29 19:06:23 +08:00
|
|
|
|
LOG.error("更新失败:"+e.toString());
|
2020-06-28 18:20:38 +08:00
|
|
|
|
}finally {
|
|
|
|
|
|
docOverwrite.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|