Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
zavoraad authored Mar 8, 2024
1 parent b7853b8 commit 4b216a4
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ customFormat = type("", (), dict({'SEGMENT_DELIM': '~', 'ELEMENT_DELIM': '*', 'S

```

#### EDI as a Table for SQL

```python
""""
Look at all data refernce -> https://justransform.com/edi-essentials/edi-structure/
(1) Including control header / ISA & IEA segments
"""
from pyspark.sql.functions import input_file_name

( df.withColumn("filename", input_file_name()).rdd
.map(lambda x: (x.asDict().get("filename"),x.asDict().get("value")))
.map(lambda x: (x[0], EDI(x[1])))
.map(lambda x: [{**{"filename": x[0]}, **y} for y in x[1].toRows()])
.flatMap(lambda x: x)
.toDF()).show()

"""
+--------------------+----------+--------------------------+--------------+------------+-----------------------------+--------+
| row_data|row_number|segment_element_delim_char|segment_length|segment_name|segment_subelement_delim_char|filename|
+--------------------+----------+--------------------------+--------------+------------+-----------------------------+--------+
|ISA*00* ...| 0| *| 17| ISA| :|file:///|
|GS*HC*CLEARINGHOU...| 1| *| 9| GS| :|file:///|
|ST*837*000000001*...| 2| *| 4| ST| :|file:///|
|BHT*0019*00*73490...| 3| *| 7| BHT| :|file:///|
|NM1*41*2*CLEARING...| 4| *| 10| NM1| :|file:///|
|PER*IC*CLEARINGHO...| 5| *| 7| PER| :|file:///|
|NM1*40*2*12345678...| 6| *| 10| NM1| :|file:///|
```
```python
from databricksx12.edi import *
Expand Down Expand Up @@ -103,30 +132,8 @@ ediDF.show()
+-----------------+
"""
""""
Look at all data refernce -> https://justransform.com/edi-essentials/edi-structure/
(1) Including control header / ISA & IEA segments
"""
( df.withColumn("filename", input_file_name()).rdd
.map(lambda x: (x.asDict().get("filename"),x.asDict().get("value")))
.map(lambda x: (x[0], EDI(x[1])))
.map(lambda x: [{**{"filename": x[0]}, **y} for y in x[1].toRows()])
.flatMap(lambda x: x)
.toDF()).show()
"""
Includes filename column but not shown below
+--------------------+----------+--------------------------+--------------+------------+-----------------------------+
| row_data|row_number|segment_element_delim_char|segment_length|segment_name|segment_subelement_delim_char|
+--------------------+----------+--------------------------+--------------+------------+-----------------------------+
|ISA*00* ...| 0| *| 17| ISA| :|
|GS*HC*CLEARINGHOU...| 1| *| 9| GS| :|
|ST*837*000000001*...| 2| *| 4| ST| :|
|BHT*0019*00*73490...| 3| *| 7| BHT| :|
|NM1*41*2*CLEARING...| 4| *| 10| NM1| :|
|PER*IC*CLEARINGHO...| 5| *| 7| PER| :|
|NM1*40*2*12345678...| 6| *| 10| NM1| :|
"""

# (2) Individual Transactions (Functional header) / ST & SE segments
trxDF = ( df.withColumn("filename", input_file_name()).rdd
Expand Down

0 comments on commit 4b216a4

Please sign in to comment.