Giter Club home page Giter Club logo

Comments (5)

yruslan avatar yruslan commented on September 26, 2024 1

Hi,

For the example above this might work:

spark.read.format("cobol")
  .option("copybook_contents", copybookContentsStr)
  .option("encoding", "ascii")
  .option("record_format", "F") // Fixed length
  .option("record_length", "426") // Record length encoded in the copybook
  .option("file_start_offset", "100") // skip file header
  .option("file_end_offset", "100") // skip file footer
  .load(pathToData)

But you also need the copybook for your record payload that might look like:

      01 RECORD.
           10 FIELD1    PIC X(10).
           10 FIELD2    PIC X(15).
...

Remember that first characters in each line of the copybook should be spaces.

from cobrix.

jaysara avatar jaysara commented on September 26, 2024

I just got another requirements added to the above, if this is considered a new question, I can create a new question. There are scenarios where record length can vary and it is presented by the first 4 bytes... Example is below, (there is no LF or CR character to separate the line)
Header -- size 100 bytes.
0426xxxxmyrecorddataupto426bytes
0456xxxxxmyrecorddataupto456bytes
0435xxxxxmyrecorddataupto435bytes
Trailer - size 100 bytes (optional)

Does Cobrix support this kind of variable length format ? What options should I use for that.

from cobrix.

yruslan avatar yruslan commented on September 26, 2024

With the new requirement the record format is now 'V', which is 'variable length. You can specify the field in the copybook that contains the length:

spark.read.format("cobol")
  .option("copybook_contents", copybookContentsStr)
  .option("encoding", "ascii")
  .option("record_format", "V") // Variable length records
  .option("record_length_field", "RECORD_LENGTH_FIELD")
  .option("file_start_offset", "100") // skip file header
  .option("file_end_offset", "100") // skip file footer
  .load(pathToData)

The copybook should define the first 4 bytes as a numeric field:

      01 RECORD.
           10 RECORD_LENGTH_FIELD  PIC 9(4).
           10 FIELD1    PIC X(10).
           10 FIELD2    PIC X(15).
...

The record length by default is the full record payload. If the value in the field does not match the record length exactly, you can use an arithmetic expression, for instance:

  .option("record_length_field", "RECORD_LENGTH_FIELD + 4")

from cobrix.

jaysara avatar jaysara commented on September 26, 2024

Ok. Thanks. In the above copybook example you gave, the lenght of field is defined by "RECORD_LENGTH_FIELD PIC 9(4)" The FIELD1 and FIELD2 length will depend upon the value in RECORD_LENGTH_FIELD. For every record it can be different based on the value in RECORD_LENGTH_FIELD. In that case, PIC X(10) and PIC X(15) may not be true all the time.. My structure will be like this,
``

01 RECORD.
           10 RECORD_LENGTH_FIELD  PIC 9(4).
           10 BASE_SEGMENT    PIC X(???). ** The size of this will come from RECORD_LENGTH_FIELD above.

How should I define the contents for copybook for that usecase. I have used any arbitary number in the above number and I was able to process the file successfully using Cobrix datasource. The sample is https://github.com/jaysara/spark-cobol-jay/blob/main/src/main/java/com/test/cobol/FixedWidthApp.java

from cobrix.

yruslan avatar yruslan commented on September 26, 2024

Since each record type probably has a different schema, your data can be considered multisegment. In this case you can define a redefined group for each segment. So the copybook will look like this:

      01 RECORD.
           10 RECORD_LENGTH_FIELD  PIC 9(4).
             15 SEGMENT1.
                20 SEG1_FIELD1    PIC X(15).
                20 SEG1_FIELD2    PIC X(10).
             15 SEGMENT2 REDEFINES SEGMENT1.
                20 SEG2_FIELD1    PIC X(5).
                20 SEG2_FIELD2    PIC X(11).

(note that SEGMENT2 redefines SEGMENT1)

You can also apply automatic segment filtering based on record length, like this: https://github.com/AbsaOSS/cobrix?tab=readme-ov-file#automatic-segment-redefines-filtering
You can use the record length field as the segment id.

from cobrix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.