Skip to content

Commit

Permalink
Updated version with ability to generate JSONSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
jgwoolley committed Dec 30, 2024
1 parent 0d9b90b commit 2194730
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 54 deletions.
21 changes: 16 additions & 5 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

A Java CLI for parsing Apache NiFi FlowFiles.

== Background

While the Apache Foundation has its own Unpackager (link:https://github.com/apache/nifi/blob/main/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/Unpackage.java[nifi-flowfile-packager]). It lacks a packager, and dumps all attributes into their own text files which is a little messy for large projects.

I've developed several versions of this project over the years, and created a WebGUI based version: link:https://www.nf2t.net/[].

== How to Install

This program is available in the Maven Central Repository: https://central.sonatype.com/artifact/com.yelloowstone.nf2t/nf2t-cli
This program is available in the Maven Central Repository: link:https://central.sonatype.com/artifact/com.yelloowstone.nf2t/nf2t-cli[]

== How To Run

[source,console]
---
----
$ java -jar target/nf2t-cli-0.0.2.jar
org.apache.commons.cli.MissingOptionException: Missing required options: v, a, i, ousage: CommandLineParameters
-a,--action <arg> The action <package,unpackage>.
Expand All @@ -24,9 +30,14 @@ org.apache.commons.cli.MissingOptionException: Missing required options: v, a, i
directory where the "flowFiles.pkg" will be created
or the name of the file.
-v,--version <arg> The Apache FlowFile version <1,2,3>.
---
----

== How to Build

Run the ./update.sh script to update the versions of the project.
Run the ./build.sh to build the project and get the maven.zip file to upload to maven central repository.
Run the link:./update.sh[] script to update the versions of the project.
Run the link:./build.sh[] to build the project and get the maven.zip file to upload to Maven Central Repository.

== TODO

* Review the standard implementation, make sure this implementation is as similiar as possible: link:https://github.com/apache/nifi/blob/main/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/Unpackage.java[].
* Always say unpackage before package reflecting its primacy.
79 changes: 50 additions & 29 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
set -e

# Configuration
GPG_USER="0xCED254CF741FE1663B9BEC32D12C9545C6D5AA73"

# Evaluate Maven Coordinates

mvn help:evaluate -Dexpression=project.groupId -Doutput=groupId.mvnhelp
mvn help:evaluate -Dexpression=project.artifactId -Doutput=artifactId.mvnhelp
mvn help:evaluate -Dexpression=project.version -Doutput=version.mvnhelp

groupId=$(cat groupId.mvnhelp)
artifactId=$(cat artifactId.mvnhelp)
version=$(cat version.mvnhelp)
artifact_path="${groupId//./\/}/${artifactId//./\/}/${version}/"
prefix_name="${artifactId}-${version}"
prefix_path="${artifact_path}/${prefix_name}"

rm *.mvnhelp

# Run Maven

mvn clean install

VERSION="0.0.2"
REPOSITORY_PATH="com/yelloowstone/nf2t/nf2t-cli/$VERSION/"
POM_TARGET_PATH="./target/nf2t-cli-0.0.2.pom"

mkdir --parents "$REPOSITORY_PATH"
rm -f "$REPOSITORY_PATH*"

generate_file() {
echo "$1"
gpg --yes --local-user 0xCED254CF741FE1663B9BEC32D12C9545C6D5AA73 -ab $1
md5sum $1 | cut -d ' ' -f 1 > "$1.md5"
sha1sum $1 | cut -d ' ' -f 1 > "$1.sha1"
sha256sum $1 | cut -d ' ' -f 1 > "$1.sha256"
sha512sum $1 | cut -d ' ' -f 1 > "$1.sha512"

cp "$1" "./${REPOSITORY_PATH}"
cp "$1.md5" "./${REPOSITORY_PATH}"
cp "$1.sha1" "./${REPOSITORY_PATH}"
cp "$1.sha256" "./${REPOSITORY_PATH}"
cp "$1.asc" "./${REPOSITORY_PATH}"
}

for file in ./target/*.jar; do
generate_file $file
done
# Create Maven Central ZIP Folder Structure

cp "pom.xml" "$POM_TARGET_PATH"
mkdir --parents "$artifact_path"

generate_file "$POM_TARGET_PATH"
artifact_root_path=$(echo "${artifact_path}" | cut -d '/' -f 1)

# Check if the variable is less than 3
if [ "$artifact_root_path" -lt 3 ]; then
echo "Error: Could not delete ${artifact_root_path}"
exit 1
fi

rm -rf "$artifact_root_path"
mkdir --parents "$artifact_path"

mvn help:effective-pom "-Doutput=${prefix_path}.pom"
cp "./target/${prefix_name}-javadoc.jar" "$artifact_path"
cp "./target/${prefix_name}-sources.jar" "$artifact_path"
cp "./target/${prefix_name}.jar" "$artifact_path"

# Create Maven Central ZIP file

for file in "$artifact_path"/*; do
echo "$file"
gpg --yes --local-user $GPG_USER -ab $file
md5sum $file | cut -d ' ' -f 1 > "$file.md5"
sha1sum $file | cut -d ' ' -f 1 > "$file.sha1"
sha256sum $file | cut -d ' ' -f 1 > "$file.sha256"
sha512sum $file | cut -d ' ' -f 1 > "$file.sha512"
done

rm -f ./maven.zip
zip -r ./maven.zip "$artifact_path"

zip -r ./maven.zip "$REPOSITORY_PATH"
rm -rf "$REPOSITORY_PATH"
echo "${groupId}.${artifactId}.${version}/"
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.yelloowstone.nf2t</groupId>
<artifactId>nf2t-cli</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>

<name>nf2t-cli</name>
<description>A Java CLI for parsing Apache NiFi FlowFiles.</description>
Expand Down Expand Up @@ -66,6 +66,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
<version>2.18.2</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -76,7 +81,8 @@
<configuration>
<!--
https://stackoverflow.com/questions/50919125/maven-shade-plugin-remove-original -->
<outputFile>${project.build.directory}/${project.build.finalName}.jar</outputFile>
<outputFile>
${project.build.directory}/${project.build.finalName}.jar</outputFile>
<filters>
<filter>
<artifact>*:*</artifact>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/yelloowstone/nf2t/cli/Action.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.yelloowstone.nf2t.cli;

public enum Action {
PACKAGE, UNPACKAGE;
PACKAGE, UNPACKAGE, SCHEMA;
}
33 changes: 20 additions & 13 deletions src/main/java/com/yelloowstone/nf2t/cli/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class App {
public static final String PACKAGE_EXTENSION = ".pkg";
public static final String FLOWFILE_DEFAULT_FILENAME = "flowFiles" + PACKAGE_EXTENSION;

public static final String FILE_SIZE_ATTRIBUTE = "size";

private final PrintStream stderr;
Expand All @@ -64,26 +65,20 @@ public App(final PrintStream stdout, final PrintStream stderr) {

final List<String> actions = List.of(Action.values()).stream().map(x -> x.toString().toLowerCase()).sorted()
.collect(Collectors.toList());
final List<String> versions = this.unpackagers.keySet().stream().map(x -> x.toString()).sorted()
.collect(Collectors.toList());

options.addOption("h", "help", false, "Shows help information.");
options.addRequiredOption("v", "version", true,
"The Apache FlowFile version <" + String.join(",", versions) + ">.");
options.addRequiredOption("v", "version", true, FlowFileStreamResult.VERSION_DESCRIPTION);
options.addRequiredOption("a", "action", true, "The action <" + String.join(",", actions) + ">.");
options.addRequiredOption("i", "in", true,
"The input path. For unpackage, a directory or file containing a FlowFileStream. For package, a directory or file containing FlowFile content.");
options.addRequiredOption("o", "out", true,
"The output path. For unpackage, a directory containing the FlowFile content. For package, a directory where the \""
+ FLOWFILE_DEFAULT_FILENAME + "\" will be created or the name of the file.");
options.addRequiredOption("i", "in", true, FlowFileStreamResult.INPUTPATH_DESCRIPTION);
options.addRequiredOption("o", "out", true, FlowFileStreamResult.OUTPUTPATH_DESCRIPTION);
}

public void packageFiles(final FlowFileStreamResult result) {
final Path inputPath = result.getInputPath();
Path outputPath = result.getOutputPath();

if (Files.isDirectory(outputPath)) {
outputPath = outputPath.resolve(FLOWFILE_DEFAULT_FILENAME);
outputPath = outputPath.resolve(FlowFileStreamResult.FLOWFILE_DEFAULT_FILENAME);
result.setOutputPath(outputPath);
}

Expand Down Expand Up @@ -191,6 +186,13 @@ public void unpackageFiles(final FlowFileStreamResult result) {
}
}

public void generateSchema() throws JsonProcessingException {
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(FlowFileStreamResult.class, visitor);
JsonSchema personSchema = visitor.finalSchema();
this.stdout.print(this.mapper.writer().writeValueAsString(personSchema));
}

public FlowFileStreamResult parse(final CommandLineParser parser, final String[] args) {
try {
final CommandLine commandLine = parser.parse(this.options, args);
Expand Down Expand Up @@ -218,8 +220,13 @@ public FlowFileStreamResult parse(final CommandLineParser parser, final String[]
} else if (Action.UNPACKAGE == action) {
unpackageFiles(result);
}

if(Action.SCHEMA == action) {
generateSchema();
} else {
this.stdout.print(this.mapper.writer().writeValueAsString(result));
}

this.stdout.print(this.mapper.writer().writeValueAsString(result));
return result;

} catch (ParseException | JsonProcessingException e) {
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/com/yelloowstone/nf2t/cli/FlowFileStreamResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,44 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;

public class FlowFileStreamResult {
public static final String PACKAGE_EXTENSION = ".pkg";
public static final String FLOWFILE_DEFAULT_FILENAME = "flowFiles" + PACKAGE_EXTENSION;

public static final String VERSION_DESCRIPTION = "This is the FlowFile version of the file tis packaged or unpackaged.";
@JsonPropertyDescription(VERSION_DESCRIPTION)
@JsonProperty("version")
final private int version;

public static final String INPUTPATH_DESCRIPTION = "The input path. For unpackage, a directory or file containing a FlowFileStream. For package, a directory or file containing FlowFile content.";
@JsonPropertyDescription(INPUTPATH_DESCRIPTION)
@JsonProperty("inputPath")
final private Path inputPath;

public static final String OUTPUTPATH_DESCRIPTION = "The output path. For unpackage, a directory containing the FlowFile content. For package, a directory where the \""
+ FLOWFILE_DEFAULT_FILENAME + "\" will be created or the name of the file.";

@JsonPropertyDescription(OUTPUTPATH_DESCRIPTION)
@JsonProperty("outputPath")
private Path outputPath;

@JsonProperty("unixTime")
final private long unixTime;

@JsonPropertyDescription("The errors encountered while trying to unpackage or package the file(s).")
@JsonProperty("errors")
final private List<ErrorResult> errors = new ArrayList<>();

@JsonPropertyDescription("The file(s) that were successfully unpackaged or packaged.")
@JsonProperty("outputFiles")
final private List<FlowFileResult> outputFiles = new ArrayList<>();

@JsonCreator
public FlowFileStreamResult(@JsonProperty("version") final int version, @JsonProperty("inputPath") final Path inputPath, @JsonProperty("outputPath") final Path outputPath, @JsonProperty("unixTime") final long unixTime) {
public FlowFileStreamResult(@JsonProperty("version") final int version,
@JsonProperty("inputPath") final Path inputPath, @JsonProperty("outputPath") final Path outputPath,
@JsonProperty("unixTime") final long unixTime) {
this.version = version;
this.inputPath = inputPath;
this.outputPath = outputPath;
Expand All @@ -44,7 +65,7 @@ public Path getOutputPath() {
public void setOutputPath(Path outputPath) {
this.outputPath = outputPath;
}

public long getUnixTime() {
return unixTime;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/yelloowstone/nf2t/cli/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void testUnpackageFiles() throws Exception {
Files.write(examplePath, content);
}

final Path flowFileStreamPath = packagedPath.resolve(App.FLOWFILE_DEFAULT_FILENAME);
final Path flowFileStreamPath = packagedPath.resolve(FlowFileStreamResult.FLOWFILE_DEFAULT_FILENAME);

try (OutputStream out = Files.newOutputStream(flowFileStreamPath)) {
for(Path path: Files.list(contentPath).collect(Collectors.toList())) {
Expand Down
2 changes: 1 addition & 1 deletion update.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mvn versions:set -DnewVersion=0.0.2
mvn versions:set -DnewVersion=0.0.3
mvn versions:commit

0 comments on commit 2194730

Please sign in to comment.