-
Notifications
You must be signed in to change notification settings - Fork 8
Refactor #6
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
Refactor #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| name: Java CI | ||
| on: | ||
| push: | ||
| branches: [ "main" ] | ||
| pull_request: | ||
| branches: [ "main" ] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - uses: actions/setup-java@v3 | ||
| with: | ||
| distribution: 'temurin' | ||
| java-version: '11' | ||
| - name: Build with Maven | ||
| run: mvn -B test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,35 +23,37 @@ public static void main(String[] args) { | |
| DateTime dateTime = new DateTime(2020, 6, 1, 0, 0, 0, 0, DateTimeZone.UTC); | ||
|
|
||
| for(int i = 0; i < 2; i++) { | ||
| generateParquetFileFor(dateTime.plusDays(i)); | ||
| try { | ||
| generateParquetFileFor(dateTime.plusDays(i)); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(System.out); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void generateParquetFileFor(DateTime dateTime) { | ||
| try { | ||
| Schema schema = parseSchema(); | ||
| DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd"); | ||
| Path path = new Path("data_" + dateTime.toString(fmt) + ".parquet"); | ||
|
|
||
| List<GenericData.Record> recordList = generateRecords(schema, dateTime); | ||
|
|
||
| try (ParquetWriter<GenericData.Record> writer = AvroParquetWriter.<GenericData.Record>builder(path) | ||
| .withSchema(schema) | ||
| .withCompressionCodec(CompressionCodecName.SNAPPY) | ||
| .withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE) | ||
| .withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE) | ||
| .withConf(new Configuration()) | ||
| .withValidation(false) | ||
| .withDictionaryEncoding(false) | ||
| .build()) { | ||
|
|
||
| for (GenericData.Record record : recordList) { | ||
| writer.write(record); | ||
| } | ||
| public static Path generateParquetFileFor(DateTime dateTime) throws Exception { | ||
|
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. Declaring Looking at the Parquet writer and file operations, public static Path generateParquetFileFor(DateTime dateTime) throws java.io.IOException { |
||
| Schema schema = parseSchema(); | ||
| DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd"); | ||
| Path path = new Path("data_" + dateTime.toString(fmt) + ".parquet"); | ||
|
|
||
| List<GenericData.Record> recordList = generateRecords(schema, dateTime); | ||
|
|
||
| try (ParquetWriter<GenericData.Record> writer = AvroParquetWriter.<GenericData.Record>builder(path) | ||
| .withSchema(schema) | ||
| .withCompressionCodec(CompressionCodecName.SNAPPY) | ||
| .withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE) | ||
| .withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE) | ||
| .withConf(new Configuration()) | ||
| .withValidation(false) | ||
| .withDictionaryEncoding(false) | ||
| .build()) { | ||
|
|
||
| for (GenericData.Record record : recordList) { | ||
| writer.write(record); | ||
| } | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(System.out); | ||
| } | ||
|
|
||
| return path; | ||
| } | ||
|
|
||
| private static Schema parseSchema() { | ||
|
|
@@ -87,4 +89,4 @@ private static List<GenericData.Record> generateRecords(Schema schema, DateTime | |
|
|
||
| return recordList; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.instarsocial.parquet; | ||
|
|
||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.joda.time.DateTime; | ||
| import org.joda.time.DateTimeZone; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| /** | ||
| * Tests for {@link App} using the Arrange-Act-Assert pattern. | ||
| */ | ||
|
|
||
| public class AppTest { | ||
|
|
||
| /** Temporary file generated by the test. */ | ||
| private static File generatedFile; | ||
|
|
||
| @BeforeClass | ||
| public static void beforeAll() { | ||
| generatedFile = null; | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void afterAll() { | ||
| if (generatedFile != null && generatedFile.exists()) { | ||
| generatedFile.delete(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void Should_CreateParquetFile_When_CallingGenerateParquetFileFor() throws Exception { | ||
| // Arrange | ||
| DateTime dateTime = new DateTime(2020, 6, 1, 0, 0, 0, 0, DateTimeZone.UTC); | ||
|
|
||
| // Act | ||
| org.apache.hadoop.fs.Path actual = App.generateParquetFileFor(dateTime); | ||
|
|
||
| // Assert | ||
| assertNotNull("Path should not be null", actual); | ||
| generatedFile = new File(actual.toString()); | ||
| assertTrue("Generated file should exist", generatedFile.exists()); | ||
|
|
||
| try (FileInputStream fis = new FileInputStream(generatedFile)) { | ||
| byte[] magic = new byte[4]; | ||
| int read = fis.read(magic); | ||
| assertEquals(4, read); | ||
| assertEquals("PAR1", new String(magic, StandardCharsets.US_ASCII)); | ||
| } | ||
| } | ||
| } |
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.
Using
e.printStackTrace()is generally discouraged in application code, especially when writing toSystem.out. It can be difficult to manage and control log output this way.Could we consider using a dedicated logging framework (like SLF4J with Logback or Log4j2) here? This would provide more robust and configurable logging.
For example, with SLF4J:
If adding a full logging framework is out of scope for this PR, at least consider writing to
System.errfor errors, or re-throwing a custom exception ifmainshouldn't handle this directly.