-
Notifications
You must be signed in to change notification settings - Fork 34
[DCP] Adds JSON-LD Support to the ingestion pipeline #495
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
Open
gmechali
wants to merge
13
commits into
datacommonsorg:master
Choose a base branch
from
gmechali:jsonld
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8348329
Adds JSON-LD support - still needs much more testing and validiton
gmechali ac8a9a3
Clean up for JSON-LD support. Verified MCF and JSON-ld
gmechali d3a0d9f
More cleanup
gmechali be5b333
Gemini comment cleanup
gmechali b742f4e
Adds a todo
gmechali b6b1234
Remove the JSONLD + CSV combo for now.
gmechali ac9b136
Undo the JSONLD Tmpl + CSV code which wasnt working
gmechali 79d48a6
More cleanup for PR
gmechali 53923bc
Addresses comment from Dan regarding namespaced values. We current re…
gmechali cf19a3c
Merge branch 'master' into jsonld
gmechali 895e5d5
Code cleanup
gmechali def651a
more cleanup, tested and verified with mcf
gmechali 5c3504d
Apply suggestions from code review
gmechali 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.channels.Channels; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
|
|
@@ -18,6 +20,7 @@ | |
| import java.util.stream.StreamSupport; | ||
| import java.util.zip.GZIPOutputStream; | ||
| import org.apache.beam.sdk.Pipeline; | ||
| import org.apache.beam.sdk.io.FileIO; | ||
| import org.apache.beam.sdk.io.TFRecordIO; | ||
| import org.apache.beam.sdk.io.TextIO; | ||
| import org.apache.beam.sdk.io.fs.EmptyMatchTreatment; | ||
|
|
@@ -39,6 +42,7 @@ | |
| import org.datacommons.proto.Mcf.McfOptimizedGraph; | ||
| import org.datacommons.proto.Mcf.McfStatVarObsSeries; | ||
| import org.datacommons.util.GraphUtils; | ||
| import org.datacommons.util.parser.jsonld.JsonLdParser; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -162,6 +166,55 @@ public McfGraph apply(String input) { | |
| return mcf; | ||
| } | ||
|
|
||
| // Input file formats supported by the ingestion pipeline. | ||
| public enum InputFormat { | ||
| JSONLD, | ||
| TFRECORD, | ||
| MCF | ||
| } | ||
|
|
||
| /** Resolves the input format based on the file path. */ | ||
| public static InputFormat resolveFormat(String graphPath) { | ||
| if (graphPath == null) { | ||
| throw new IllegalArgumentException("graphPath cannot be null"); | ||
| } | ||
| if (graphPath.contains("tfrecord")) { | ||
| return InputFormat.TFRECORD; | ||
| } | ||
| if (graphPath.contains(".jsonld")) { | ||
| return InputFormat.JSONLD; | ||
| } | ||
| // Fallback to MCF as default behavior | ||
| return InputFormat.MCF; | ||
| } | ||
|
|
||
| /** Reads JSON-LD files and converts them to McfGraph protos. */ | ||
| public static PCollection<McfGraph> readJsonLdFiles(String name, String files, Pipeline p) { | ||
| return p.apply("MatchJsonLdFiles-" + name, FileIO.match().filepattern(files)) | ||
| .apply("ReadJsonLdFiles-" + name, FileIO.readMatches()) | ||
| .apply( | ||
| "ParseJsonLd-" + name, | ||
| ParDo.of( | ||
| new DoFn<FileIO.ReadableFile, McfGraph>() { | ||
| @ProcessElement | ||
| public void processElement( | ||
| @Element FileIO.ReadableFile file, OutputReceiver<McfGraph> receiver) { | ||
| try (InputStream is = Channels.newInputStream(file.open())) { | ||
| McfGraph graph = JsonLdParser.parse(is); | ||
|
Contributor
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. |
||
| for (Map.Entry<String, org.datacommons.proto.Mcf.McfGraph.PropertyValues> | ||
| entry : graph.getNodesMap().entrySet()) { | ||
| McfGraph.Builder singleNodeGraph = McfGraph.newBuilder(); | ||
| singleNodeGraph.putNodes(entry.getKey(), entry.getValue()); | ||
| receiver.output(singleNodeGraph.build()); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new RuntimeException( | ||
| "Failed to parse JSON-LD file: " + file.toString(), e); | ||
| } | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| public static PCollectionTuple splitGraph(String name, PCollection<McfGraph> graph) { | ||
| return graph.apply( | ||
| "SplitGraph-" + name, | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.