Clojure 从数据库读取 Blob

发布于 2024-09-17 06:19:01 字数 474 浏览 6 评论 0原文

我需要从这个 Blob 读取字节。我正在尝试以下操作,但出现此异常: oracle.sql.BLOB 无法转换为 [B

(defn select-test2[]
  (clojure.contrib.sql/with-connection db
    (with-query-results res ["SELECT my_blob from some_table"] (doall res))))

(defn obj [byte-buffer]
  (if-not (nil? byte-buffer)
    (with-open [object-in (ObjectInputStream.
                            (ByteArrayInputStream. byte-buffer))]
      (.readObject object-in))))

(obj (:my_blob (first (select-test2))))

I need to read bytes from this Blob. I'm trying the following but I'm getting this exception:
oracle.sql.BLOB cannot be cast to [B

(defn select-test2[]
  (clojure.contrib.sql/with-connection db
    (with-query-results res ["SELECT my_blob from some_table"] (doall res))))

(defn obj [byte-buffer]
  (if-not (nil? byte-buffer)
    (with-open [object-in (ObjectInputStream.
                            (ByteArrayInputStream. byte-buffer))]
      (.readObject object-in))))

(obj (:my_blob (first (select-test2))))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法唧唧 2024-09-24 06:19:01

[B 是字节数组的“类”:

user=> (type (byte-array 0))
[B

因此,代码中的某个位置需要字节数组,但它被赋予一个 oracle.sql.Blob实例。我敢打赌 :my_blob 会给你一个 Blob;当您将 byte-buffer (即 Blob)传递给 ByteArrayInputStream 构造函数时,您会遇到异常。

查找 oracle.sql.Blob 的 javadocs 以了解如何从中提取字节数组或输入流。

[B is the "class" of a byte array:

user=> (type (byte-array 0))
[B

So there's a place in your code that is expecting a byte array, but it's being given an oracle.sql.Blob instance. My bet is that :my_blob is giving you a Blob; when you pass byte-buffer (which is the Blob) to the ByteArrayInputStream constructor, you get the exception.

Look up the javadocs for oracle.sql.Blob to see how to extract a byte array or input stream from it.

み青杉依旧 2024-09-24 06:19:01
(ns test-jdbc
  (:use clojure.contrib.sql))

; read clob with BufferedReader
(defn clob-to-string [clob]
  (with-open [rdr (java.io.BufferedReader. (.getCharacterStream clob))]
    (apply str (line-seq rdr))))

; read first CLOB 
(defn get-meta-by-id [db id]
  "read META_COL from MY_TABLE by given id"
  (with-connection db
    (transaction ;; need to be inside a transaction
      (with-query-results rs 
        ["select META_COL from MY_TABLE where ID = ? " id]
        (clob-to-string (:meta-col (first rs))) ))))
(ns test-jdbc
  (:use clojure.contrib.sql))

; read clob with BufferedReader
(defn clob-to-string [clob]
  (with-open [rdr (java.io.BufferedReader. (.getCharacterStream clob))]
    (apply str (line-seq rdr))))

; read first CLOB 
(defn get-meta-by-id [db id]
  "read META_COL from MY_TABLE by given id"
  (with-connection db
    (transaction ;; need to be inside a transaction
      (with-query-results rs 
        ["select META_COL from MY_TABLE where ID = ? " id]
        (clob-to-string (:meta-col (first rs))) ))))
深海夜未眠 2024-09-24 06:19:01
(defn blob-to-byte [blob]
    (let [ary (byte-array (.length blob))
          is (.getBinaryStream blob)]
    (.read is ary)
    (.close is)
    ary))

(j/query db/real-db ["select blob from table"]
         {:row-fn #(->> % :blob blob-to-byte)}))
(defn blob-to-byte [blob]
    (let [ary (byte-array (.length blob))
          is (.getBinaryStream blob)]
    (.read is ary)
    (.close is)
    ary))

(j/query db/real-db ["select blob from table"]
         {:row-fn #(->> % :blob blob-to-byte)}))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文