一、相关说明

这篇博客没什么大的营养,自己记下来以防自己忘记。自己之前写程序读文本,对其解析后存入数据库。刚开始,程序是单线程的,程序效率低下:读70个文本入库(约20G)需要30多个小时。后来,自己将其改为多线程(五个),效率提升了好几倍。
本程序采用一个数据库连接和多个文件句柄的形式。

二、代码

package xiamengps.connect;
import oracle.liesmars.stcode.coder.*;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Date;
import java.util.Locale;import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;
import oracle.kv.table.ArrayValue;
import oracle.kv.table.Row;
import oracle.kv.table.Table;
import oracle.kv.table.TableAPI;
// 13--14
public class connect implements Runnable{private int rowID=0;private String host="202.114.118.126:5000";private KVStoreConfig kvconfig=new KVStoreConfig("xiamen",host);private KVStore kvstore=KVStoreFactory.getStore(kvconfig);TableAPI tableH=kvstore.getTableAPI();Table ParentTable=tableH.getTable("st_objectstates_xiamengps1");Table ChildTable=tableH.getTable("st_objectstates_xiamengps1.attribute1");String folderPath="D:\\Wuhan_University_Oracle\\data\\1";String encoding="utf-8";String[] filePaths=getTxtPaths(folderPath);private int i=-1;public void run(){try{for(;i<filePaths.length;){i++;/<span style="color:#ff0000;"><strong>/循环放在这里是为了防止两个及以上线程读同一个文本</strong></span>System.out.println("开始读取第"+i+"个文本...");File file=new File(filePaths[i]);if(file.isFile() && file.exists()){InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//鑰冭檻鍒扮紪鐮佹牸寮�           BufferedReader bufferedReader = new BufferedReader(read);String lineTxt = null;while((lineTxt = bufferedReader.readLine()) != null){rowID++;                    String []fields=lineTxt.split(",");Row Prow =ParentTable.createRow();Row Crow=ChildTable.createRow();constructRow(fields,Prow,Crow,rowID);if(Prow==null||Crow==null){//rowID--;continue;}tableH.put(Prow,null,null);tableH.put(Crow,null,null);}read.close();}System.out.println("读取第"+i+"个文本结束!");}System.out.println("存储任务结束!");}catch(Exception e){CommanFunction.printException(e);return;}}public static void main(String[] args) throws IOException {connect c=new connect();new Thread(c).start();new Thread(c).start();new Thread(c).start();new Thread(c).start();new Thread(c).start();}private static void constructRow(String []fields,Row Prow,Row Crow,int rowID){    int fieldsLength=fields.length;Prow.put("rowID",rowID);Prow.put("objectID",fields[0].trim());long time=CommanFunction.DateTimeToMilliSec(fields[1]);Prow.put("start_time",time);Prow.put("end_time",time);// 子表里也要put父表的主键,这样做是为了父表的记录与子表的记录一一对应。Crow.put("rowID", rowID);Crow.put("objectID",fields[0].trim());Crow.put("ID",rowID);Crow.put("CTELNUM",fields[2]);Crow.put("DX",fields[3]);Crow.put("DY",fields[4]);int cioid=CommanFunction.StringToInt(fields[5]);Crow.put("CIOID",cioid);Crow.put("CSTATUS",fields[6]);int cgpsf=CommanFunction.StringToInt(fields[7]);Crow.put("CGPSF",cgpsf);int cspeed=CommanFunction.StringToInt(fields[8]);Crow.put("CSPEED",cspeed);int cdir=CommanFunction.StringToInt(fields[9]);Crow.put("CDIRECTION",cdir);Crow.put("ADDRESS",fields[10]);float dx_54=CommanFunction.StringToFloat(fields[fieldsLength-8]);float dy_54=CommanFunction.StringToFloat(fields[fieldsLength-7]);float coor[]={dx_54,dy_54};ArrayValue center=Prow.putArray("center");center.add(coor);ArrayValue bound_box=Prow.putArray("bound_box");bound_box.add(coor);ArrayValue geometry=Prow.putArray("geometry");geometry.add(coor);Crow.put("DALARMPEOPLE",fields[fieldsLength-6]);//DecimalFormat decFormat=new DecimalFormat(".00000",new DecimalFormatSymbols(new Locale("en_US")));double lat=CommanFunction.StringToFloat(decFormat.format(dy_54));double lon=CommanFunction.StringToFloat(decFormat.format(dx_54));Date date=CommanFunction.StringToDate(fields[1]);if(date==null){Prow=Crow=null;System.out.println(rowID+":转换失败!");return;}Cube cube=new Cube(30,lat,lon,date,2013);Prow.put("st_code",cube.getCode());float totalkm=CommanFunction.StringToFloat(fields[fieldsLength-5]);Crow.put("TOTALKM",totalkm);int datekm=CommanFunction.StringToInt(fields[fieldsLength-4]);Crow.put("DATEKM",datekm);int shtxt=CommanFunction.StringToInt(fields[fieldsLength-1]);Crow.put("SHTXT",shtxt);if(time==Constants.DateTimeToMilliSecFunc_Exception||cioid==Constants.StringToIntFunc_Exception||cgpsf==Constants.StringToIntFunc_Exception||cspeed==Constants.StringToIntFunc_Exception||dx_54==Constants.StringToFloatFunc_Exception||dy_54==Constants.StringToFloatFunc_Exception||totalkm==Constants.StringToFloatFunc_Exception||datekm==Constants.StringToIntFunc_Exception||shtxt==Constants.StringToIntFunc_Exception){Prow=Crow=null;System.out.println(rowID+":转换失败!");return;}}private static String[] getTxtPaths(String folderPath){File files=new File(folderPath);String[] result=files.list();for(int i=0;i<result.length;i++){result[i]=folderPath+"/"+result[i];}return result;}}

CommonFunction类:

package xiamengps.connect;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;public class CommanFunction {public CommanFunction() {// TODO Auto-generated constructor stub}public static long DateTimeToMilliSec(String time){try{Date date = StringToDate(time);Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));cal.setTime(date);return cal.getTimeInMillis();}catch(Exception e){printException(e);return Constants.DateTimeToMilliSecFunc_Exception;}}public static Date StringToDate(String time){SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd H:m:s");format.setTimeZone(TimeZone.getTimeZone("UTC"));try{return format.parse(time);}catch(Exception e){printException(e);return Constants.StringToDateFunc_Exception;}}public static int StringToInt(String value){try{if(value.equals("")||value.equals(null)){return 0;}return Integer.parseInt(value);}catch(Exception e){printException(e);return Constants.StringToIntFunc_Exception;}}public static float StringToFloat(String value){try{if(value.equals("")||value.equals(null)){return 0;}return Float.parseFloat(value);}catch(Exception e){printException(e);return Constants.StringToFloatFunc_Exception;}}public static void printException(Exception e){System.out.println(e.getMessage());}
}

Constants类:

package xiamengps.connect;import java.util.Date;public final class Constants {public final static long DateTimeToMilliSecFunc_Exception=-111;public final static int StringToIntFunc_Exception=-111;public final static float StringToFloatFunc_Exception=-111;public final static Date StringToDateFunc_Exception=null;}

多线程读文本写入OracleNoSQL数据库相关推荐

  1. spring批量写入mysql数据库_快速使用组件-spring batch(3)读文件数据到数据库

    tags: springbatch 1.引言 上一篇文章<快速了解组件-spring batch(2)之helloworld>对Spring Batch进行了入门级的开发,也对基本的组件有 ...

  2. 使用FSO把文本信息导入数据库

    使用FSO把文本信息导入数据库 在开发WEB应用程序中,我们经常需要对文件系统中的驱动器.文件夹和文件进行处理,比如收集驱动器的相关信息:创建.添加.移动或删除文件夹和文件等.在VB6中新提供了一套称 ...

  3. Asterisk realtime 之SIP用户动态写入mysql 数据库

    asterisk  配置默认是文本方式由文件管理,但是对于SIP用户,队列坐席这些数据,保存在 数据库中方便维护,Asterisk 通过realtime 引擎支持此功能,可以把其 配置文件写入 数据库 ...

  4. 上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)(1)

    最近做了个用户维护功能,涉及到照片的操作. 照片是存到数据库oracle中的Blob字段中. 难点有两个: 1,图片的上传:2,Blob字段的读取. 先说图片的上传吧, 我使用common-fileu ...

  5. Python高并发应用场景下四种写入SQLite数据库的速度比较

    "Python小屋"编程比赛正式开始 中国大学MOOC"Python程序设计基础"第5次开课 推荐图书: <Python程序设计基础(第2版)>,I ...

  6. python文本框与数据库的关联_Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理...

    基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3. ...

  7. python 并发 数据库_python写入mysql数据库

    scrapy爬虫成长日记之将抓取内容写入mysql数据库 前面小试了一下scrapy抓取博客园的博客(您可在此查看scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据),但是前面抓取 ...

  8. Oracle10表数据编辑器,Oracle ORACLE 快速批量导入文本数据到数据库(sqlldr工具)方法与分析 (Windows CMD 方式)...

    Oracle ORACLE 快速批量导入文本数据到数据库(sqlldr工具)方法与分析 (Windows CMD 方式) 在实际生产环境中,常会碰到将一些如通过通讯接口传过来的数据(文本文件 txt) ...

  9. java多线程 文件夹_java多线程读同一个文件

    java多线程同时读取一个文件,这个方法可行吗?不可行. 多线程能够提高效率是因为现在的cpu普遍是多核cpu, 多条线程可以在多个内核中同时执行来提高计算效率.但是计算机磁盘的磁头只有一个,即使多条 ...

  10. 多线程高并发编程MySQL数据库处理

    本文作者:陈进坚 个人博客:https://jian1098.github.io CSDN博客:https://blog.csdn.net/c_jian 简书:https://www.jianshu. ...

最新文章

  1. Deep Learning | 深度学习介绍与基本概念
  2. 阿里云MySQL远程登录异常
  3. 对计算机网络的父亲,父亲节朋友圈对老爸说的话 写给父亲催泪的话简短
  4. 华为手机投屏电脑_华为手机如何投屏到电脑?这很实用
  5. vue 指令 v-if v-else-if v-else
  6. Spring Security OAuth2.0_实现分布式认证授权_搭建网关工程_Spring Security OAuth2.0认证授权---springcloud工作笔记151
  7. springboot不会运行gc_SpringBoot 深度调优,让你的项目飞起来!
  8. xp等系统的登陆的密码清除方法
  9. PaperWeekly 第三期
  10. 一个广告资源运营管理中台系统简介
  11. 弘辽科技:直通车测款的必备技巧
  12. js将华氏度转为摄氏度
  13. Linux 程序编译过程的来龙去脉
  14. 计算机房的正常温度和湿度,各类机房的温湿度标准参考
  15. 如何在macOS 中让Gatekeeper在任何地方允许应用程序
  16. 自动控制原理MATLAB命令
  17. 英雄联盟手游登录注册地与服务器不匹配,英雄联盟手游提示Unable to login地区不支持怎么解决 地区不支持解决方法_手心游戏...
  18. WOT博科聂小云:WLAN网络容量性能设计和优化
  19. 微信小程序用户登录前后台详解
  20. 流量分析——安恒科技(八月CTF)

热门文章

  1. CS5211 eDP转LVDS转换器芯片 CS5211芯片说明书
  2. python在线朗读-python朗读软件
  3. android的got表HOOK实现
  4. 计算机专业 论文检索,如何检索计算机专业论文文献
  5. 易康EPS2的使用(一)
  6. 邮件安全风险评估方案
  7. MIR7 金额计算公式
  8. feign.exception Connection reset executing
  9. element-ui tabs组件导致页面假死浏览器崩溃
  10. Apple 宣布 2021 年 Apple Design Awards 获奖者