clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
2020-11-03T03:16:36.061400Z

I'm trying to transform a collection of data items into XML. The items each represent one instance of one of about 500 different classes, which will be read into a Java program via JAXB (what can I say, this system was designed in the early 2000's). I have the .xsd files for these classes. Is there any way to easily import these .xsd's into clojure, turning the types described therein into corresponding records? Similarly, I need to also do the reverse process - reading the XML data objects into Clojure. Again, is there an easy way to do this? Alternately, is there a simple JAXB wrapper that I can use for these operations?

Ed 2020-11-03T19:18:23.069400Z

I have had some success with clojure.data.zip in the past (https://github.com/clojure/data.zip) but it seems there's this which looks newer https://github.com/clojure/data.xml and may be more what you're looking for? Writing a custom JAXB handler probably isn't much work though ...

Ed 2020-11-03T19:19:30.069800Z

equally though, you should be able to generate the java classes, and use them directly

Ed 2020-11-03T19:20:37.070Z

what I'd recommend you do probably depends on the transformations you want to make 😉

2020-11-04T20:42:27.132600Z

Thank you all. I'm considering the suggestions you've all given. They've been helpful.

valerauko 2020-11-03T05:19:00.061800Z

thanks!

valerauko 2020-11-03T05:19:45.062Z

> Any errors are my own i love this comment

suren 2020-11-03T05:50:45.064100Z

Hi guys I wrote a clojure package for composing sql statement. I have been using it for my internal project and I am happy with it. The main reason being, it has lower learning curve and I get to write mostly sql statements. Check it out here https://github.com/ludbek/sql-compose The package has not been published yet due to following artifact issue during deploy https://stackoverflow.com/questions/64654868/deploy-clojure-packages-to-clojar Cheers

suren 2020-11-04T02:06:11.085600Z

The issue has been fixed. Not sure what went wrong with lein. Instead of using lein I used mvn to deploy the package. The blog post below was helpful. https://oli.me.uk/clojure-projects-from-scratch/

phronmophobic 2020-11-03T05:59:36.064600Z

did you run lein jar first?

suren 2020-11-03T06:00:47.064800Z

nope I didnt I did this time and I still get the same error

phronmophobic 2020-11-03T06:05:14.065200Z

are you using up to date versions of lein and clojure?

suren 2020-11-03T06:10:24.065400Z

$ lein --version
Leiningen 2.9.4 on Java 10.0.1 Java HotSpot(TM) 64-Bit Server @vm 
Clojure version is 1.10.1

phronmophobic 2020-11-03T06:11:56.065800Z

i'm not actually sure what the issue is. from the error message it seems like it's having trouble finding files to deploy. is your project.clj file shareable?

suren 2020-11-03T07:39:55.066Z

Here is the link to it https://github.com/ludbek/sql-compose/blob/main/project.clj

Jovannie Landero 2020-11-03T19:46:15.070500Z

what channel should i use for questions regarding generating csv files?

2020-11-03T19:47:07.070800Z

here should be fine, are you using clojure.data.csv?

Jovannie Landero 2020-11-03T19:48:14.071800Z

yes. i'm trying to generate a csv with a large amount of data. my function works fine with smaller datasets but gets stuck when dealing with a large dataset. was wondering if anyone ran into this issue using clojure.data.csv

2020-11-03T19:50:05.072200Z

it just delegates to the .write method of the writer - https://github.com/clojure/data.csv/blob/master/src/main/clojure/clojure/data/csv.clj#L105

ghadi 2020-11-03T19:50:35.073Z

Paste your csv writing code @jovannie.landero396

Jovannie Landero 2020-11-03T19:51:49.073600Z

(defn maps->csv
  "Converts list of hash-maps to CSV."
  [file data]
  (let [filename (str "resources/csvs/" file "-" (time/instant) ".csv")
        headers (->> data
                     first
                     keys
                     (map name)
                     (map str/capitalize))
        row-data (map vals data)]
    (with-open [writer (io/writer filename)]
      (csv/write-csv writer (cons headers row-data)))))

2020-11-03T20:07:47.075500Z

I believe such code will cause the entire sequence row-data to be realized in memory all at once, with none of it beginning elements GC'able garbage, because of the row-data reference to the head of the list. If all of that data is large compared to your JVM's max heap size, that will cause it to go slower and slower, GC'ing more frequently, until eventually it could cause an OutOfMemory exception.

2020-11-03T20:09:31.077200Z

I also believe there are small variations of your code that should avoid "holding onto the head", e.g. do not declare row-data at all, and use the expression (map vals data) instead of the one place where row-data occurs now. Note that if data is a lazy sequence and its head is held onto outside of the function map->csv, then it could also end up consuming lots of non-GC'able memory.

2020-11-03T20:09:59.077800Z

(non GC'able until some later time, when your code no longer keeps a reference to the beginning of such a sequence)

2020-11-03T20:16:30.078400Z

I thought locals clearing was smart about escape

Jovannie Landero 2020-11-03T20:18:01.079700Z

hm yep data is a lazy sequence as well and i believe its head is held onto outside of the function but just to make sure we're on the same page.. what do you mean by its head is held onto outside of the function?

2020-11-03T20:18:01.079800Z

eg. this runs forever but doesn't increase heap usage

user=> (let [r (range)] (run! identity r))

2020-11-03T20:18:39.080400Z

@jovannie.landero396 if there's something outside maps->csv that holds data, that means the realized values can't be garbage collected

2020-11-03T20:18:43.080600Z

so it's a potential heap bomb

2020-11-03T20:19:32.081300Z

@jovannie.landero396 anyway, this should be easy to test, with eg. visualvm or yourkit attached to your process, look for heap usage, which methods are being called etc.

Jovannie Landero 2020-11-03T20:25:15.081900Z

ok let me test

Jovannie Landero 2020-11-03T20:50:12.082400Z

so the amount of memory being used it 54.7 MiB without running it through that function