一.导入MySQL数据库中
(1)通过Navicat导入向导直接导入即可
(2)代码方式导入
【如果字段太多,这种方式做肯定不现实】
csv导入mysql数据库 -- 按需修改即可
package com.blb.mysql_java_es.util; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.io.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class CsvToMysql { static { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { } } //从csv中去读取数据 public void readData() throws IOException, SQLException { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/temp1?characterEncoding=utf8&serverTimezone=GMT", "root", "root"); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("F://java_ES//112.csv"),"GBK")); String line = null; String sql = "insert into tb_temp1 values(?,?,?,?,?,?,?,?)"; PreparedStatement preparedStatement = conn.prepareStatement(sql); int i = 1; while ((line = reader.readLine())!= null){ String[] split = line.split(","); preparedStatement.setString(1,split[0]); preparedStatement.setString(2,split[1]); preparedStatement.setString(3,split[2]); preparedStatement.setString(4,split[3]); preparedStatement.setString(5,split[4]); preparedStatement.setString(6,split[5]); preparedStatement.setString(7,split[6]); preparedStatement.setString(8,split[7]); preparedStatement.addBatch(); if (i % 100 == 0){ preparedStatement.executeBatch(); preparedStatement.clearBatch(); } i++; } preparedStatement.executeBatch(); preparedStatement.clearBatch(); preparedStatement.close(); conn.close(); } public static void main(String[] args) throws IOException, SQLException { new CsvToMysql().readData(); } }
xls导入mysql数据库 -- 按需修改即可
package com.blb.mysql_java_es.util; import com.alibaba.excel.EasyExcel; import com.blb.mysql_java_es.listener.UserListener; import com.blb.mysql_java_es.pojo.User; import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * 最简单的读: * 1.创建excel对应的实体对象 * 2.由于默认的是一行一行的读取excel,所以需要创建一个excel一行一行的回调监听器 * 3.直接调用即可 */ public class ExcelToMysql { static { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //获取连接 public static Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/temp1?characterEncoding=utf8&serverTimezone=GMT", "root", "root"); } catch (SQLException e) { e.printStackTrace(); } return conn; } //批量保存 public static void batchRead(List<User> userList){ Connection conn = getConnection(); String sql = "insert into tb_temp1 values(?,?,?,?,?,?,?,?)"; try { PreparedStatement preparedStatement = conn.prepareStatement(sql); for (User user:userList) { preparedStatement.setString(1,user.getUid()+""); preparedStatement.setString(2,user.getUname()); preparedStatement.setString(3,user.getUsex()); preparedStatement.setString(4,user.getUage()); preparedStatement.setString(5,user.getAddress()); preparedStatement.setString(6,user.getJob()); preparedStatement.setString(7,user.getJobyear()); preparedStatement.setString(8,user.getSkill()); preparedStatement.addBatch(); } preparedStatement.executeBatch(); preparedStatement.clearBatch(); preparedStatement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void readData() throws SQLException { //指定需要去哪个class去读,然后读取到第一个sheet文件流就会自动关闭 EasyExcel.read(new File("F://java_ES//114.xls"), User.class,new UserListener(new ArrayList<User>())).sheet().headRowNumber(0).doRead(); } public static void main(String[] args) { try { readData(); } catch (SQLException e) { e.printStackTrace(); } } }
二.导入ES数据库中
/** * 如何读取XLS文件中的数据,可以通过Kettle工具直接做 * * 读取xls的一些技术:-- 可以读取xls的数据,也可以导出为xls格式的数据 * 阿里巴巴 easyexcel * 韩国人写的 jxls * apache POI(原生) * * 我们使用的是easyexcel,简单易操作 */ /** * 推荐阅读: * https://github.com/alibaba/easyexcel/ * * 使用最简单的easyexcel读取数据 * 由于是默认一行一行的读取excel的,所以需要创建一个excel一行一行的回调监听器 */ //批量从xls表中添加数据 -- 单条插入,数据量大效率就很低 @Test void insert() throws IOException { //文件的路径 String fileName = "F:\企查查\test.xls"; //需要指定去哪个excel中读取,然后读取到第一个sheet之后文件流就会自动关闭 /** * new File(fileName):需要读取的文件 * User.class:读出来的数据需要绑定哪一个类 * new UserListener():读出来的数据需要如何处理 */ ArrayList<User> users = new ArrayList<>(); EasyExcel.read(new File(fileName),User.class, new UserListener(users)).sheet().doRead(); int i = 2; for (User user:users){ System.out.println(user); //User(uid=1, uname=张三, upwd=123456) insertData1("test","std",i+"",user); i++; } } //从xsl中批量插入数据 -- 对于海量数据效率非常高 @Test void batchInsert() throws IOException { //文件的路径 String fileName = "F:\企查查\test.xls"; ArrayList<User> users = new ArrayList<>(); EasyExcel.read(new File(fileName),User.class,new UserListener(users)).sheet().doRead(); batchInsert1("test","std",users,16); } public void insertData1(String index, String type, String id, User user) throws IOException { IndexRequest request = new IndexRequest(index).type(type).id(id); request.source(JSON.toJSONString(user),XContentType.JSON); IndexResponse index1 = restHighLevelClient.index(request, RequestOptions.DEFAULT); } /** * * @param index 索引数据库的名字 * @param type 文档类型 * @param userList 批处理的数据 * @param i 起始id */ public void batchInsert1(String index, String type, List<User> userList,int i) throws IOException { BulkRequest bulkRequest = new BulkRequest(index,type); for (User user:userList){ IndexRequest request = new IndexRequest(); request.id(i+""); request.source(JSON.toJSONString(user),XContentType.JSON); bulkRequest.add(request); i++; } restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT); }