Spark - Java - Create Parquet/Avro Without Using Dataframes of Spark SQL










0















I want to get output of a Spark application(which we only use core Spark and people working on the project do not want to change it to Spark SQL) as Parquet or Avro files.



When I look for these two file types, I couldn't find any example without DataFrames, or in general Spark SQL. Can I achieve this without using SparkSQL?



My data is tabular, it has columns but in the processing, all data will be used, not a single column. It's columns are decided at runtime, so there is no "name,ID,adress" kinda generic columns. It looks like this:



No f1 f2 f3 ...
1, 123.456, 123.457, 123.458, ...
2, 123.789, 123.790, 123.791, ...
...









share|improve this question






















  • Could you please elaborate what do you want to achieve

    – Chandan Ray
    Aug 17 '18 at 10:36











  • My output is going large as I use more input data. Currently, it is 3.5Gb. It should be smaller in size(which I can achieve with Snappy compression), but they also ask me if there is another output type which can be used to decrease the size, read/write time etc. Currently it is just a human-readeble text file.

    – Melih
    Aug 17 '18 at 11:14







  • 1





    Ok so if you want to save your output in Avro without dataframe and spark sql then you can use rdd

    – Chandan Ray
    Aug 17 '18 at 11:16






  • 2





    Rdd.toDF().write.parquet(filepath)

    – Chandan Ray
    Aug 17 '18 at 11:22











  • Okay, which means anyway I need to use SparkSQL. At least only at the end. Because, I can't see a toDF() method for my Pair and normal JavaRDD's.

    – Melih
    Aug 17 '18 at 11:26
















0















I want to get output of a Spark application(which we only use core Spark and people working on the project do not want to change it to Spark SQL) as Parquet or Avro files.



When I look for these two file types, I couldn't find any example without DataFrames, or in general Spark SQL. Can I achieve this without using SparkSQL?



My data is tabular, it has columns but in the processing, all data will be used, not a single column. It's columns are decided at runtime, so there is no "name,ID,adress" kinda generic columns. It looks like this:



No f1 f2 f3 ...
1, 123.456, 123.457, 123.458, ...
2, 123.789, 123.790, 123.791, ...
...









share|improve this question






















  • Could you please elaborate what do you want to achieve

    – Chandan Ray
    Aug 17 '18 at 10:36











  • My output is going large as I use more input data. Currently, it is 3.5Gb. It should be smaller in size(which I can achieve with Snappy compression), but they also ask me if there is another output type which can be used to decrease the size, read/write time etc. Currently it is just a human-readeble text file.

    – Melih
    Aug 17 '18 at 11:14







  • 1





    Ok so if you want to save your output in Avro without dataframe and spark sql then you can use rdd

    – Chandan Ray
    Aug 17 '18 at 11:16






  • 2





    Rdd.toDF().write.parquet(filepath)

    – Chandan Ray
    Aug 17 '18 at 11:22











  • Okay, which means anyway I need to use SparkSQL. At least only at the end. Because, I can't see a toDF() method for my Pair and normal JavaRDD's.

    – Melih
    Aug 17 '18 at 11:26














0












0








0








I want to get output of a Spark application(which we only use core Spark and people working on the project do not want to change it to Spark SQL) as Parquet or Avro files.



When I look for these two file types, I couldn't find any example without DataFrames, or in general Spark SQL. Can I achieve this without using SparkSQL?



My data is tabular, it has columns but in the processing, all data will be used, not a single column. It's columns are decided at runtime, so there is no "name,ID,adress" kinda generic columns. It looks like this:



No f1 f2 f3 ...
1, 123.456, 123.457, 123.458, ...
2, 123.789, 123.790, 123.791, ...
...









share|improve this question














I want to get output of a Spark application(which we only use core Spark and people working on the project do not want to change it to Spark SQL) as Parquet or Avro files.



When I look for these two file types, I couldn't find any example without DataFrames, or in general Spark SQL. Can I achieve this without using SparkSQL?



My data is tabular, it has columns but in the processing, all data will be used, not a single column. It's columns are decided at runtime, so there is no "name,ID,adress" kinda generic columns. It looks like this:



No f1 f2 f3 ...
1, 123.456, 123.457, 123.458, ...
2, 123.789, 123.790, 123.791, ...
...






java apache-spark avro parquet






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 17 '18 at 9:33









MelihMelih

8914




8914












  • Could you please elaborate what do you want to achieve

    – Chandan Ray
    Aug 17 '18 at 10:36











  • My output is going large as I use more input data. Currently, it is 3.5Gb. It should be smaller in size(which I can achieve with Snappy compression), but they also ask me if there is another output type which can be used to decrease the size, read/write time etc. Currently it is just a human-readeble text file.

    – Melih
    Aug 17 '18 at 11:14







  • 1





    Ok so if you want to save your output in Avro without dataframe and spark sql then you can use rdd

    – Chandan Ray
    Aug 17 '18 at 11:16






  • 2





    Rdd.toDF().write.parquet(filepath)

    – Chandan Ray
    Aug 17 '18 at 11:22











  • Okay, which means anyway I need to use SparkSQL. At least only at the end. Because, I can't see a toDF() method for my Pair and normal JavaRDD's.

    – Melih
    Aug 17 '18 at 11:26


















  • Could you please elaborate what do you want to achieve

    – Chandan Ray
    Aug 17 '18 at 10:36











  • My output is going large as I use more input data. Currently, it is 3.5Gb. It should be smaller in size(which I can achieve with Snappy compression), but they also ask me if there is another output type which can be used to decrease the size, read/write time etc. Currently it is just a human-readeble text file.

    – Melih
    Aug 17 '18 at 11:14







  • 1





    Ok so if you want to save your output in Avro without dataframe and spark sql then you can use rdd

    – Chandan Ray
    Aug 17 '18 at 11:16






  • 2





    Rdd.toDF().write.parquet(filepath)

    – Chandan Ray
    Aug 17 '18 at 11:22











  • Okay, which means anyway I need to use SparkSQL. At least only at the end. Because, I can't see a toDF() method for my Pair and normal JavaRDD's.

    – Melih
    Aug 17 '18 at 11:26

















Could you please elaborate what do you want to achieve

– Chandan Ray
Aug 17 '18 at 10:36





Could you please elaborate what do you want to achieve

– Chandan Ray
Aug 17 '18 at 10:36













My output is going large as I use more input data. Currently, it is 3.5Gb. It should be smaller in size(which I can achieve with Snappy compression), but they also ask me if there is another output type which can be used to decrease the size, read/write time etc. Currently it is just a human-readeble text file.

– Melih
Aug 17 '18 at 11:14






My output is going large as I use more input data. Currently, it is 3.5Gb. It should be smaller in size(which I can achieve with Snappy compression), but they also ask me if there is another output type which can be used to decrease the size, read/write time etc. Currently it is just a human-readeble text file.

– Melih
Aug 17 '18 at 11:14





1




1





Ok so if you want to save your output in Avro without dataframe and spark sql then you can use rdd

– Chandan Ray
Aug 17 '18 at 11:16





Ok so if you want to save your output in Avro without dataframe and spark sql then you can use rdd

– Chandan Ray
Aug 17 '18 at 11:16




2




2





Rdd.toDF().write.parquet(filepath)

– Chandan Ray
Aug 17 '18 at 11:22





Rdd.toDF().write.parquet(filepath)

– Chandan Ray
Aug 17 '18 at 11:22













Okay, which means anyway I need to use SparkSQL. At least only at the end. Because, I can't see a toDF() method for my Pair and normal JavaRDD's.

– Melih
Aug 17 '18 at 11:26






Okay, which means anyway I need to use SparkSQL. At least only at the end. Because, I can't see a toDF() method for my Pair and normal JavaRDD's.

– Melih
Aug 17 '18 at 11:26













2 Answers
2






active

oldest

votes


















1














You can’t save an rdd in parquet without converting it to dataframe. Rdd does not have schema but parquet file is in columnar format which needs schema, so we need to convert it to dataframe.



You can use createdataframe api






share|improve this answer






























    0














    I tried this and it works like a champ...



    public class ParquetHelper

    static ParquetWriter<GenericData.Record> writer = null;
    private static Schema schema;

    public ParquetHelper(Schema schema, String pathName)

    try
    Path path = new Path(pathName);
    writer = AvroParquetWriter.
    <GenericData.Record>builder(path)
    .withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE)
    .withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE)
    .withSchema(schema)
    .withConf(new Configuration())
    .withCompressionCodec(CompressionCodecName.SNAPPY)
    .withValidation(true)
    .withDictionaryEncoding(false)
    .build();
    this.schema = schema;
    catch (IOException e)
    // TODO Auto-generated catch block
    e.printStackTrace();



    /*
    *
    */
    public static void writeToParquet(JavaRDD<Record> empRDDRecords) throws IOException

    empRDDRecords.foreach(record ->
    if(null != record && new RecordValidator().validate(record, schema).isEmpty())
    writeToParquet(record);
    // TODO collect bad records here
    );

    writer.close();








    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51892449%2fspark-java-create-parquet-avro-without-using-dataframes-of-spark-sql%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can’t save an rdd in parquet without converting it to dataframe. Rdd does not have schema but parquet file is in columnar format which needs schema, so we need to convert it to dataframe.



      You can use createdataframe api






      share|improve this answer



























        1














        You can’t save an rdd in parquet without converting it to dataframe. Rdd does not have schema but parquet file is in columnar format which needs schema, so we need to convert it to dataframe.



        You can use createdataframe api






        share|improve this answer

























          1












          1








          1







          You can’t save an rdd in parquet without converting it to dataframe. Rdd does not have schema but parquet file is in columnar format which needs schema, so we need to convert it to dataframe.



          You can use createdataframe api






          share|improve this answer













          You can’t save an rdd in parquet without converting it to dataframe. Rdd does not have schema but parquet file is in columnar format which needs schema, so we need to convert it to dataframe.



          You can use createdataframe api







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 17 '18 at 11:30









          Chandan RayChandan Ray

          1,0841211




          1,0841211























              0














              I tried this and it works like a champ...



              public class ParquetHelper

              static ParquetWriter<GenericData.Record> writer = null;
              private static Schema schema;

              public ParquetHelper(Schema schema, String pathName)

              try
              Path path = new Path(pathName);
              writer = AvroParquetWriter.
              <GenericData.Record>builder(path)
              .withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE)
              .withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE)
              .withSchema(schema)
              .withConf(new Configuration())
              .withCompressionCodec(CompressionCodecName.SNAPPY)
              .withValidation(true)
              .withDictionaryEncoding(false)
              .build();
              this.schema = schema;
              catch (IOException e)
              // TODO Auto-generated catch block
              e.printStackTrace();



              /*
              *
              */
              public static void writeToParquet(JavaRDD<Record> empRDDRecords) throws IOException

              empRDDRecords.foreach(record ->
              if(null != record && new RecordValidator().validate(record, schema).isEmpty())
              writeToParquet(record);
              // TODO collect bad records here
              );

              writer.close();








              share|improve this answer





























                0














                I tried this and it works like a champ...



                public class ParquetHelper

                static ParquetWriter<GenericData.Record> writer = null;
                private static Schema schema;

                public ParquetHelper(Schema schema, String pathName)

                try
                Path path = new Path(pathName);
                writer = AvroParquetWriter.
                <GenericData.Record>builder(path)
                .withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE)
                .withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE)
                .withSchema(schema)
                .withConf(new Configuration())
                .withCompressionCodec(CompressionCodecName.SNAPPY)
                .withValidation(true)
                .withDictionaryEncoding(false)
                .build();
                this.schema = schema;
                catch (IOException e)
                // TODO Auto-generated catch block
                e.printStackTrace();



                /*
                *
                */
                public static void writeToParquet(JavaRDD<Record> empRDDRecords) throws IOException

                empRDDRecords.foreach(record ->
                if(null != record && new RecordValidator().validate(record, schema).isEmpty())
                writeToParquet(record);
                // TODO collect bad records here
                );

                writer.close();








                share|improve this answer



























                  0












                  0








                  0







                  I tried this and it works like a champ...



                  public class ParquetHelper

                  static ParquetWriter<GenericData.Record> writer = null;
                  private static Schema schema;

                  public ParquetHelper(Schema schema, String pathName)

                  try
                  Path path = new Path(pathName);
                  writer = AvroParquetWriter.
                  <GenericData.Record>builder(path)
                  .withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE)
                  .withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE)
                  .withSchema(schema)
                  .withConf(new Configuration())
                  .withCompressionCodec(CompressionCodecName.SNAPPY)
                  .withValidation(true)
                  .withDictionaryEncoding(false)
                  .build();
                  this.schema = schema;
                  catch (IOException e)
                  // TODO Auto-generated catch block
                  e.printStackTrace();



                  /*
                  *
                  */
                  public static void writeToParquet(JavaRDD<Record> empRDDRecords) throws IOException

                  empRDDRecords.foreach(record ->
                  if(null != record && new RecordValidator().validate(record, schema).isEmpty())
                  writeToParquet(record);
                  // TODO collect bad records here
                  );

                  writer.close();








                  share|improve this answer















                  I tried this and it works like a champ...



                  public class ParquetHelper

                  static ParquetWriter<GenericData.Record> writer = null;
                  private static Schema schema;

                  public ParquetHelper(Schema schema, String pathName)

                  try
                  Path path = new Path(pathName);
                  writer = AvroParquetWriter.
                  <GenericData.Record>builder(path)
                  .withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE)
                  .withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE)
                  .withSchema(schema)
                  .withConf(new Configuration())
                  .withCompressionCodec(CompressionCodecName.SNAPPY)
                  .withValidation(true)
                  .withDictionaryEncoding(false)
                  .build();
                  this.schema = schema;
                  catch (IOException e)
                  // TODO Auto-generated catch block
                  e.printStackTrace();



                  /*
                  *
                  */
                  public static void writeToParquet(JavaRDD<Record> empRDDRecords) throws IOException

                  empRDDRecords.foreach(record ->
                  if(null != record && new RecordValidator().validate(record, schema).isEmpty())
                  writeToParquet(record);
                  // TODO collect bad records here
                  );

                  writer.close();









                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 19:08









                  ltd9938

                  9641823




                  9641823










                  answered Nov 15 '18 at 18:34









                  Sarabhaiah PolakamSarabhaiah Polakam

                  1




                  1



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51892449%2fspark-java-create-parquet-avro-without-using-dataframes-of-spark-sql%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands