文本数据增量导入到mysql
实现思路:
实现Java读取TXT文件中的内容并存到内存,将内存中的数据和mysql 数据库里面某张表数据的字段做一个比较,如果比较内存中的数据在mysql 里存在则不做处理,如果不存在则将该数据插入mysql数据库中
步骤3 、内存中文本数据和读取的mysql 数据做比较
/**
* 实现读取文件信息
*
* @param fileName
* @return
*/
public static String readFileByLines(String fileName) {
String result = "";
file = new File(fileName);
String tempString = null;
BufferedReader reader = null;
try { reader = new BufferedReader(new FileReader(file));
while ((tempString = reader.readLine()) != null) {
result += tempString + "\n";
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return result;
}
public static String url = null;
public static String username = null;
public static String password = null;
public static Connection conn;
public static Statement stmt;
public static ResultSet rs;
public static String fileName;
public static String tempString = null; public static String PATH = "/dbconfig.properties";
private static Properties properties;
static {
try {
InputStream is = DBlUtils.class.getResourceAsStream(PATH);
properties = new Properties();
properties.load(is);
url = properties.getProperty("jdbc.url");
username = properties.getProperty("jdbc.username");
password = properties.getProperty("jdbc.password");
fileName = properties.getProperty("fileName");
System.out.println("fileName:" + fileName);
if (is != null)
is.close();
} catch (IOException e) { e.printStackTrace();
} }
查询mysql 数据库数据
/**
* 查询mysql 数据库数据,并获得内容
*
* @param sql
*/
public static String queryDatas(String sql) {
String queryDatas = null;
try {
conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
stmt = conn.prepareStatement("load data local infile '' " + "into table loadtest fields terminated by ','");
StringBuilder sb = new StringBuilder();
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
((com.mysql.jdbc.Statement) stmt).setLocalInfileInputStream(is);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
// 输出列名
for (int i = 1; i <= columnCount; i++) {
if (rsmd.getColumnName(i).startsWith("content")) {
System.out.print(rsmd.getColumnName(i));
}
}
System.out.println();
// 输出mysql数据
while (rs.next()) {
queryDatas = rs.getString(2);
}
} catch (Exception e) {
e.printStackTrace();
}
return queryDatas;
}
文本读取的数据和mysql查询的数据进行比较:
文本数据增量导入到mysql的更多相关文章
- 【实战】使用 Kettle 工具将 mysql 数据增量导入到 MongoDB 中
最近有一个将 mysql 数据导入到 MongoDB 中的需求,打算使用 Kettle 工具实现.本文章记录了数据导入从0到1的过程,最终实现了每秒钟快速导入约 1200 条数据.一起来看吧~ 一.K ...
- 第3节 sqoop:6、sqoop的数据增量导入和数据导出
增量导入 在实际工作当中,数据的导入,很多时候都是只需要导入增量数据即可,并不需要将表中的数据全部导入到hive或者hdfs当中去,肯定会出现重复的数据的状况,所以我们一般都是选用一些字段进行增量的导 ...
- Python读取Excel中的数据并导入到MySQL
""" 功能:将Excel数据导入到MySQL数据库 """ import xlrd import MySQLdb # Open the w ...
- 用C#实现的两个试验编程(txt文本数据的导入,和数据导出为TXT)
1. 文件的存取 数据文件1.txt为学生成绩统计表,要对它做一个统计工作.文件中若一个人有多个成绩,则取他们的最好成绩来统计,然后计算全班平均成绩:统计0-69.70-79.80-89.90-100 ...
- 在windows下,将mysql离线数据文件导入本地mysql数据库
1. 查看mysql路径 SELECT @@basedir AS basePath FROM DUAL 其实mysql5.6 的数据文件在 C:\ProgramData\MySQL\MySQL Ser ...
- Sqoop(四)增量导入、全量导入、减量导入
增量导入 一.说明 当在生产环境中,我们可能会定期从与业务相关的关系型数据库向Hadoop导入数据,导入数仓后进行后续离线分析.这种情况下我们不可能将所有数据重新再导入一遍,所以此时需要数据增量导入. ...
- 如何用java POI将word中的内容导入到mysql数据库中
由于作业需要,要求我们将word文档中的数据直接导入到mysql中,在网上找了很常时间,终于将其解决. 由于比较初级,所以处理的word文档是那种比较规范的那种,条例比较清晰,设计的思路也比较简单,就 ...
- 大数据之路week07--day07 (Sqoop 从mysql增量导入到HDFS)
我们之前导入的都是全量导入,一次性全部导入,但是实际开发并不是这样,例如web端进行用户注册,mysql就增加了一条数据,但是HDFS中的数据并没有进行更新,但是又再全部导入一次又完全没有必要. 所以 ...
- shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中
shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...
随机推荐
- express简易代理请求
var express = require('express') var app = express() var proxy = require('http-proxy-middleware') va ...
- STM32嵌入式开发学习笔记(二):将功能封装为库文件
将所有的函数都堆在main.c文件里不是好的选择,庞大的代码文件会是你维护的障碍,明智的做法是,一种功能封装到一个库文件里. 库文件就是你代码开始部分写的#include<xxxx.h>里 ...
- shell 检查文件夹是否包含文件,或者只是空文件
empty_dir_check(){ check_dir=$ if [ -d $check_dir ];then file_list=` -maxdepth -type f` if [ $file_l ...
- centos7升级php5.4到php5.6
history命令历史 8 yum provides php #自带的只有5.4版本 9 rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-relea ...
- vue中:key 和react 中key={} 的作用,以及ref的特性?
vue中:key 和react 中key={} 为了给 vue 或者react 一个提示,以便它能跟踪每个节点的身份,从而重用和重新排序现有元素,你需要为每项提供一个唯一 key 属性 一句话概括就是 ...
- if控制器
因为比较的是字符串,所以要在两边加双引号哦
- Use on Git
Preface The document is about to introduce some specialties on PLM development and mainte ...
- cpu相关信息查看
查看CPU型号:grep "model name" /proc/cpuinfo | uniqdmidecode -s processor-version | uniq 查看物理CP ...
- laravel 关掉debug
修改.env文件 APP_DEBUG=false 然后把Laravel服务重启一下
- 强大的httpClientUtils
<!-- https://mvnrepository.com/artifact/com.arronlong/httpclientutil --> <dependency> &l ...