• 售前

  • 售后

热门帖子
入门百科

mysql中blob数据处置惩罚方式

[复制链接]
身无分文的少年 显示全部楼层 发表于 2021-10-26 12:19:49 |阅读模式 打印 上一主题 下一主题
具体代码如下所示:
  1. package epoint.mppdb_01.h3c;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.URI;
  8. import java.sql.Blob;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.Statement;
  13. import org.apache.commons.net.ftp.FTPClient;
  14. import org.apache.commons.net.ftp.FTPReply;
  15. import org.apache.hadoop.conf.Configuration;
  16. import org.apache.hadoop.fs.FSDataInputStream;
  17. import org.apache.hadoop.fs.FileSystem;
  18. import org.apache.hadoop.fs.Path;
  19. import org.apache.hadoop.io.IOUtils;
  20. public class MySQLblobToMPPphoto {
  21.   // MySQL连接
  22.   public static Connection getMySQLConnection() throws Exception {
  23.     String MySQLDRIVER = "com.mysql.jdbc.Driver";
  24.     String MySQLURL = "jdbc:mysql://192.168.186.13:3306/bigdata_scene03_rktj";
  25.     String MySQLUSERNAME = "root";
  26.     String MySQLPASSWORD = "Gepoint";
  27.     Connection MySQLconn = DriverManager.getConnection(MySQLURL, MySQLUSERNAME, MySQLPASSWORD);
  28.     return MySQLconn;
  29.   }
  30.   // MPP连接
  31.   public static Connection getMPPConnection() throws Exception {
  32.     String MPPDRIVER = "com.MPP.jdbc.Driver";
  33.     String MPPURL = "jdbc:MPP://192.168.186.14:5258/bigdata_scene03_rktj";
  34.     String MPPUSERNAME = "mpp";
  35.     String MPPPASSWORD = "h3c";
  36.     Connection MPPconn = DriverManager.getConnection(MPPURL, MPPUSERNAME, MPPPASSWORD);
  37.     return MPPconn;
  38.   }
  39.   //
  40.   public static void getMySQLblobToHDFS() throws Exception {
  41.     Connection conn = getMySQLConnection();
  42.     ResultSet rs = null;
  43.     try {
  44.       String sql = "select ROW_ID,photo from t_rk_baseinfo_blob limit 10";
  45.       Statement prest = conn.prepareStatement(sql);
  46.       rs = prest.executeQuery(sql);
  47.       while (rs.next()) {
  48.         int row_id = rs.getInt(1);
  49.         Blob photo = rs.getBlob(2);
  50.         System.out.println(row_id + " " + photo);
  51.         InputStream in = photo.getBinaryStream();
  52.         OutputStream out = new FileOutputStream("H:/photo/" + row_id + ".jpg");
  53.         int len = 0;
  54.         byte[] buffer = new byte[1024];
  55.         while ((len = in.read(buffer)) != -1) {
  56.           out.write(buffer, 0, len);
  57.         }
  58.         upload("H:/photo/" + row_id + ".jpg");
  59.       }
  60.       prest.close();
  61.       rs.close();
  62.     } catch (Exception e) {
  63.       e.printStackTrace();
  64.     } finally {
  65.       // 关闭连接
  66.       if (conn != null) {
  67.         try {
  68.           conn.close();
  69.           conn = null;
  70.         } catch (Exception e) {
  71.           e.printStackTrace();
  72.         }
  73.       }
  74.     }
  75.   }
  76.   public static void main(String[] args) throws Exception {
  77.     getMySQLblobToHDFS();
  78.   }
  79.   // HDFS附件上传
  80.   public static void upload(String uploadpath) throws Exception {
  81.     Configuration conf = new Configuration();
  82.     URI uri = new URI("hdfs://192.168.186.14:8020");
  83.     FileSystem fs = FileSystem.get(uri, conf, "HDFS");
  84.     Path resP = new Path(uploadpath);
  85.     Path destP = new Path("/photo");
  86.     if (!fs.exists(destP)) {
  87.       fs.mkdirs(destP);
  88.     }
  89.     fs.copyFromLocalFile(resP, destP);
  90.     fs.close();
  91.     System.out.println("***********************");
  92.     System.out.println("上传成功!");
  93.   }
  94.   // HDFS附件下载
  95.   public static void download() throws Exception {
  96.     Configuration conf = new Configuration();
  97.     String dest = "hdfs://192.168.186.14:/photo/11.png";
  98.     String local = "D://11.png";
  99.     FileSystem fs = FileSystem.get(URI.create(dest), conf, "hdfs");
  100.     FSDataInputStream fsdi = fs.open(new Path(dest));
  101.     OutputStream output = new FileOutputStream(local);
  102.     IOUtils.copyBytes(fsdi, output, 4096, true);
  103.     System.out.println("***********************");
  104.     System.out.println("下载成功!");
  105.   }
  106. }
复制代码
总结
以上所述是小编给各人先容的mysql中blob数据处理方式,渴望对各人有所资助,假如各人有任何疑问请给我留言,小编会实时回复各人的。在此也非常感谢各人对草根技术分享网站的支持!

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作