mysql中blob数据处理方式
程序员文章站
2022-07-02 19:47:02
具体代码如下所示:
package epoint.mppdb_01.h3c;
import java.io.file;
import java.io.file...
具体代码如下所示:
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数据处理方式,希望对大家有所帮助
上一篇: 梁山的覆灭真的是偶然吗?真相是什么
下一篇: 你这死老头子