-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NIFI-10953: Implement GCP Vision AI processors #6762
Closed
KalmanJantner
wants to merge
13
commits into
apache:main
from
KalmanJantner:NIFI-10953-Implement-GCP-Vision-AI-processors
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b230f2c
NIFI-10953: Implement GCP Vision AI processors
KalmanJantner b4c6b5f
NIFI-10953: apply review comments and add more details to additionDet…
KalmanJantner cf0b261
NIFI-10953: add more description details and default value for json p…
KalmanJantner 13d5c7a
NIFI-10953: introduce operation key property, update default json pay…
KalmanJantner edeea96
Update nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/…
KalmanJantner 5319cdf
Update nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/…
KalmanJantner 46ef59e
Update nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/…
KalmanJantner 1148fed
Update nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/…
KalmanJantner bd6ff01
Update nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/…
KalmanJantner 9ad69d5
Update nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/…
KalmanJantner 7c9886b
Update nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/…
KalmanJantner 8a44b4e
NIFI-10953: fix additionalDetails.html title
KalmanJantner 6b45ed7
NIFI-10953: fix additionalDetails.html
KalmanJantner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ssors/src/main/java/org/apache/nifi/processors/gcp/vision/AbstractGcpVisionProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.nifi.processors.gcp.vision; | ||
|
||
import static org.apache.nifi.processors.gcp.util.GoogleUtils.GCP_CREDENTIALS_PROVIDER_SERVICE; | ||
|
||
import com.google.api.gax.core.FixedCredentialsProvider; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.vision.v1.ImageAnnotatorClient; | ||
import com.google.cloud.vision.v1.ImageAnnotatorSettings; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.nifi.annotation.lifecycle.OnScheduled; | ||
import org.apache.nifi.components.PropertyDescriptor; | ||
import org.apache.nifi.gcp.credentials.service.GCPCredentialsService; | ||
import org.apache.nifi.processor.AbstractProcessor; | ||
import org.apache.nifi.processor.ProcessContext; | ||
import org.apache.nifi.processor.Relationship; | ||
import org.apache.nifi.processor.exception.ProcessException; | ||
|
||
public abstract class AbstractGcpVisionProcessor extends AbstractProcessor { | ||
public static final String GCP_OPERATION_KEY = "operationKey"; | ||
|
||
public static final Relationship REL_SUCCESS = new Relationship.Builder().name("success") | ||
.description("FlowFiles are routed to success relationship").build(); | ||
public static final Relationship REL_FAILURE = new Relationship.Builder().name("failure") | ||
.description("FlowFiles are routed to failure relationship").build(); | ||
|
||
protected static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( | ||
REL_SUCCESS, | ||
REL_FAILURE | ||
))); | ||
protected static final List<PropertyDescriptor> properties = Collections.unmodifiableList(Arrays.asList( | ||
GCP_CREDENTIALS_PROVIDER_SERVICE) | ||
); | ||
|
||
private ImageAnnotatorClient vision; | ||
|
||
@Override | ||
public List<PropertyDescriptor> getSupportedPropertyDescriptors() { | ||
return properties; | ||
} | ||
|
||
@Override | ||
public Set<Relationship> getRelationships() { | ||
return relationships; | ||
} | ||
|
||
@OnScheduled | ||
public void onScheduled(ProcessContext context) { | ||
final GCPCredentialsService gcpCredentialsService = | ||
context.getProperty(GCP_CREDENTIALS_PROVIDER_SERVICE).asControllerService(GCPCredentialsService.class); | ||
try { | ||
GoogleCredentials credentials = gcpCredentialsService.getGoogleCredentials(); | ||
FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials); | ||
ImageAnnotatorSettings.Builder builder = ImageAnnotatorSettings.newBuilder().setCredentialsProvider(credentialsProvider); | ||
vision = ImageAnnotatorClient.create(builder.build()); | ||
} catch (Exception e) { | ||
getLogger().error("Failed to create vision client.", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The framework should know that the processor is incapable of doing it's job when this happens. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx, fixed. |
||
throw new ProcessException("Failed to create vision client.", e); | ||
} | ||
} | ||
|
||
protected ImageAnnotatorClient getVisionClient() { | ||
return this.vision; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...va/org/apache/nifi/processors/gcp/vision/AbstractGetGcpVisionAnnotateOperationStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.nifi.processors.gcp.vision; | ||
|
||
import static org.apache.nifi.expression.ExpressionLanguageScope.FLOWFILE_ATTRIBUTES; | ||
|
||
import com.google.longrunning.Operation; | ||
import com.google.protobuf.ByteString; | ||
import com.google.protobuf.GeneratedMessageV3; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import com.google.protobuf.util.JsonFormat; | ||
import com.google.rpc.Status; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.apache.nifi.components.PropertyDescriptor; | ||
import org.apache.nifi.flowfile.FlowFile; | ||
import org.apache.nifi.flowfile.attributes.CoreAttributes; | ||
import org.apache.nifi.processor.ProcessContext; | ||
import org.apache.nifi.processor.ProcessSession; | ||
import org.apache.nifi.processor.Relationship; | ||
import org.apache.nifi.processor.exception.ProcessException; | ||
import org.apache.nifi.processor.util.StandardValidators; | ||
|
||
abstract public class AbstractGetGcpVisionAnnotateOperationStatus extends AbstractGcpVisionProcessor { | ||
public static final PropertyDescriptor OPERATION_KEY = new PropertyDescriptor.Builder() | ||
.name("operationKey") | ||
.displayName("GCP Operation Key") | ||
.description("The unique identifier of the Vision operation.") | ||
.defaultValue("${operationKey}") | ||
.required(true) | ||
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) | ||
.expressionLanguageSupported(FLOWFILE_ATTRIBUTES) | ||
.build(); | ||
public static final Relationship REL_RUNNING = new Relationship.Builder() | ||
.name("running") | ||
.description("The job is currently still being processed") | ||
.build(); | ||
public static final Relationship REL_ORIGINAL = new Relationship.Builder() | ||
.name("original") | ||
.description("Upon successful completion, the original FlowFile will be routed to this relationship.") | ||
.autoTerminateDefault(true) | ||
.build(); | ||
private static final List<PropertyDescriptor> PROPERTIES = | ||
Collections.unmodifiableList(Stream.concat(properties.stream(), Stream.of(OPERATION_KEY)).collect(Collectors.toList())); | ||
private static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( | ||
REL_ORIGINAL, | ||
REL_SUCCESS, | ||
REL_FAILURE, | ||
REL_RUNNING | ||
))); | ||
|
||
@Override | ||
public List<PropertyDescriptor> getSupportedPropertyDescriptors() { | ||
return PROPERTIES; | ||
} | ||
|
||
@Override | ||
public Set<Relationship> getRelationships() { | ||
return relationships; | ||
} | ||
|
||
@Override | ||
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { | ||
FlowFile flowFile = session.get(); | ||
if (flowFile == null) { | ||
return; | ||
} | ||
try { | ||
String operationKey = context.getProperty(OPERATION_KEY).evaluateAttributeExpressions(flowFile).getValue();; | ||
Operation operation = getVisionClient().getOperationsClient().getOperation(operationKey); | ||
getLogger().info(operation.toString()); | ||
if (operation.getDone() && !operation.hasError()) { | ||
GeneratedMessageV3 response = deserializeResponse(operation.getResponse().getValue()); | ||
FlowFile childFlowFile = session.create(flowFile); | ||
session.write(childFlowFile, out -> out.write(JsonFormat.printer().print(response).getBytes(StandardCharsets.UTF_8))); | ||
session.putAttribute(childFlowFile, CoreAttributes.MIME_TYPE.key(), "application/json"); | ||
session.transfer(flowFile, REL_ORIGINAL); | ||
session.transfer(childFlowFile, REL_SUCCESS); | ||
} else if (!operation.getDone()) { | ||
session.transfer(flowFile, REL_RUNNING); | ||
} else { | ||
Status error = operation.getError(); | ||
getLogger().error("Failed to execute vision operation. Error code: {}, Error message: {}", error.getCode(), error.getMessage()); | ||
session.transfer(flowFile, REL_FAILURE); | ||
} | ||
} catch (Exception e) { | ||
getLogger().error("Fail to get GCP Vision operation's status", e); | ||
session.transfer(flowFile, REL_FAILURE); | ||
} | ||
} | ||
|
||
abstract protected GeneratedMessageV3 deserializeResponse(ByteString responseValue) throws InvalidProtocolBufferException; | ||
} |
81 changes: 81 additions & 0 deletions
81
.../src/main/java/org/apache/nifi/processors/gcp/vision/AbstractStartGcpVisionOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.nifi.processors.gcp.vision; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.protobuf.util.JsonFormat; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import org.apache.nifi.annotation.lifecycle.OnStopped; | ||
import org.apache.nifi.components.PropertyDescriptor; | ||
import org.apache.nifi.flowfile.FlowFile; | ||
import org.apache.nifi.processor.ProcessContext; | ||
import org.apache.nifi.processor.ProcessSession; | ||
import org.apache.nifi.processor.exception.ProcessException; | ||
|
||
public abstract class AbstractStartGcpVisionOperation<B extends com.google.protobuf.GeneratedMessageV3.Builder<B>> extends AbstractGcpVisionProcessor { | ||
|
||
@Override | ||
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { | ||
FlowFile flowFile = session.get(); | ||
if (flowFile == null && !context.getProperty(getJsonPayloadPropertyDescriptor()).isSet()) { | ||
return; | ||
} else if (flowFile == null) { | ||
flowFile = session.create(); | ||
} | ||
try { | ||
OperationFuture<?, ?> asyncResponse = startOperation(session, context, flowFile); | ||
String operationName = asyncResponse.getName(); | ||
session.putAttribute(flowFile, GCP_OPERATION_KEY, operationName); | ||
session.transfer(flowFile, REL_SUCCESS); | ||
} catch (Exception e) { | ||
getLogger().error("Fail to start GCP Vision operation", e); | ||
session.transfer(flowFile, REL_FAILURE); | ||
} | ||
} | ||
|
||
@OnStopped | ||
public void onStopped() throws IOException { | ||
getVisionClient().close(); | ||
} | ||
|
||
protected OperationFuture<?, ?> startOperation(ProcessSession session, ProcessContext context, FlowFile flowFile) { | ||
B builder = newBuilder(); | ||
InputStream inStream = context.getProperty(getJsonPayloadPropertyDescriptor()).isSet() | ||
? getInputStreamFromProperty(context, flowFile) : session.read(flowFile); | ||
try (InputStream inputStream = inStream) { | ||
JsonFormat.parser().ignoringUnknownFields().merge(new InputStreamReader(inputStream), builder); | ||
} catch (final IOException e) { | ||
throw new ProcessException("Read FlowFile Failed", e); | ||
} | ||
return startOperation(builder); | ||
} | ||
|
||
private InputStream getInputStreamFromProperty(ProcessContext context, FlowFile flowFile) { | ||
return new ByteArrayInputStream(context.getProperty(getJsonPayloadPropertyDescriptor()).evaluateAttributeExpressions(flowFile).getValue().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
abstract B newBuilder(); | ||
|
||
abstract OperationFuture<?, ?> startOperation(B builder); | ||
|
||
abstract PropertyDescriptor getJsonPayloadPropertyDescriptor(); | ||
} |
41 changes: 41 additions & 0 deletions
41
.../java/org/apache/nifi/processors/gcp/vision/GetGcpVisionAnnotateFilesOperationStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.nifi.processors.gcp.vision; | ||
|
||
import com.google.cloud.vision.v1p2beta1.AsyncBatchAnnotateFilesResponse; | ||
import com.google.protobuf.ByteString; | ||
import com.google.protobuf.GeneratedMessageV3; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import org.apache.nifi.annotation.behavior.ReadsAttribute; | ||
import org.apache.nifi.annotation.behavior.ReadsAttributes; | ||
import org.apache.nifi.annotation.documentation.CapabilityDescription; | ||
import org.apache.nifi.annotation.documentation.SeeAlso; | ||
import org.apache.nifi.annotation.documentation.Tags; | ||
|
||
@Tags({"Google", "Cloud", "Vision", "Machine Learning"}) | ||
@CapabilityDescription("Retrieves the current status of an Google Vision operation.") | ||
@SeeAlso({StartGcpVisionAnnotateFilesOperation.class}) | ||
@ReadsAttributes({ | ||
@ReadsAttribute(attribute = "operationKey", description = "A unique identifier of the operation designated by the Vision server.") | ||
}) | ||
public class GetGcpVisionAnnotateFilesOperationStatus extends AbstractGetGcpVisionAnnotateOperationStatus { | ||
@Override | ||
protected GeneratedMessageV3 deserializeResponse(ByteString responseValue) throws InvalidProtocolBufferException { | ||
return AsyncBatchAnnotateFilesResponse.parseFrom(responseValue); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...java/org/apache/nifi/processors/gcp/vision/GetGcpVisionAnnotateImagesOperationStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.nifi.processors.gcp.vision; | ||
|
||
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse; | ||
import com.google.protobuf.ByteString; | ||
import com.google.protobuf.GeneratedMessageV3; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import org.apache.nifi.annotation.behavior.ReadsAttribute; | ||
import org.apache.nifi.annotation.behavior.ReadsAttributes; | ||
import org.apache.nifi.annotation.documentation.CapabilityDescription; | ||
import org.apache.nifi.annotation.documentation.SeeAlso; | ||
import org.apache.nifi.annotation.documentation.Tags; | ||
|
||
@Tags({"Google", "Cloud", "Vision", "Machine Learning"}) | ||
@CapabilityDescription("Retrieves the current status of an Google Vision operation.") | ||
@SeeAlso({StartGcpVisionAnnotateImagesOperation.class}) | ||
@ReadsAttributes({ | ||
@ReadsAttribute(attribute = "operationKey", description = "A unique identifier of the operation designated by the Vision server.") | ||
}) | ||
public class GetGcpVisionAnnotateImagesOperationStatus extends AbstractGetGcpVisionAnnotateOperationStatus { | ||
@Override | ||
protected GeneratedMessageV3 deserializeResponse(ByteString responseValue) throws InvalidProtocolBufferException { | ||
return AsyncBatchAnnotateImagesResponse.parseFrom(responseValue); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vision
should be closed in an@OnStopped
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, fixed.