兴国资源网 Design By www.nnzcdc.com
具体代码如下所示:
package epoint.mppdb_01.h3c; 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 = "h3c"; 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 { // 关闭连接 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("下载成功!"); } }
总结
以上所述是小编给大家介绍的mysql中blob数据处理方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
兴国资源网 Design By www.nnzcdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
兴国资源网 Design By www.nnzcdc.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]