文本数据增量导入到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 ...
随机推荐
- css定位的理解
在CSS中关于定位的内容是:position:relative | absolute | static | fixed ● static 没有特别的设定,遵循基本的定位规定,不能通过z-index进行 ...
- mac 安装 pymssql 报错
报错信息 _mssql.c:346:10: fatal error: 'sqlfront.h' file not found #include "sqlfront.h" ^~~~~ ...
- Invoking destroy method 'close' on bean with name 'dataSource'
Invoking destroy method 'close' on bean with name 'dataSource' Spring与Mybatis整合时出现的问题,找了一晚上结果是一个属性写错 ...
- PHP面试 PHP基础知识 三(运算符)
PHP运算符 PHP的运算符的错误控制符@ PHP支持一个错误运算符:@.当将其放在一个PHP表达式之前,该表达式可能产生的任何错误信息都将会被忽略掉. PHP运算符 运算符的优先级 着重记忆运算符 ...
- 非JAVA客户端与mina使用 PrefixedStringCodecFactory 通讯
与C++,C#不同,java的写入字节顺序是从高到低(左低到右高) 例如 内存数据:{ 0x67,0x45,0x23,0x01} ,java int值是:0x6745231 而C++是:0x1234 ...
- ajax - getJSON() 方法
$("body").on("click",".layui-input-inline:eq(3)",function(){ $(this).f ...
- Python 操作excel常见异常
一.使用xlrd模块读取excel: 1.报错:IndexError: list index out of range,如下图 解决方法:reading_sheet.cell(1,0).value中c ...
- 10 个优秀的JavaScript开发框架
阅读目录 TouchStone.js Meteor Knockout Enyo.js Reactive-coffee ExtJS Aurajs Cappuccino canjs Feathersjs ...
- htons(), ntohl(), ntohs(),htons()这4个函数(摘)
在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl(), ntohs(),htons()这4个函数.网络字节顺序与本地字节顺序之间的转换 ...
- ubuntu安装WPS替代office
安装 1.下载地址:http://community.wps.cn/download/(去WPS官网下载) 下载第一个即可 2.执行安装命令: sudo dpkg -i wps-office_10.1 ...