如何通过编程方式读取NiFi Counter的值(非REST API方式)
Great question! When building a custom NiFi processor, you don’t have to rely on the external REST API to access counter values. NiFi provides internal Java APIs that let you fetch counters directly within your processor, which is more efficient (no network overhead) and integrated tightly with the runtime environment. Here are the most practical approaches:
1. Use ProcessSession to Fetch Counters Directly
This is the simplest and most recommended method, since ProcessSession is already passed to your processor’s onTrigger method. You can retrieve counters by name (or name + group) and get their current value immediately:
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { // Fetch a counter by its name Counter standaloneCounter = session.getCounter("My_Standalone_Counter"); long standaloneValue = standaloneCounter.getValue(); // Fetch a grouped counter (common for component-specific counters) Counter groupedCounter = session.getCounter("My_Processor_Group", "Records_Processed"); long groupedValue = groupedCounter.getValue(); // Use the values in your processor logic (e.g., log, route flow files, etc.) getLogger().info("Standalone counter value: {}", new Object[]{standaloneValue}); getLogger().info("Grouped counter value: {}", new Object[]{groupedValue}); }
This API is stable across NiFi versions (1.x and 2.x) and avoids any dependency on external services or network calls.
2. Access NiFi's Metrics Registry
If your counter is registered with NiFi's built-in metrics system (many core components auto-register counters here), you can fetch it via the MetricsRegistry:
import org.apache.nifi.metrics.MetricsRegistry; import com.codahale.metrics.Counter; @Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { // Retrieve the MetricsRegistry service MetricsRegistry metricsRegistry = context.getControllerServiceLookup().getControllerService(MetricsRegistry.class); // Fetch the counter by its metric name (follows pattern: nifi.controller.counters.<group>.<name>) Counter metricsCounter = metricsRegistry.counter("nifi.controller.counters.My_Processor_Group.Records_Processed"); long counterValue = metricsCounter.getCount(); getLogger().info("Metrics counter value: {}", new Object[]{counterValue}); }
Note: Metric names for counters follow a consistent pattern, so adjust the string to match your counter's group and name.
3. Directly Access CounterRepository (Not Recommended)
NiFi persists counter data in a CounterRepository, which you can access via the controller service lookup. However, this is an internal implementation detail—its API may change between NiFi versions, so use this only if the above methods don’t fit your use case:
import org.apache.nifi.controller.repository.CounterRepository; import java.util.Optional; @Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { CounterRepository counterRepo = context.getControllerServiceLookup().getControllerService(CounterRepository.class); // Fetch counter (API may vary by NiFi version) Optional<org.apache.nifi.controller.Counter> counter = counterRepo.getCounter("My_Counter_Name"); if (counter.isPresent()) { long value = counter.get().getValue(); getLogger().info("Counter repository value: {}", new Object[]{value}); } }
Key Advantages Over REST API
- Performance: No network calls, so faster and more reliable, especially in large clusters.
- Security: Avoids dealing with REST API authentication/authorization within your processor.
- Integration: Tightly coupled with NiFi’s runtime, so you get real-time, node-local counter values (critical in clustered environments).
内容的提问来源于stack exchange,提问作者Sourav Gulati

