SQLite之大数据量批量入库

前端之家收集整理的这篇文章主要介绍了SQLite之大数据量批量入库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.sqlException;
  11. import java.sql.Statement;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14. import java.util.concurrent.TimeUnit;
  15.  
  16. public class BatchTool {
  17. // ddl
  18. private static String ddl = "CREATE TABLE IF NOT EXISTS pbeijing_point (OBJECTID INTEGER,NAME TEXT,ADDRESS TEXT,PHONE TEXT,FAX TEXT,TYPE TEXT,CITYCODE TEXT,URL TEXT,EMAIL TEXT,NAME2 TEXT,X INTEGER,Y INTEGER)";
  19. Connection jCon = null;
  20.  
  21. // get connection
  22. public synchronized Connection getConnection() {
  23. if (jCon == null) {
  24. // json=
  25. Statement state = null;
  26. try {
  27. Class.forName("org.sqlite.JDBC");
  28. jCon = DriverManager.getConnection("jdbc:sqlite:c:\\newD.db");
  29. state = jCon.createStatement();
  30. state.executeUpdate(ddl);
  31. } catch (sqlException e) {
  32. e.printStackTrace();
  33. } catch (ClassNotFoundException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. return jCon;
  38. }
  39.  
  40. // 创建500个线程
  41. ExecutorService service = Executors.newFixedThreadPool(20);
  42.  
  43. // 读取sql文件 每五百个insert 语句由一个线程批量操作
  44. public void readBatchsql(InputStream is) throws IOException {
  45. BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
  46. String line;
  47. String one = "";
  48. int tag = 0;
  49. String batchsql = "";
  50. while ((line = bufferReader.readLine()) != null) {
  51. one += line;
  52. if (one.indexOf(";") != -1) {
  53. batchsql += one;
  54. one = "";// reset
  55. tag++;
  56. }
  57. // 符合条件 开辟一个线程
  58. if (tag != 0 && tag / 500 != 0) {
  59. service.execute(new sqliteBatchHandler(batchsql));
  60. batchsql = "";// reset
  61. tag = 0;// reset
  62. }
  63. }
  64. // 最后执行 剩余的sql
  65. if (batchsql.length() > 0) {
  66. System.out.println("finalsql:" + batchsql);
  67. Runnable r = new sqliteBatchHandler(batchsql);
  68. service.execute(r);
  69. }
  70. try {
  71. // 关闭线程池
  72. this.service.shutdown();
  73. this.service.awaitTermination(1,TimeUnit.HOURS);
  74. getConnection().close();
  75. } catch (InterruptedException e) {
  76. e.printStackTrace();
  77. } catch (sqlException e) {
  78. e.printStackTrace();
  79. }
  80.  
  81. };
  82.  
  83. /**
  84. * @note 分割sql
  85. * */
  86. private static String[] splitsql(String batchsql) {
  87. if (batchsql != null) {
  88. return batchsql.split(";");
  89. }
  90. return null;
  91. }
  92.  
  93. /**
  94. * @note 执行批量更新操作 由于connection.comit 操作时 如果存在 statement没有close 就会报错
  95. * 因此将此方法加上同步 。
  96. * */
  97. private synchronized void exucteUpdate(String batch) {
  98. Statement ste = null;
  99. Connection con = null;
  100. try {
  101. con = getConnection();
  102. con.setAutoCommit(false);
  103. ste = con.createStatement();
  104. String[] sqls = splitsql(batch);
  105. for (String sql : sqls) {
  106. if (sql != null) {
  107. ste.addBatch(sql);
  108. }
  109. }
  110. ste.executeBatch();
  111. ste.close();
  112. con.commit();// 提交
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. System.out.println("执行失败:" + batch);
  116. try {
  117. con.rollback();// 回滚
  118. } catch (sqlException e1) {
  119. e1.printStackTrace();
  120. }
  121. } finally {
  122. if (ste != null) {
  123. try {
  124. ste.close();
  125. } catch (sqlException e) {
  126. e.printStackTrace();
  127. }
  128. }
  129. }
  130. }
  131.  
  132. /**
  133. * @note 入库线程
  134. * */
  135. private class sqliteBatchHandler implements Runnable {
  136. private String batch;
  137.  
  138. public sqliteBatchHandler(String sql) {
  139. this.batch = sql;
  140. }
  141.  
  142. @SuppressWarnings("static-access")
  143. @Override
  144. public void run() {
  145. try {
  146. Thread.currentThread().sleep(50);
  147. } catch (InterruptedException e) {
  148. e.printStackTrace();
  149. }
  150. if (this.batch.length() > 0) {
  151. exucteUpdate(batch);
  152.  
  153. }
  154. }
  155. }
  156.  
  157. public static void main(String[] args) throws FileNotFoundException,IOException {
  158. BatchTool s = new BatchTool();
  159. s.readBatchsql(new FileInputStream(new File("c:\\poi.sql")));
  160. }
  161. }

猜你在找的Sqlite相关文章