@SuppressWarnings("unchecked") static <T extends Destination> T fromPb(String destinationPb) { if (BucketDestination.matchesDestination(destinationPb)) { return (T) BucketDestination.fromPb(destinationPb); } else if (DatasetDestination.matchesDestination(destinationPb)) { return (T) DatasetDestination.fromPb(destinationPb); } else if (TopicDestination.matchesDestination(destinationPb)) { return (T) TopicDestination.fromPb(destinationPb); } throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } }
@Override SinkInfo parse(String... args) throws Exception { if (args.length >= 3) { if (args.length > 4) { throw new IllegalArgumentException("Too many arguments."); } String name = args[0]; Destination destination; switch (args[1]) { case "bucket": destination = BucketDestination.of(args[2]); break; case "dataset": destination = DatasetDestination.of(args[2]); break; case "topic": destination = TopicDestination.of(args[2]); break; default: throw new IllegalArgumentException("Second argument must be bucket|dataset|topic."); } SinkInfo.Builder builder = SinkInfo.newBuilder(name, destination); if (args.length == 4) { builder.setFilter(args[3]); } return builder.build(); } throw new IllegalArgumentException("Missing required sink name, destination or filter."); }
static DatasetDestination fromPb(String destinationPb) { Matcher matcher = PATTERN.matcher(destinationPb); if (!matcher.matches()) { throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } return new DatasetDestination(matcher.group(1), matcher.group(2)); } }
@Override public int hashCode() { return Objects.hash(baseHashCode(), project, dataset); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || !(obj instanceof DatasetDestination)) { return false; } DatasetDestination other = (DatasetDestination) obj; return baseEquals(other) && Objects.equals(project, other.project) && Objects.equals(dataset, other.dataset); }
/** * Creates a {@code DatasetDestination} object given the name of the dataset to be used as * sink destination. Dataset is assumed to reside in the default project. */ public static DatasetDestination of(String dataset) { return new DatasetDestination(null, dataset); }
/** * Creates a {@code DatasetDestination} object given the name of the project and dataset to be * used as sink destination. */ public static DatasetDestination of(String project, String dataset) { return new DatasetDestination(project, dataset); }
@SuppressWarnings("unchecked") static <T extends Destination> T fromPb(String destinationPb) { if (BucketDestination.matchesDestination(destinationPb)) { return (T) BucketDestination.fromPb(destinationPb); } else if (DatasetDestination.matchesDestination(destinationPb)) { return (T) DatasetDestination.fromPb(destinationPb); } else if (TopicDestination.matchesDestination(destinationPb)) { return (T) TopicDestination.fromPb(destinationPb); } throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } }
static DatasetDestination fromPb(String destinationPb) { Matcher matcher = PATTERN.matcher(destinationPb); if (!matcher.matches()) { throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } return new DatasetDestination(matcher.group(1), matcher.group(2)); } }
@Test public void testCreateGetUpdateAndDeleteSink() { String name = formatForTest("test-create-get-update-sink"); SinkInfo sinkInfo = SinkInfo.newBuilder(name, DatasetDestination.of("dataset")) .setFilter("severity>=ERROR") .setVersionFormat(SinkInfo.VersionFormat.V2) .build(); Sink sink = logging().create(sinkInfo); assertEquals(name, sink.getName()); assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat()); assertEquals("severity>=ERROR", sink.getFilter()); DatasetDestination datasetDestination = sink.getDestination(); assertEquals(logging().getOptions().getProjectId(), datasetDestination.getProject()); assertEquals("dataset", datasetDestination.getDataset()); assertEquals(sink, logging().getSink(name)); sink = sink.toBuilder().setFilter("severity<=ERROR").build().update(); assertEquals(name, sink.getName()); assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat()); assertEquals("severity<=ERROR", sink.getFilter()); assertTrue(sink.delete()); assertFalse(sink.delete()); }
@Test public void testToAndFromPbDestination() { BucketDestination bucketDestination = Destination.fromPb(BUCKET_DESTINATION.toPb("other")); assertEquals(Destination.Type.BUCKET, bucketDestination.getType()); assertEquals("bucket", bucketDestination.getBucket()); compareBucketDestination(BUCKET_DESTINATION, bucketDestination); DatasetDestination datasetDestination = Destination.fromPb(DATASET_DESTINATION.toPb("other")); assertEquals(Destination.Type.DATASET, datasetDestination.getType()); assertEquals("project", datasetDestination.getProject()); assertEquals("dataset", datasetDestination.getDataset()); compareDatasetDestination(DATASET_DESTINATION, datasetDestination); TopicDestination topicDestination = Destination.fromPb(TOPIC_DESTINATION.toPb("other")); assertEquals(Destination.Type.TOPIC, topicDestination.getType()); assertEquals("project", topicDestination.getProject()); assertEquals("topic", topicDestination.getTopic()); compareTopicDestination(TOPIC_DESTINATION, topicDestination); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("wrongDestination is not a valid sink destination"); Destination.fromPb("wrongDestination"); }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment try (Logging logging = LoggingOptions.getDefaultInstance().getService()) { // Create a sink to back log entries to a BigQuery dataset SinkInfo sinkInfo = SinkInfo.newBuilder("test-sink", DatasetDestination.of("test-dataset")) .setFilter("severity >= ERROR") .build(); logging.create(sinkInfo); // List sinks Page<Sink> sinks = logging.listSinks(); for (Sink sink : sinks.iterateAll()) { System.out.println(sink); } } } }
/** Example of asynchronously updating a sink. */ // [TARGET updateAsync(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink updateSinkAsync(String sinkName, String datasetName) throws ExecutionException, InterruptedException { // [START updateSinkAsync] SinkInfo sinkInfo = SinkInfo.newBuilder(sinkName, DatasetDestination.of(datasetName)) .setVersionFormat(SinkInfo.VersionFormat.V2) .setFilter("severity>=ERROR") .build(); Future<Sink> future = logging.updateAsync(sinkInfo); // ... Sink sink = future.get(); // [END updateSinkAsync] return sink; }
@Test public void testListSinks() throws InterruptedException { String firstName = formatForTest("test-list-sinks-1"); String secondName = formatForTest("test-list-sinks-2"); Sink firstSink = logging().create(SinkInfo.of(firstName, DatasetDestination.of("dataset"))); Sink secondSink = logging().create(SinkInfo.of(secondName, DatasetDestination.of("dataset"))); Logging.ListOption[] options = {Logging.ListOption.pageSize(1)}; Page<Sink> sinkPage = logging().listSinks(options); Set<Sink> sinks = Sets.newHashSet(sinkPage.iterateAll()); while (!sinks.contains(firstSink) || !sinks.contains(secondSink)) { Thread.sleep(500); sinks = Sets.newHashSet(logging().listSinks(options).iterateAll()); } firstSink.delete(); secondSink.delete(); }
/** * Example of asynchronously creating a sink to export logs to a BigQuery dataset (in the {@link * LoggingOptions#getProjectId()} project). */ // [TARGET createAsync(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink createSinkAsync(String sinkName, String datasetName) throws ExecutionException, InterruptedException { // [START createSinkAsync] SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName)); Future<Sink> future = logging.createAsync(sinkInfo); // ... Sink sink = future.get(); // [END createSinkAsync] return sink; }
/** Example of updating a sink. */ // [TARGET update(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink updateSink(String sinkName, String datasetName) { // [START logging_update_sink] SinkInfo sinkInfo = SinkInfo.newBuilder(sinkName, DatasetDestination.of(datasetName)) .setVersionFormat(SinkInfo.VersionFormat.V2) .setFilter("severity>=ERROR") .build(); Sink sink = logging.update(sinkInfo); // [END logging_update_sink] return sink; }
/** * Example of creating a sink to export logs to a BigQuery dataset (in the {@link * LoggingOptions#getProjectId()} project). */ // [TARGET create(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink createSink(String sinkName, String datasetName) { // [START logging_create_sink] SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName)); Sink sink = logging.create(sinkInfo); // [END logging_create_sink] return sink; }
@Test public void testUpdateNonExistingSink() { String name = formatForTest("test-update-non-existing-sink"); SinkInfo sinkInfo = SinkInfo.newBuilder(name, DatasetDestination.of("dataset")) .setFilter("severity>=ERROR") .setVersionFormat(SinkInfo.VersionFormat.V2) .build(); assertNull(logging().getSink(name)); thrown.expect(LoggingException.class); thrown.expectMessage("NOT_FOUND"); logging().update(sinkInfo); }
@Test public void testToAndFromPb_NoProjectId() { DatasetDestination datasetDestination = DatasetDestination.of("dataset"); SinkInfo sinkInfo = SinkInfo.of("name", DATASET_DESTINATION); compareSinkInfo( sinkInfo, SinkInfo.fromPb(SinkInfo.of("name", datasetDestination).toPb("project"))); TopicDestination topicDestination = TopicDestination.of("topic"); sinkInfo = SinkInfo.of("name", TOPIC_DESTINATION); compareSinkInfo( sinkInfo, SinkInfo.fromPb(SinkInfo.of("name", topicDestination).toPb("project"))); }
/** * Creates a {@code DatasetDestination} object given the name of the dataset to be used as * sink destination. Dataset is assumed to reside in the default project. */ public static DatasetDestination of(String dataset) { return new DatasetDestination(null, dataset); }