Data extraction from Map

Knowledge Base Archive This article is part of the Chinoba Knowledge Base. Explore Chinoba.org →

Programming Technology  ICT Technology  Front-end development in Javascript  Web Technology  Clojure and Functional Programming  Digital Transformation

When we process data in Clojure, we often use Map data access. For example, let’s say you have a postgresql database output or a CSV data map like the one below.

(def output-text [{:id 1 :text "aaa" :tag true}
                 { :id 2 :text "bbb" :tag false}
                 { :id 3 :text "ccc" :tag true}
                 { :id 4 :text "ddd" :tag true}
                 { :id 5 :text "eee" :tag false}])

If you want to extract arbitrary :text data from this

(defn output-data [n]
  (->> output-text
     (filter #(= (:text %) n))
      first))

if we take

(output-data "ccc") 
->  {:id 3 :text "ccc" :tag true}

If you use the “re-find” function instead of the “=” function, you can do a simple full-text search as follows. If you use the “re-find” function instead of the “=” function, you can do a simple full-text search like the following.

(defn search-data [n]
       (filter #(re-find  (re-pattern n) (:text %)) output-text))

Now, for a more complex nested map structure like the output of rdf store (e.g. generated from xml like this

<root>
  <records>
    <record id="1">record01</record>
    <record id="2">record02</record>
  </records>
  <record id="3">record03</record>
</root>

Using clojure.data.zip, set the leiningen dependency as follows

[org.clojure/data.zip "1.0.0"]

The data is extracted in the following way.

(use 'clojure.data.zip.xml)
(require ['clojure.xml :as 'xml])
(require ['clojure.zip :as 'zip])

(xml-> (zip/xml-zip (xml/parse "in.xml")) :records :record text)
; -> ("record01" "record02")

(xml-> (zip/xml-zip (xml/parse "in.xml")) :record text)
; -> ("record03")

(xml-> (zip/xml-zip (xml/parse "in.xml")) :record (attr :id))
; -> ("3")

For data with a complex structure, it would be simpler to use SAPRQL or other queries to get processed data directly from the DB via the API.

Related Research

This topic is part of the Chinoba Knowledge Base.

Chinoba Research
Chinoba-lab Open Source
Books and Library

コメント

Exit mobile version
タイトルとURLをコピーしました