您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)mysql中怎么處理blob數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
具體代碼如下所示:
package epoint.mppdb_01.h4c; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class MySQLblobToMPPphoto { // MySQL連接 public static Connection getMySQLConnection() throws Exception { String MySQLDRIVER = "com.mysql.jdbc.Driver"; String MySQLURL = "jdbc:mysql://192.168.186.13:3306/bigdata_scene03_rktj"; String MySQLUSERNAME = "root"; String MySQLPASSWORD = "Gepoint"; Connection MySQLconn = DriverManager.getConnection(MySQLURL, MySQLUSERNAME, MySQLPASSWORD); return MySQLconn; } // MPP連接 public static Connection getMPPConnection() throws Exception { String MPPDRIVER = "com.MPP.jdbc.Driver"; String MPPURL = "jdbc:MPP://192.168.186.14:5258/bigdata_scene03_rktj"; String MPPUSERNAME = "mpp"; String MPPPASSWORD = "h4c"; Connection MPPconn = DriverManager.getConnection(MPPURL, MPPUSERNAME, MPPPASSWORD); return MPPconn; } // public static void getMySQLblobToHDFS() throws Exception { Connection conn = getMySQLConnection(); ResultSet rs = null; try { String sql = "select ROW_ID,photo from t_rk_baseinfo_blob limit 10"; Statement prest = conn.prepareStatement(sql); rs = prest.executeQuery(sql); while (rs.next()) { int row_id = rs.getInt(1); Blob photo = rs.getBlob(2); System.out.println(row_id + " " + photo); InputStream in = photo.getBinaryStream(); OutputStream out = new FileOutputStream("H:/photo/" + row_id + ".jpg"); int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } upload("H:/photo/" + row_id + ".jpg"); } prest.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } finally { // 關(guān)閉連接 if (conn != null) { try { conn.close(); conn = null; } catch (Exception e) { e.printStackTrace(); } } } } public static void main(String[] args) throws Exception { getMySQLblobToHDFS(); } // HDFS附件上傳 public static void upload(String uploadpath) throws Exception { Configuration conf = new Configuration(); URI uri = new URI("hdfs://192.168.186.14:8020"); FileSystem fs = FileSystem.get(uri, conf, "HDFS"); Path resP = new Path(uploadpath); Path destP = new Path("/photo"); if (!fs.exists(destP)) { fs.mkdirs(destP); } fs.copyFromLocalFile(resP, destP); fs.close(); System.out.println("***********************"); System.out.println("上傳成功!"); } // HDFS附件下載 public static void download() throws Exception { Configuration conf = new Configuration(); String dest = "hdfs://192.168.186.14:/photo/11.png"; String local = "D://11.png"; FileSystem fs = FileSystem.get(URI.create(dest), conf, "hdfs"); FSDataInputStream fsdi = fs.open(new Path(dest)); OutputStream output = new FileOutputStream(local); IOUtils.copyBytes(fsdi, output, 4096, true); System.out.println("***********************"); System.out.println("下載成功!"); } }
看完上述內(nèi)容,你們對mysql中怎么處理blob數(shù)據(jù)有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。