Skip to content

comet-ml/comet-java-sdk

Repository files navigation

https://www.comet.com official Java SDK

version Maven Central license javadoc

Branch Tests Coverage Linting Code Security
master CI codecov Lint Code Base CodeQL

Using Comet Java SDK

Add dependency to the pom.xml

    <dependencies>
        <dependency>
            <groupId>ml.comet</groupId>
            <artifactId>comet-java-client</artifactId>
            <version>1.1.14</version>
        </dependency>
    </dependencies>

Create experiment and log metrics and parameters

OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment()
                .withApiKey("someApiKey")
                .withProjectName("someProject")
                .withWorkspace("someWorkspace")
                .build();
    experiment.setExperimentName("My experiment");
    experiment.logParameter("batch_size", "500");
    experiment.logMetric("strMetric", 123);
    experiment.end();

The OnlineExperiment also can be used with try-with-resources statement which automatically handles call to the experiment.end().

try (OnlineExperiment experiment = ExperimentBuilder.OnlineExperiment()
                .withApiKey("someApiKey")
                .withProjectName("someProject")
                .withWorkspace("someWorkspace")
                .build()) {
    experiment.setExperimentName("My experiment");
    experiment.logParameter("batch_size", "500");
    experiment.logMetric("strMetric", 123);
} catch (Exception e) {
    e.printStackTrace();
}

Configure experiment object

Configuration sources hierarchy

The configuration parameters search order as following (first-listed are higher priority):

  • system properties or environment variables
  • configuration file set by call to withConfigOverride(java.io.File)
  • application.conf (all resources on the classpath with this name)
  • reference.conf (all resources on the classpath with this name)

Programmatic configuration

It is possible to override some or all configuration parameters programmatically when you create a new experiment's instance using ExperimentBuilder factory.

// Setting specific configuration parameters with builder
ExperimentBuilder.OnlineExperiment().withApiKey("someApiKey").build();

// Override configuration file (can have partial keys)
ExperimentBuilder.OnlineExperiment().withConfigOverride(new File("/tmp/comet.conf")).build();

// Read from environment variables OR from configuration file in classpath (application.conf)
ExperimentBuilder.OnlineExperiment().build();

Full list of environment variables

COMET_API_KEY
COMET_PROJECT_NAME
COMET_WORKSPACE_NAME
COMET_BASE_URL
COMET_MAX_AUTH_RETRIES

Examples