Artificial Intelligence Technology Semantic Web Technology Search Technology DataBase Technology Ontology Technology Digital Transformation Technology User Interface and DataVisualization Workflow & Services. Programming Clojure Database Technologies
In the previous article, I took a little detour and talked about the database, but this time I would like to write about the linkage between the web server and the database. The final goal is to build a UI on the client (described separately), access the server from there via HTTP, and then communicate with the database regarding the information requested by the server. In addition, by putting the machine learning results described before into the database, we can set up verification experiments for artificial intelligence systems.
Previously, we set up several servers and a database, and connected to them. This time we are going to combine them.
First, as a way to use the DB, we need to define a function to store and retrieve the data from the web.
;;db設定(前回)
(def db-spec {:dbtype "postgresql" :dbname "my-db" :host "localhost" :port 5432})
;;データベースにtableを設定
(defn migrate []
(jdbc/db-do-commands
db-spec
(ddl/create-table :my-data
[:id :serial]
[:title :varchar])))
;;データベースにデータをsave
(defn save-data [title]
(jdbc/insert! db-spec :my-data {:title title}))
;;データベースからデータを抽出
(defn find-data-all []
(jdbc/query db-spec "select * from my-data")) ;;(1)
Next, we define a function to retrieve data from the client from the server startup as follows.
(defonce server (atom nil))
;;requestからquer
(defn param-pick [msg] (:text (:params msg))) ;;requestのデータによる
(defn ans-text [msg] (find-data-all (param-pick msg))) ;;dbへqueryをかけて出力
(defn response-data [n]
{:output [{:type "text" :value (str n)}]}) ;;responseのデータによる
;;handler
(defn my-db-data [req]
(r/response (response-data (str (ans-text req)))))
;;route
(defroutes app-routes
(GET "/my-db-data" [] my-db-data)
(route/not-found "Not Found"))
(def app
(-> app-routes
wrap-json-body
wrap-json-response
(wrap-defaults site-defaults)
(wrap-cors :access-control-allow-origin [#".*"] :access-control-allow-methods [:get])))
;;servef
(defn start-server []
(when-not @server
(reset! server (server/run-jetty app {:port 3000 :join? false}))))
(defn stop-server []
(when @server
(.stop @server)
(reset! server nil)))
(defn restart-server []
(when @server
(stop-server)
(start-server)))
(defn -main []
(start-server))
For the data to be output to the client, we can adjust the function in (1), and for the data to be input to the DB, the simplest way is to assign it to a variable once.
In the next article, I would like to discuss Bayesian estimation, which is a modeling-based approach to machine learning.
コメント