How can I find the number of components in each repository instance(for example,maven- release,npm-hosted)?

How can I find the number of components in each repository instance(for example,maven- release,npm-hosted ), and how can I get a list of components for a repository instance?

You can use the Repositories API call to get the list of repositories, and then use the Components API to count the components. The Components List API Call includes the repository format (i.e. maven2, npm, etc.)

Repositories API: Repositories API
Components API: Components API

It’s Ok, but the default page size is 10, if I count the components by page,It’s too slow to count all the components in each repository. Is there a better way?

There is another Component API option that will allow you to set the page size: Repository Results View REST API

You could use a PowerShell module I wrote called NexuShell for this:

Connect-NexusServer -Hostname -Credential (Get-Credential) -UseSsl
Get-NexusComponent -Repository npm-hosted

You can manage quite a lot of Sonatype Nexus using NexuShell. Check out for more information about it!

Sample output:

This API has a parameter named repositoryId, how do I get it?

As it turns out, there is no automated way to get the Repository ID. Here is another way to achieve your goal:

To get the number of components & assets in the repository you can use the Groovy script provided in the Sonatype support article.

First, here is how you enable scripting:

Then, this script can be put into an Execute Script scheduled task and it will print component and asset counts.

Here is the script:

import groovy.json.JsonOutputdef result = [:]repository.repositoryManager.browse().each { Repository repo ->    try {    def tx = repo.facet(StorageFacet).txSupplier().get()    tx.begin()    def components = tx.countComponents(Query.builder().where('1').eq(1).build(), [repo])    def assets = tx.countAssets(Query.builder().where('1').eq(1).build(), [repo])    tx.commit()    result[] = [components: components, assets: assets]    } catch (Exception e) {      log.warn('Yikes: {}', e.toString())    } finally {      tx.close()    }  }def json = JsonOutput.toJson(result) jsonreturn json

After this script runs, it will print a single log message in nexus.log that shows counts per repository - example:

2017-06-06 09:42:19,486-0300 INFO  [quartz-4-thread-5] *SYSTEM - {"":{"components":0,"assets":0},"maven-releases":{"components":0,"assets":0},"releases":{"components":0,"assets":0},"snapshots":{"components":0,"assets":0},"nuget-group":{"components":0,"assets":0},"maven-snapshots":{"components":0,"assets":0},"maven-central":{"components":0,"assets":2},"docker-hosted":{"components":0,"assets":0},"nuget-hosted":{"components":0,"assets":0},"maven-public":{"components":0,"assets":0},"raw-hosted":{"components":1,"assets":1}}

Please note: This script may have performance issues with large blobstores and is not applicable for S3 or PostgreSQL; it will only work on file-based blob stores and OrientDB.