Fork me on GitHub
#xtdb
<
2023-02-01
>
quan xing11:02:08

How can I get column name and column value from xtdb.

#{[:commodity/CH4 "Methane" :molecule/gas]}
I want to get #{[{:id :commondity/Ch4, :name "Methane" .. }]} like sql from db(mysql) and return the kv collection in clojure.jdbc

Hukka11:02:15

Please show the query you are doing, so it's easier to see the shape of the data too

quan xing11:02:29

(def node (xt/start-node {}))
  (xt/submit-tx
   node
   [[::xt/put
     {:xt/id :commodity/Pu
      :common-name "Plutonium"
      :type :element/metal
      :density 19.816
      :radioactive true}]

    [::xt/put
     {:xt/id :commondity/N
      :common-name "Nitrogen"
      :type :element/gas
      :density 1.2506
      :radioactive false}]

    [::xt/put
     {:xt/id :commodity/CH4
      :common-name "Methane"
      :type :molecule/gas
      :density 0.717
      :radioactive false}]])
  
     (xt/q (xt/db node) '{:find [name]
                          :where [
                                  [_ :common-name name]
                                  ]})
     ;; => #{["Methane"] ["Plutonium"] ["Nitrogen"]}
     
how can I get like this
#{[{:id  :commodity/Pu :name "Methane" }] [ {:id  :commodity/Pu :name "Plutonium" } ] [ {:id  :commodity/Pu :name "Nitrogen" }]}
return the all line data like from mysql

🙏 2
Hukka11:02:42

(->> (xt/q (xt/db node)
           '{:find [(pull e [:xt/id :common-name])]
             :where [[e :common-name name]]
             })
     (map first)
     (map #(clojure.set/rename-keys % {:xt/id :id, :common-name :name})))

Hukka11:02:54

Or

(xt/q (xt/db node)
      '{:find [id name]
        :keys [id name]
        :where [[e :common-name name]
                [e :xt/id id]]
        })
https://docs.xtdb.com/language-reference/1.23.0/datalog-queries/#return-maps

refset14:02:05

I should perhaps add you that you can use regular Clojure functions in your clauses to do things like this if really needed: [(hash-map :foo 1 :bar 2) ?map]

quan xing02:02:38

;; with just 'query':
(xt/q
 (xt/db node)
 '{:find [?uid ?name ?profession]
   :where [[?user :user/id ?uid]
           [?user :user/name ?name]
           [?user :user/profession ?profession]]})
#{[1 "Ivan" :doctor] [2 "Sergei" :lawyer], [3 "Petr" :doctor]}
How can I understand content in :where , is it a condition query? but It's not like SQL where:
select uid,name,profession from node where uid=? or etc...
What's mean by :where above code

refset04:02:42

These particular :where clauses have no conditions other than an implied existence predicate, so the effect is only unification and projection. In other words "pattern matching" for only relevant entities that hold all the attributes simultaneously (with potentially many combinations of values)

refset04:02:27

The unification of ?user here is behaving more like a self join in SQL, but I wouldn't recommend attempting to reason about Datalog in terms of SQL operations too much when trying to learn Datalog

Hukka05:02:06

Yeah. In SQL you have tables, that define a scope. You can query all rows in a table, even without knowing what columns exist. Multiple tables might have the same columns you are interested in, or be even identical, but you cannot query for the columns without knowing the table(s). In datalog you query "columns first", in a way. That is, you know at least one attribute you are interested in, and then can get all objects/documents/"rows" that have that, even if they otherwise are different. Database is not the layer that limits the schema, unless you would consider transactions functions to be that (but whenever a client chooses to insert data using a transaction function, it might as well use any other schema mechanism and a plain put).

quan xing06:02:22

ok, thanks, maybe I need to read the documentation carefully and understand the usage scenarios of xtdb

Hukka06:02:03

Perhaps I misread, but there certainly are scenarios where XTDB is especially useful, but I wouldn't think that you need to have any special needs to gain a lot of benefit compared to SQL, or especially compared to "no-SQL" databases

Hukka06:02:48

If you feel uncertain about how you can even find the data you want, when anything could be in the DB, I certainly sympathize

Hukka06:02:31

In a way, xt/attribute-stats might help there. It lets you see what kind of things you could be querying for. And of course you can always query for documents that have :xt/id, i.e. everything

Hukka06:02:57

In actual deployed code, the queries would be limited anyway. Meaning that there's no practical difference between a SQL based system inserting and querying rows in a table, and a XTDB/datalog based system putting documents, and then querying the attributes it writes

quan xing06:02:14

Thanks. • I have never known about bitemporal database. so I don't know what kind of scenarios to use it in. Because I only have a background in rdbms usage

Hukka06:02:41

If you don't have a use for it, it won't get into your way. It lets you answer questions of type "Why did the system show in January that 2022 yearly report was A, but now it's showing it's B"

quan xing06:02:22

Oh, I understand a little bit

Hukka06:02:26

In SQL, you could perhaps fix that with thinking ahead, and adding a "data_added" column in addition to the actual date column. But handling all cases becomes a hassle

refset14:02:46

Thank you for a very entertaining meetup session yesterday @mail992/@seiib3/@kevin842/@malcolmsparks/Ezra (and sorry again for the technical trouble @tomi.hukkalainen_slac!) Here's the replay for anyone interested: https://discuss.xtdb.com/t/video-virtual-meetup-1-recording-modelling-and-tradeoffs-discussion/133 - and https://discuss.xtdb.com/t/ann-virtual-meetups-in-2023/129 about registering for the session next week 🙏

🙏 2
richiardiandrea18:02:42

Hey XTDB folks! Is there any recommendation for storing a large (XML) payload in XTDB? Do I need extra compression or nippy will take care of it for me? I don't need to query it, it is basically just a blob to hydrate and store as is.

refset21:02:06

Hey @U0C8489U6 I hope all is well 🙂 I just posted your question onto Discuss and answered it there https://discuss.xtdb.com/t/will-large-xml-blobs-get-compressed/135 Funnily enough it also got discussed during the meetup lightly (though not XML specifically)!

❤️ 2
richiardiandrea21:02:08

All is well 🙂 I am watching the video as we speak 😉 Thank you!

🙌 2
tatut05:02:42

one thing to note, if you store it as a string, then it will bloat your lucene as well (if you have lucene indexing)

4
tatut05:02:47

I think this is a common question not just in XTDB but all databases… in basically every application I’ve had to contend with the “should I put files in the db vs external storage and store metadata”, in the cloud era I think S3 (and equivalents) is the usual solution

💯 4