forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Function Config and Functions Cmd (apache#4)
- introduce a `fs` package for function state related classes - add `FunctionConfig` for user defined function configurations - add `CmdFunctions`: it takes a yaml file and also command parameters
- Loading branch information
Showing
11 changed files
with
460 additions
and
57 deletions.
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
83 changes: 83 additions & 0 deletions
83
pulsar-functions/src/main/java/org/apache/pulsar/admin/cli/CmdFunctions.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,83 @@ | ||
/** | ||
* 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.pulsar.admin.cli; | ||
|
||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | ||
import org.apache.commons.lang3.builder.ToStringStyle; | ||
import org.apache.pulsar.client.admin.PulsarAdmin; | ||
import org.apache.pulsar.functions.fs.FunctionConfig; | ||
|
||
@Parameters(commandDescription = "Operations about functions") | ||
public class CmdFunctions extends CmdBase { | ||
|
||
private final LocalRunner cmdRunner; | ||
|
||
@Getter | ||
@Parameters(commandDescription = "Run function locally") | ||
class LocalRunner extends CliCommand { | ||
|
||
@Parameter(names = "--name", description = "Function Name\n") | ||
private String name; | ||
@Parameter(names = "--source-topic", description = "Input Topic Name\n") | ||
private String sourceTopicName; | ||
@Parameter(names = "--sink-topic", description = "Output Topic Name\n") | ||
private String sinkTopicName; | ||
|
||
@Parameter(names = "--function-config", description = "Function Config\n") | ||
private String fnConfigFile; | ||
|
||
@Override | ||
void run() throws Exception { | ||
FunctionConfig fc; | ||
if (null != fnConfigFile) { | ||
fc = FunctionConfig.load(fnConfigFile); | ||
} else { | ||
fc = new FunctionConfig(); | ||
} | ||
if (null != sourceTopicName) { | ||
fc.setSourceTopic(sourceTopicName); | ||
} | ||
if (null != sinkTopicName) { | ||
fc.setSinkTopic(sinkTopicName); | ||
} | ||
if (null != name) { | ||
fc.setName(name); | ||
} | ||
// TODO: execute the runner here | ||
|
||
System.out.println(ReflectionToStringBuilder.toString(fc, ToStringStyle.MULTI_LINE_STYLE)); | ||
} | ||
|
||
} | ||
|
||
public CmdFunctions(PulsarAdmin admin) { | ||
super("functions", admin); | ||
cmdRunner = new LocalRunner(); | ||
jcommander.addCommand("run", cmdRunner); | ||
} | ||
|
||
@VisibleForTesting | ||
LocalRunner getCmdRunner() { | ||
return cmdRunner; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
pulsar-functions/src/main/java/org/apache/pulsar/admin/cli/FunctionsTool.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,59 @@ | ||
/** | ||
* 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.pulsar.admin.cli; | ||
|
||
import java.io.FileInputStream; | ||
import java.util.Arrays; | ||
import java.util.Properties; | ||
|
||
/** | ||
* TODO: merge this into {@link PulsarAdminTool}. | ||
*/ | ||
public class FunctionsTool extends PulsarAdminTool { | ||
|
||
FunctionsTool(Properties properties) throws Exception { | ||
super(properties); | ||
commandMap.put("functions", CmdFunctions.class); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
String configFile = args[0]; | ||
Properties properties = new Properties(); | ||
|
||
if (configFile != null) { | ||
FileInputStream fis = null; | ||
try { | ||
fis = new FileInputStream(configFile); | ||
properties.load(fis); | ||
} finally { | ||
if (fis != null) | ||
fis.close(); | ||
} | ||
} | ||
|
||
FunctionsTool tool = new FunctionsTool(properties); | ||
|
||
if (tool.run(Arrays.copyOfRange(args, 1, args.length))) { | ||
System.exit(0); | ||
} else { | ||
System.exit(1); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
pulsar-functions/src/main/java/org/apache/pulsar/admin/cli/package-info.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,25 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Pulsar Functions CLI. | ||
* | ||
* TODO: move this to `pulsar-client-tools` after merged back to the apache repo. | ||
*/ | ||
package org.apache.pulsar.admin.cli; |
47 changes: 47 additions & 0 deletions
47
pulsar-functions/src/main/java/org/apache/pulsar/functions/fs/FunctionConfig.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,47 @@ | ||
/** | ||
* 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.pulsar.functions.fs; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* Function Configuration. | ||
*/ | ||
@Setter | ||
@Getter | ||
public class FunctionConfig { | ||
|
||
// function name | ||
private String name; | ||
// source topic | ||
private String sourceTopic; | ||
// sink topic | ||
private String sinkTopic; | ||
|
||
public static FunctionConfig load(String yamlFile) throws IOException { | ||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); | ||
return mapper.readValue(new File(yamlFile), FunctionConfig.class); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
pulsar-functions/src/main/java/org/apache/pulsar/functions/fs/package-info.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,23 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Function State. | ||
*/ | ||
package org.apache.pulsar.functions.fs; |
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
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
Oops, something went wrong.