下面列出了com.mongodb.Function#com.mongodb.client.MongoIterable 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@GET
@Path("/collection/{collectionName}")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public JsonArray getCollection(@PathParam("collectionName") String collectionName) {
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
MongoIterable<Document> iterable = producerTemplate.requestBody(
"mongodb:camelMongoClient?database=test&collection=" + collectionName
+ "&operation=findAll&dynamicity=true&outputType=MongoIterable",
null, MongoIterable.class);
MongoCursor<Document> iterator = iterable.iterator();
while (iterator.hasNext()) {
Document document = iterator.next();
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
objectBuilder.add("message", (String) document.get("message"));
arrayBuilder.add(objectBuilder.build());
}
return arrayBuilder.build();
}
/**
* 获取DB名称列表
*
* @return
*/
public List<String> getDBNames() {
MongoIterable<String> dbs = connection.listDatabaseNames();
List<String> dbNames = new ArrayList<>();
dbs.forEach((Block<? super String>) dbNames::add);
return dbNames;
}
/**
* @decription 查询数据库表名
* @author yi.zhang
* @time 2017年6月30日 下午2:16:02
* @param table 表名
* @return
*/
public List<String> queryTables(){
try {
if(session==null){
init(servers, database, schema, username, password);
}
MongoIterable<String> collection = session.listCollectionNames();
if (collection == null) {
return null;
}
List<String> tables = new ArrayList<String>();
MongoCursor<String> cursor = collection.iterator();
while(cursor.hasNext()){
String table = cursor.next();
tables.add(table);
}
return tables;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
@Test
public void testVerifyDataAfterFeatureExecution() throws DbFeatureException {
// GIVEN
final MongoIterable<String> collectionIterable = mock(MongoIterable.class);
final MongoCursor<String> iterator = mock(MongoCursor.class);
when(expectedDataSets.strict()).thenReturn(Boolean.FALSE);
when(expectedDataSets.value()).thenReturn(new String[] {});
when(expectedDataSets.orderBy()).thenReturn(new String[] {});
when(expectedDataSets.excludeColumns()).thenReturn(new String[] {});
when(connection.listCollectionNames()).thenReturn(collectionIterable);
when(collectionIterable.iterator()).thenReturn(iterator);
when(iterator.hasNext()).thenReturn(Boolean.FALSE);
// WHEN
final DbFeature<MongoDatabase> feature = featureExecutor.createVerifyDataAfterFeature(expectedDataSets);
assertThat(feature, notNullValue());
feature.execute(connection);
// THEN
verify(connection).listCollectionNames();
verifyNoMoreInteractions(connection);
}
public boolean collectionExists(String collectionName) {
if (this.database == null) {
return false;
}
final MongoIterable<String> iterable = database.listCollectionNames();
try (final MongoCursor<String> it = iterable.iterator()) {
while (it.hasNext()) {
if (it.next().equalsIgnoreCase(collectionName)) {
return true;
}
}
}
return false;
}
public List<String> getIndexNames() {
globalLock.writeLock().lock();
try {
ArrayList<String> indexNames = new ArrayList<>();
log.info("Searching database <" + mongoConfig.getDatabaseName() + "> for indexes");
MongoDatabase db = mongo.getDatabase(mongoConfig.getDatabaseName());
MongoIterable<String> allCollections = db.listCollectionNames();
for (String collection : allCollections) {
if (collection.endsWith(LumongoIndex.CONFIG_SUFFIX)) {
String indexName = collection.substring(0, collection.length() - LumongoIndex.CONFIG_SUFFIX.length());
indexNames.add(indexName);
}
}
return indexNames;
}
finally {
globalLock.writeLock().unlock();
}
}
private void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps,
boolean includeMetaDataSearchLimit, boolean includeSortExpr )
{
if( includeMetaDataSearchLimit )
{
Integer searchLimit = getModel().getEffectiveMDSearchLimit( queryProps );
if( searchLimit > 0 )
{
// Apply to FindIterable or MapReduceIterable
if ( mongoIterable instanceof FindIterable )
{
FindIterable<Document> findIterable = (FindIterable<Document>) mongoIterable;
findIterable.limit( searchLimit.intValue( ) );
}
else if ( mongoIterable instanceof MapReduceIterable )
{
MapReduceIterable<Document> mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
mapReduceIterable.limit( searchLimit.intValue( ) );
}
}
}
applyPropertiesToCursor( mongoIterable, queryProps, includeSortExpr );
}
@Override
public TableDto runCommand(ProfilingReader profilingReader, MongoDbAccessor mongoDbAccessor) {
final TableDto result = new TableDto();
final MongoDatabase database = mongoDbAccessor.getMongoDatabase(profilingReader.getDatabase());
final MongoIterable<String> collNames = database.listCollectionNames();
if(collNames != null){
for(String collName : collNames){
if(!"system.profile".equals(collName)) {
final TableDto collStats = getIndexStats(database.getCollection(collName), profilingReader.getProfiledServerDto().getLabel());
result.addRows(collStats);
}
}
}
return result;
}
/**
* 数据库对应的数据集合列表
* @param dbName
* @return
*/
@ResponseBody
@RequestMapping("/db")
public Res db(String dbName) {
if(StringUtils.isEmpty(dbName)){
return Res.error("dbName参数不能为空");
}
if("undefined".equals(dbName)){
return Res.error("请关闭所有的iframe后在执行F5");
}
MongoDatabase mogo = mongoSdkBase.getMongoDb(dbName);
//获取所有集合的名称
MongoIterable<String> collectionNames = mogo.listCollectionNames();
MongoCursor<String> i = collectionNames.iterator();
List<JSONObject> listNames = new ArrayList<JSONObject>();
while (i.hasNext()) {
String tableName = i.next();
if(!Arrays.asList(TAVLEARR).contains(tableName)) {
JSONObject t = new JSONObject();
t.put("tableName", tableName);
BasicDBObject obj = mongoSdkBase.getStats(dbName, tableName);
t.put("size", ByteConvKbUtils.getPrintSize(obj.getInt("size")));
listNames.add(t);
}
}
return Res.ok().put("listNames", listNames);
}
/**
* 查询dbName下的所有表名
*
* @param dbName
* @return
*/
public List<String> getAllCollections(String dbName) {
MongoIterable<String> cols = getDatabase(dbName).listCollectionNames();
List<String> _list = new ArrayList<String>();
for (String s : cols) {
_list.add(s);
}
return _list;
}
private void retrieveOffsets() {
MongoIterable<Document> documentIterable = mongoHelper.getDocuments(OFFSETS_COLLECTION);
try (MongoCursor<Document> documents = documentIterable.iterator()) {
while (documents.hasNext()) {
Document doc = documents.next();
Document id = (Document) doc.get("_id");
String topic = id.getString("topic");
int partition = id.getInteger("partition");
long offset = doc.getLong("offset");
latestOffsets.put(new TopicPartition(topic, partition), offset);
}
}
}
public static <T extends IEntity> List<T> toEntities(Class<T> entity, MongoIterable<Document> iterable) throws Exception {
MongoCursor<Document> _documentIt = iterable.iterator();
List<T> _resultSet = new ArrayList<T>();
while (_documentIt.hasNext()) {
_resultSet.add(toEntity(entity, _documentIt.next()));
}
return _resultSet;
}
private <V> void fetch(MongoIterable<V> iterable, List<V> results) {
try (MongoCursor<V> cursor = iterable.iterator()) {
while (cursor.hasNext()) {
results.add(cursor.next());
}
}
}
private boolean isCollectionExists(String collectionName) {
boolean result = false;
MongoIterable<String> allCollections = db.listCollectionNames();
for ( String collection : allCollections ) {
if (collection.equals(collectionName)) {
result = true;
break;
}
}
return result;
}
public static List<MongoCollectionRef> getCollectionNames(boolean includeSystemPrefs) {
MongoIterable<String> names = Database.listCollectionNames();
ArrayList<MongoCollectionRef> list = new ArrayList<>();
for (String str : names)
if (includeSystemPrefs || !str.startsWith("system."))
list.add(new MongoCollectionRef(str));
return list;
}
private static Boolean existsDatabase( MongoClient mongoClient,
String dbName, Properties connProps )
throws OdaException
{
if ( dbName == null )
{
return false;
}
try
{
MongoIterable<String> databaseNameIterable = mongoClient
.listDatabaseNames( );
for ( String databaseName : databaseNameIterable )
{
if ( dbName.equals( databaseName ) )
{
return true;
}
}
return false;
}
catch ( MongoException ex )
{
MongoDBDriver.getLogger( ).log( Level.SEVERE,
"Unable to connect host",
ex ); // unable
// to
// get
// db
// names
// user may not have permission for listDatabaseName, return true,
// let the getDatabase() handle it.
throw new OdaException( ex );
}
}
private TableDto getIndexStats(MongoCollection<Document> collection, String dbsLabel){
final TableDto result = new TableDto();
final MongoIterable<Document> stats = collection
.aggregate(Arrays.asList(
new Document("$indexStats", new Document()),
new Document("$sort", new Document("accesses.ops", 1))
));
final HashMap<String, Document> indexesProperties = getIndexesProperties(collection);
for(Document doc : stats){
LOG.info("doc: {}", JSON.serialize(doc));
final ArrayList<Object> row = new ArrayList<Object>();
row.add(dbsLabel);
row.add(doc.getString("host"));
row.add(collection.getNamespace().getDatabaseName());
row.add(collection.getNamespace().getCollectionName());
final String indexName = doc.getString("name");
row.add(indexName);
row.add(((Document)doc.get("key")).toJson());
row.add(Boolean.toString(isTTL(indexesProperties, indexName)));
final Object accesses = doc.get("accesses");
if(accesses instanceof Document){
final Document accDoc = (Document) accesses;
row.add(accDoc.getLong("ops"));
final Date date = accDoc.getDate("since");
final LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
row.add(localDateTime.format(DATE_TIME_FORMATTER));
}else{
row.add(0L);
row.add("");
}
result.addRow(row);
}
return result;
}
private ArrayList<String> getCollectionNames(MongoDbAccessor mongoDbAccessor, String dbName){
final ArrayList<String> result = new ArrayList<String>();
final MongoIterable<String> collNames = mongoDbAccessor.getMongoDatabase(dbName).listCollectionNames();
if(collNames != null){
for(String collName : collNames){
result.add(collName);
}
}
return result;
}
private void find(String dbName, String collName, int limit){
final MongoIterable<Document> res = getMongoDatabase(dbName).getCollection(collName)
.find()
.limit(limit);
if(res != null){
for(Document doc : res){
LOG.info("doc: {}", JSON.serialize(doc));
}
}
}
@Override
public void onRegister(CompileScope scope) {
MemoryOperation.register(new BasicDBObjectMemoryOperation());
MemoryOperation.register(new DocumentMemoryOperation());
MemoryOperation.register(new IndexOptionsMemoryOperation());
MemoryOperation.register(new CountOptionsMemoryOperation());
MemoryOperation.register(new ObjectIdMemoryOperation());
registerWrapperClass(scope, ObjectId.class, WrapObjectId.class);
registerWrapperClass(scope, MongoIterable.class, WrapMongoIterable.class);
registerWrapperClass(scope, MongoCollection.class, WrapMongoCollection.class);
registerWrapperClass(scope, MongoDatabase.class, WrapMongoDatabase.class);
registerWrapperClass(scope, MongoClient.class, WrapMongoClient.class);
// register classes ...
}
@Test
public void testAccess() {
@SuppressWarnings("resource")
final MongoIterable<String> databaseNames = mongoDbTestRule.getMongoClient().listDatabaseNames();
Assert.assertNotNull(databaseNames);
Assert.assertNotNull(databaseNames.first());
}
public static <X> MongoIterable<X> iterate(Collection<X> result)
{
return new MongoIterable<X>()
{
@Override
public MongoCursor<X> iterator()
{
return new StubbingCursor<>(result.iterator());
}
@Override
public X first()
{
throw new UnsupportedOperationException();
}
@Override
public <U> MongoIterable<U> map(Function<X, U> function)
{
throw new UnsupportedOperationException();
}
@Override
public void forEach(Block<? super X> block)
{
throw new UnsupportedOperationException();
}
@Override
public <A extends Collection<? super X>> A into(A objects)
{
throw new UnsupportedOperationException();
}
@Override
public MongoIterable<X> batchSize(int i)
{
return this;
}
};
}
public QueryResultIterator(MongoIterable<T> mongoIterable) {
this.mongoCursor = mongoIterable.iterator();
}
/**
* Retrieves all documents in a collection.
*/
public MongoIterable<Document> getDocuments(String topic) {
return getCollection(topic).find();
}
/**
* Applies data set query properties and hints on DBCursor, except
* for cursor limit.
* @see #applyPropertiesToCursor(DBCursor,QueryProperties,boolean,boolean)
*/
static void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps,
boolean includeSortExpr )
{
BasicDBObject sortExprObj = null;
if( includeSortExpr ) // normally done only when executing a query to get full result set
{
try
{
sortExprObj = queryProps.getSortExprAsParsedObject();
}
catch( OdaException ex )
{
// log warning and ignore
DriverUtil.getLogger().log( Level.WARNING,
Messages.bind( "Unable to parse the user-defined Sort Expression: {0}", queryProps.getSortExpr() ), //$NON-NLS-1$
ex );
}
}
// Map it to correct iterable object
FindIterable<Document> findIterable = null;
AggregateIterable<Document> aggregateIterable = null;
MapReduceIterable<Document> mapReduceIterable = null;
if ( mongoIterable instanceof FindIterable )
{
findIterable = (FindIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof AggregateIterable )
{
aggregateIterable = (AggregateIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof MapReduceIterable )
{
mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
}
if ( findIterable == null
&& aggregateIterable == null && mapReduceIterable == null )
{
// Unknown type, return
}
if ( findIterable != null )
{
if ( sortExprObj != null )
findIterable.sort( sortExprObj );
if ( queryProps.getBatchSize( ) > 0 )
findIterable.batchSize( queryProps.getBatchSize( ) );
if ( queryProps.getNumDocsToSkip( ) > 0 )
findIterable.skip( queryProps.getNumDocsToSkip( ) );
if ( queryProps.isPartialResultsOk( ) )
findIterable.partial( true );
// TODO: Remove hint from the UI
// TODO: add Time out in the UI
/*
* // hint is deprecated in 3.2 DBObject hintObj =
* queryProps.getIndexHintsAsParsedObject(); String hintValue =
* queryProps.getIndexHints(); if( hintObj != null )
* rowsCursor.hint( hintObj ); else // try to pass the hint string
* value as is { String hintValue = queryProps.getIndexHints(); if(
* ! hintValue.isEmpty() ) rowsCursor.hint( hintValue ); }
* findIterable.maxTime(Bytes.QUERYOPTION_NOTIMEOUT, arg1) if(
* queryProps.hasNoTimeOut() ) rowsCursor.addOption(
* Bytes.QUERYOPTION_NOTIMEOUT );
*/
}
if ( aggregateIterable != null )
{
if ( queryProps.getBatchSize( ) > 0 )
aggregateIterable.batchSize( queryProps.getBatchSize( ) );
}
if ( mapReduceIterable != null )
{
if ( sortExprObj != null )
mapReduceIterable.sort( sortExprObj );
if ( queryProps.getBatchSize( ) > 0 )
mapReduceIterable.batchSize( queryProps.getBatchSize( ) );
}
}
public WrapMongoIterable(Environment env, MongoIterable wrappedObject) {
super(env, wrappedObject);
}
@Signature
public WrapMongoIterable batchSize(Environment env, int size) {
MongoIterable<Document> documents = getWrappedObject().batchSize(size);
return new WrapMongoIterable(env, documents);
}
@Test
public void testAccess() {
final MongoIterable<String> databaseNames = mongoDbTestRule.getMongoClient().listDatabaseNames();
Assert.assertNotNull(databaseNames);
Assert.assertNotNull(databaseNames.first());
}
/**
* 获取所有数据库名称列表
*
* @return
*/
public MongoIterable<String> getAllDatabaseName() {
MongoIterable<String> s = mongoClient.listDatabaseNames();
return s;
}