Google cloud dataflow 将参数从云函数传递到数据流
我想把上传到谷歌云存储的文件名从云函数传递到数据流,这样我就可以处理上传的文件了 我为云函数编写的代码是Google cloud dataflow 将参数从云函数传递到数据流,google-cloud-dataflow,google-cloud-functions,Google Cloud Dataflow,Google Cloud Functions,我想把上传到谷歌云存储的文件名从云函数传递到数据流,这样我就可以处理上传的文件了 我为云函数编写的代码是 const google = require('googleapis'); exports.goWithTheDataFlow = function(event, callback) { const file = event.data; if (file.resourceState === 'exists' && file.name) { console.lo
const google = require('googleapis');
exports.goWithTheDataFlow = function(event, callback) {
const file = event.data;
if (file.resourceState === 'exists' && file.name) {
console.log(file.name);
google.auth.getApplicationDefault(function (err, authClient, projectId) {
if (err) {
throw err;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/userinfo.email'
]);
}
const dataflow = google.dataflow({ version: 'v1b3', auth: authClient });
dataflow.projects.templates.create({
projectId: '-------',
resource: {
parameters: {
inputFile: `gs://${file.bucket}/${file.name}`
},
jobName: '-------',
gcsPath: '-------'
}
}, function(err, response) {
if (err) {
console.error("problem running dataflow template, error was: ", err);
}
console.log("Dataflow template response: ", response);
callback();
});
});
}
};
我的管道代码如下所示:
public interface FruitOptions extends PipelineOptions {
@Description("Path of the file to read from")
@Validation.Required
ValueProvider<String> getInputFile();
void setInputFile(ValueProvider<String> value);
}
//Main Method
FruitOptions options = PipelineOptionsFactory.fromArgs(args).withValidation()
.as(FruitOptions.class);
Pipeline p = Pipeline.create(options);
我执行的命令是:
mvn compile exec:java -Dexec.mainClass=Common.StarterPipeline -Dexec.args="--project=******** --stagingLocation=gs://******** --dataflowJobFile=gs://********* --runner=TemplatingDataflowPipelineRunner"
POM文件:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Common</groupId>
<artifactId>Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>ossrh.snapshots</id>
<name>Sonatype OSS Repository Hosting</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>com.google.cloud.dataflow</groupId>
<artifactId>google-cloud-dataflow-java-sdk-all</artifactId>
<version>2.0.0</version>
</dependency>
<!-- slf4j API frontend binding with JUL backend -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.14</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.14</version>
</dependency>
</dependencies>
</project>
4.0.0
普通的
普通的
0.0.1-快照
UTF-8
ossrh.snapshots
Sonatype OSS存储库托管
https://oss.sonatype.org/content/repositories/snapshots/
假的
真的
org.apache.maven.plugins
maven编译器插件
3.5.1
1.8
1.8
org.codehaus.mojo
execmaven插件
1.4.0
假的
com.google.cloud.dataflow
google云数据流java sdk全部
2.0.0
org.slf4j
slf4j api
1.7.14
org.slf4j
slf4j-jdk14
1.7.14
谁能帮帮我吗。如果我做错了什么,请告诉我。谢谢Hi rish,请您显示您收到的错误。另外,您介意描述一下您的用例吗?您是否专门尝试从云功能启动管道?您是否考虑过创建一个模板管道并使用dataflow命令行工具启动它?您好@AlexAmato 1)我已经用收到的错误更新了我的帖子。2) 用例是我希望使用特定bucket中的数据流来处理每个文件。因此,云函数必须监视该bucket,并在文件进入时触发管道(模板),并将该文件的名称发送到管道。3) 是的,我试图从云功能启动管道。4) 我做了,但我希望这个过程是自动化的,因为在管道中应该在文件一进来就开始。如果你觉得其他方法更好,请告诉我。谢谢你能提供更多细节吗,你能发布你的maven pom吗?您是否能够正常运行管道,而无需将其作为模板启动?你已经验证过了吗?我尝试过使用Dataflow2.x,即ApacheBeam,今天我成功地创建了这个模板。但是,管道代码保持不变。所以我猜在使用以前的版本时缺少了一些东西。但无论如何,谢谢你:)对不起,我们没有指出这个问题,但我相信这个pom对其他可能遇到同样问题的人会很有用。谢谢你发布:)。嗨,瑞什,请你显示一下你收到的错误。另外,您介意描述一下您的用例吗?您是否专门尝试从云功能启动管道?您是否考虑过创建一个模板管道并使用dataflow命令行工具启动它?您好@AlexAmato 1)我已经用收到的错误更新了我的帖子。2) 用例是我希望使用特定bucket中的数据流来处理每个文件。因此,云函数必须监视该bucket,并在文件进入时触发管道(模板),并将该文件的名称发送到管道。3) 是的,我试图从云功能启动管道。4) 我做了,但我希望这个过程是自动化的,因为在管道中应该在文件一进来就开始。如果你觉得其他方法更好,请告诉我。谢谢你能提供更多细节吗,你能发布你的maven pom吗?您是否能够正常运行管道,而无需将其作为模板启动?你已经验证过了吗?我尝试过使用Dataflow2.x,即ApacheBeam,今天我成功地创建了这个模板。但是,管道代码保持不变。所以我猜在使用以前的版本时缺少了一些东西。但无论如何,谢谢你:)对不起,我们没有指出这个问题,但我相信这个pom对其他可能遇到同样问题的人会很有用。谢谢你发邮件:)。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Common</groupId>
<artifactId>Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>ossrh.snapshots</id>
<name>Sonatype OSS Repository Hosting</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>com.google.cloud.dataflow</groupId>
<artifactId>google-cloud-dataflow-java-sdk-all</artifactId>
<version>2.0.0</version>
</dependency>
<!-- slf4j API frontend binding with JUL backend -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.14</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.14</version>
</dependency>
</dependencies>
</project>