Browse Source

增加从数据库下载器件数据的接口

sunyj 9 years ago
parent
commit
e2ee11f50e

+ 8 - 1
search-console/src/main/java/com/uas/search/console/controller/IndexController.java

@@ -41,7 +41,14 @@ public class IndexController {
 				tableNameList.add(str.toLowerCase());
 			}
 		}
-		return String.format("Indexes created success in %.2fs", indexService.createIndexs(tableNameList) / 1000.0);
+		return String.format("Indexes created success in %.2fs",
+				indexService.createIndexes(tableNameList, true) / 1000.0);
+	}
+
+	@RequestMapping("/downloadComponentData")
+	@ResponseBody
+	public String downloadComponentDataFromDatabase() {
+		return "Data downloaded success in " + indexService.downloadComponentDataFromDatabase() + " ms.";
 	}
 
 	@RequestMapping("/listen/start")

+ 10 - 1
search-console/src/main/java/com/uas/search/console/service/IndexService.java

@@ -19,9 +19,18 @@ public interface IndexService {
 	 * 
 	 * @param tableNames
 	 *            指定的表,默认创建所有表的索引
+	 * @param componentFromFiles
+	 *            器件索引数据是否来自文件,默认来自文件
 	 * @return 创建的索引花费总时间 ms
 	 */
-	public Long createIndexs(List<String> tableNames);
+	public Long createIndexes(List<String> tableNames, Boolean componentFromFiles);
+
+	/**
+	 * 下载器件的数据至本地文件中,以供建索引用
+	 * 
+	 * @return 花费总时间 ms
+	 */
+	public Long downloadComponentDataFromDatabase();
 
 	/**
 	 * 将新对象添加在lucene索引中

+ 77 - 11
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -5,6 +5,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -47,7 +48,6 @@ import com.uas.search.console.model.PurchaseInvoiceSimpleInfo;
 import com.uas.search.console.model.PurchaseSimpleInfo;
 import com.uas.search.console.service.IndexService;
 import com.uas.search.console.support.IndexWriterManager;
-import com.uas.search.console.util.MergeComponentData;
 import com.uas.search.console.util.ObjectToDocumentUtils;
 import com.uas.search.console.util.SearchConstants;
 import com.uas.search.console.util.SearchUtils;
@@ -107,10 +107,13 @@ public class IndexServiceImpl implements IndexService {
 
 	private static final int PAGE_SIZE = 1000;
 
+	// 单个文件存储的最大数据数目
+	public static final int SINGLE_FILE_MAX_SIZE = 100000;
+
 	private static Logger logger = LoggerFactory.getLogger(IndexServiceImpl.class);
 
 	@Override
-	public Long createIndexs(List<String> tableNames) {
+	public Long createIndexes(List<String> tableNames, Boolean componentFromFiles) {
 		if (creatingIndex) {
 			throw new SearchException("已存在线程在创建索引,不可重复请求");
 		}
@@ -131,13 +134,18 @@ public class IndexServiceImpl implements IndexService {
 			for (String tableName : tableNames) {
 				Long start = new Date().getTime();
 				Long size = 0L;
-				deleteIndexs(tableName);
+				deleteIndexes(tableName);
 				if (tableName.equals(SearchConstants.KIND_TABLE_NAME)) {
-					size = createKindIndexs();
+					size = createKindIndexes();
 				} else if (tableName.equals(SearchConstants.BRAND_TABLE_NAME)) {
-					size = createBrandIndexs();
+					size = createBrandIndexes();
 				} else if (tableName.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
-					size = createComponentIndexesWithFiles();
+					// 只有明确指定不从本地文件读取数据时,才从数据库获取数据建立索引
+					if (componentFromFiles != null && !componentFromFiles) {
+						size = createComponentIndexes();
+					} else {
+						size = createComponentIndexesWithFiles();
+					}
 				} else if (tableName.equals(SearchConstants.ORDER_TABLE_NAME)) {
 					size = createOrderIndexes();
 				} else if (tableName.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
@@ -176,7 +184,7 @@ public class IndexServiceImpl implements IndexService {
 	 * @throws InterruptedException
 	 * @throws IOException
 	 */
-	private void deleteIndexs(String tableName) throws InterruptedException, IOException {
+	private void deleteIndexes(String tableName) throws InterruptedException, IOException {
 		indexWriter = indexWriterManager.get(tableName);
 		logger.info("正在清理旧索引..." + tableName);
 		indexWriter.deleteAll();
@@ -190,7 +198,7 @@ public class IndexServiceImpl implements IndexService {
 	 * @return 写入的类目索引数
 	 * @throws IOException
 	 */
-	private Long createKindIndexs() throws IOException {
+	private Long createKindIndexes() throws IOException {
 		logger.info("正在创建类目索引...");
 		List<KindSimpleInfo> kinds = kindDao.findAll();
 		logger.info("发现数据:" + kinds.size() + "条");
@@ -203,7 +211,7 @@ public class IndexServiceImpl implements IndexService {
 	 * @return 写入的品牌索引数
 	 * @throws IOException
 	 */
-	private Long createBrandIndexs() throws IOException {
+	private Long createBrandIndexes() throws IOException {
 		logger.info("正在创建品牌索引...");
 		List<BrandSimpleInfo> brands = brandDao.findAll();
 		logger.info("发现数据:" + brands.size() + "条");
@@ -221,7 +229,7 @@ public class IndexServiceImpl implements IndexService {
 				return 0L;
 			}
 			// 将要创建的索引总数目约为:文件数目*单个文件的行数
-			long totalSize = files.length * MergeComponentData.SINGLE_FILE_MAX_SIZE;
+			long totalSize = files.length * SINGLE_FILE_MAX_SIZE;
 			for (File file : files) {
 				logger.info("读取文件: " + file.getName());
 				BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
@@ -257,7 +265,7 @@ public class IndexServiceImpl implements IndexService {
 	 * @return 写入的器件索引数
 	 * @throws IOException
 	 */
-	public Long createComponentIndexs() throws IOException {
+	public Long createComponentIndexes() throws IOException {
 		logger.info("正在创建器件索引...");
 		Long size = 0L;
 		PageParams params = new PageParams();
@@ -380,6 +388,64 @@ public class IndexServiceImpl implements IndexService {
 		return null;
 	}
 
+	@Override
+	public Long downloadComponentDataFromDatabase() {
+		Long startTime = new Date().getTime();
+		logger.info("下载器件... ");
+
+		// 分页获取数据
+		PageParams pageParams = new PageParams();
+		pageParams.setPage(1);
+		pageParams.setCount(PAGE_SIZE);
+		PageInfo pageInfo = new PageInfo(pageParams);
+		Page<ComponentSimpleInfo> pageResult = componentDao.findAll(pageInfo);
+
+		// 数据库中数据的总数目
+		long totalElements = pageResult.getTotalElements();
+		logger.info("发现数据:" + totalElements + "条");
+		// 已翻页的数据数目
+		Long size = 0L;
+		int fileIndex = 1;
+		PrintWriter printWriter = null;
+		int count = 0;
+		try {
+			File file = new File(luceneProperties.getComponentsDir());
+			if (!file.exists()) {
+				file.mkdirs();
+			}
+			printWriter = new PrintWriter(luceneProperties.getComponentsDir() + "/" + fileIndex + ".txt");
+			while (totalElements > size) {
+				// 一个文件存放100000条数据,一旦超过,写入新的文件
+				if (count > SINGLE_FILE_MAX_SIZE) {
+					count = 1;
+					printWriter.flush();
+					printWriter.close();
+					fileIndex++;
+					printWriter = new PrintWriter(luceneProperties.getComponentsDir() + "/" + fileIndex + ".txt");
+				}
+				List<ComponentSimpleInfo> content = pageResult.getContent();
+				for (ComponentSimpleInfo element : content) {
+					printWriter.println(JSONObject.toJSONString(element));
+					count++;
+				}
+				size += content.size();
+				logger.info(String.format("Downloaded...................%.2f%%", size * 100.0 / totalElements));
+
+				pageParams.setPage(pageParams.getPage() + 1);
+				pageInfo = new PageInfo(pageParams);
+				pageResult = componentDao.findAll(pageInfo);
+			}
+			printWriter.flush();
+			printWriter.close();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		}
+
+		Long endTime = new Date().getTime();
+		logger.info(String.format("下载数据%s条,耗时%.2fs\n ", totalElements, (endTime - startTime) / 1000.0));
+		return endTime - startTime;
+	}
+
 	@Override
 	public Object update(Object obj) {
 		if (obj != null) {

+ 2 - 0
search-console/src/main/webapp/WEB-INF/views/console.html

@@ -73,8 +73,10 @@
 
 			<h2>3. 索引修改</h2>
 			<ol>
+				<li>index/create?tableNames=product$brand,trade$order&componentFromFiles=false</li>
 				<li><a target="_blank">index/create</a></li>
 				<li><a target="_blank">index/create?tableNames=product$brand,trade$order</a></li>
+				<li><a target="_blank">index/downloadComponentData</a></li>
 				<li><a target="_blank">index/listen/start?waitInterval=10</a></li>
 				<li><a target="_blank">index/listen/stop</a></li>
 				<li><a target="_blank">index/listen/restart</a></li>