How to process blob data in MySQL

How to process blob data in MySQL

The specific code is as follows:

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 connection 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 connection 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
      // Close the connection if (conn != null) {
        try {
          conn.close();
          conn = null;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
  public static void main(String[] args) throws Exception {
    getMySQLblobToHDFS();
  }
  //HDFS attachment upload 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("Upload successful!");
  }
  //HDFS attachment download 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("Download successful!");
  }
}

Summarize

The above is the blob data processing method in MySQL that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • The difference between TEXT and BLOB field types in MySQL
  • Learning MYSQL in comparison with MSSQL (IV) - BLOB data type
  • Solution to the problem of inserting data into Mysql longblob field
  • What is blob? Introduction to MySQL blob size configuration
  • Mysql LONGBLOB type stores binary data (modification + debugging + sorting)
  • Java implements inserting MySQL binary files, blob type, problems encountered and solutions
  • PHP uses PDO to read large amounts of data from MySQL

<<:  In-depth understanding of the role of Vuex

>>:  Detailed installation and configuration of Subversion (SVN) under Ubuntu

Recommend

MySQL permission control details analysis

Table of contents 1. Global level 2. Database lev...

A magical MySQL deadlock troubleshooting record

background Speaking of MySQL deadlock, I have wri...

24 Practical JavaScript Development Tips

Table of contents 1. Initialize the array 2. Arra...

MySQL select results to perform update example tutorial

1. Single table query -> update UPDATE table_n...

Installation tutorial of mysql5.7.21 decompression version under win10

Install the unzipped version of Mysql under win10...

Solve nginx "504 Gateway Time-out" error

Students who make websites often find that some n...

Detailed steps for Python script self-start and scheduled start under Linux

1. Python automatically runs at startup Suppose t...

Javascript tree menu (11 items)

1. dhtmlxTree dHTMLxTree is a feature-rich Tree M...

Implementation code for infinite scrolling with n container elements

Scenario How to correctly render lists up to 1000...

SELinux Getting Started

Back in the Kernel 2.6 era, a new security system...

JavaScript to achieve progress bar effect

This article example shares the specific code of ...

A brief talk about JavaScript parasitic composition inheritance

Composition inheritance Combination inheritance i...

Encapsulate the navigation bar component with Vue

Preface: Fully encapsulating a functional module ...