frumos

frumos

Json complex document streaming parsing with Jaxon

Hello!

Given

My input is, a gz batch file having records about usage of some product by customer in json structure (legacy format and not modifiable) like:

header1=value1
header2=value2
header3=value3
header4=value4
header5=value5

{"namespace":"aggregation","validators":["someValidator"],"symbol-table":{"timestamp":"0","customer":"1","product":"2","value":"3"},"records":[{"0":"1546339235","1":"Bob","2":"apple","3":"5"},{"0":"1546339739","1":"Bob","2":"orange","3":"2"},{"0":"1546339839","1":"Alice","2":"apple","3":"5"}, {"0":"1546339839","1":"Alice","2":"orange","3":"1"}, {"0":"1546339839","1":"Alice","2":"apple","3":"2"}],"record-count":"5"}

My streaming processing function to aggregated similar records (the same customer and same product) looks like:

"/home/user/temp/rawBatch/batch.gz" 
|> File.stream! 
|> StreamGzip.gunzip 
|> Stream.map(&(String.split(&1, "\n"))) 
|> Stream.flat_map(&(&1)) 
|> Stream.drop(6) 
# I also need to get symbol-table to be able compose aggregation key (used in reduce function) dynamically
|> Jaxon.Stream.query([:root, "records", :all]) 
# hardcoded key indexes
|> Stream.map(&(%{record_key: &1["1"] <> "," <> &1["2"], record_value: Decimal.new(&1["3"])})) 
|> Enum.reduce(%{}, fn %{record_key: key, record_value: value}, acc -> Map.update(acc, key, value, & Decimal.add(&1, value)) end) 
|> Enum.for_each(write_to_file)

and it works near perfect except line

Stream.map(&(%{record_key: &1[“1”] <> “,” <> &1[“2”], record_value: Decimal.new(&1[“3”])}))

where I compose an aggregation key with hard coded indexes.

The indexes are encoded however in json’s “symbol-table” element and can vary from batch to batch.

Problem

I need to compose aggregation key with indexes obtained from symbol-table element dynamically in the stream preferably with one file processing ‘loop’

There may be such an option in Jaxon library but its documentation does not give me any clear hint on the solution.

Could you suggest please viable options to solve my task?

Thank you.

First Post!

dimitarvp

dimitarvp

Are you expecting huge files to be fed to your code?

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement