批处理 bat文件制作
@echo 1. 配置好环境变量后,运行
@set classpath=%classpath%;.;.\lib\*;
@set PATH=%JAVA_HOME%\bin;%PATH%;
@java -server -Xms256m -Xmx1024m ImportCVE
@if errorlevel 1 (
@echo ----------------------------------------------
@echo ****错误***: 请设置好JAVA_HOME环境变量再运行或者检查你的classpath路径
@pause
)
:end
java文件 (执行main方法)
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
public class ImportCVE {
public static Client getClient() {
return new TransportClient().addTransportAddress(new InetSocketTransportAddress("10.56.4.41", 9300));
}
//创建索引
public void createCVEIndex(String index,String type) throws IOException{
getClient().admin().indices().prepareCreate(index).execute().actionGet();
//索引主事件
XContentBuilder mapping = null;
mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject(type)
// .startObject("settings").startObject("_source")
.startObject("_id").field("path","id").field("store", "yes").field("index", "not_analyzed").endObject()
.startObject("properties")
.startObject("name").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("status").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("description").field("type", "string").field("store", "no").endObject()
.startObject("references").field("type", "string").field("store", "no").endObject()
.startObject("phase").field("type", "string").field("store", "no").endObject()
.startObject("votes").field("type", "string").field("store", "no").endObject()
.startObject("comments").field("type", "string").field("store", "no").endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest mappingRequest = Requests.putMappingRequest(index).type(type).source(mapping);
getClient().admin().indices().putMapping(mappingRequest).actionGet();
// client.close();
}
public static String getValue(XSSFCell cell){
if(cell==null)
return "";
return cell.getStringCellValue();
}
private void saveToCVEIndex(String index, String type,File file) throws IOException {
InputStream is = new FileInputStream(file);
XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
XSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
XSSFRow hssfRow;
XSSFCell xh;
// 第3个sheet
hssfSheet = hssfWorkbook.getSheetAt(0); // 每天上报数据
System.out.println("总行数"+hssfSheet.getLastRowNum());
// 分两部分, 前半部分 数据转换封装
Client cli= getClient();
BulkRequestBuilder bulkRequest = cli.prepareBulk();
int i = 0;
for (int rowNum = 0; rowNum <=hssfSheet.getLastRowNum(); rowNum++) {
// for (int rowNum = 10; rowNum <= 10000; rowNum++) {
hssfRow = hssfSheet.getRow(rowNum);
/* CommonVulnerabilitiesExposures commonVulnerabilitiesExposures=new CommonVulnerabilitiesExposures();
commonVulnerabilitiesExposures.setName(getValue(hssfRow.getCell(0)));
commonVulnerabilitiesExposures.setStatus(getValue(hssfRow.getCell(1)));
commonVulnerabilitiesExposures.setDescription(getValue(hssfRow.getCell(2)));
commonVulnerabilitiesExposures.setReferences(getValue(hssfRow.getCell(3)));
commonVulnerabilitiesExposures.setPhase(getValue(hssfRow.getCell(4)));
commonVulnerabilitiesExposures.setVotes(getValue(hssfRow.getCell(5)));
commonVulnerabilitiesExposures.setComments(getValue(hssfRow.getCell(6)));*/
Map<String, Object> o = new HashMap<String, Object>();
o.put("name", getValue(hssfRow.getCell(0)));
o.put("status",getValue(hssfRow.getCell(1)));
o.put("description",getValue(hssfRow.getCell(2)));
o.put("references",getValue(hssfRow.getCell(3)));
o.put("phase", getValue(hssfRow.getCell(4)));
o.put("votes", getValue(hssfRow.getCell(5)));
o.put("comments", getValue(hssfRow.getCell(6)));
bulkRequest.add(cli.prepareIndex(index,type)
.setSource(o));
//cli.prepareIndex(index,type).setSource(o).execute();
if(rowNum%1000==0|| rowNum == hssfSheet.getLastRowNum())
{
BulkResponse resp = bulkRequest.execute().actionGet();
System.out.println(rowNum+" "+resp);
cli.close();
cli=getClient();
bulkRequest = cli.prepareBulk();
}
}
}
public void deleteindex(String index){
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
deleteIndexRequest.listenerThreaded(false);
getClient().admin().indices().delete(deleteIndexRequest);
}
public void deletedata(String index, String type){
DeleteResponse response = getClient().prepareDelete(index,type, "AVILx1OATt0HqWzvaooe")
.execute()
.actionGet();
}
public void createCVEIndexShijian(String index,String type) throws IOException{
getClient().admin().indices().prepareCreate(index).execute().actionGet();
//索引主事件
XContentBuilder mapping = null;
mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject(type)
// .startObject("settings").startObject("_source")
.startObject("_id").field("path","id").field("store", "yes").field("index", "not_analyzed").endObject()
.startObject("properties")
.startObject("solution").field("type", "string").field("store", "no").endObject()
.startObject("description").field("type", "string").field("store", "no").endObject()
.startObject("keyword").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("eventname").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest mappingRequest = Requests.putMappingRequest(index).type(type).source(mapping);
getClient().admin().indices().putMapping(mappingRequest).actionGet();
// client.close();
}
public static void main(String[] args) throws IOException {
Scanner s=new Scanner(System.in);
String name=s.next();
System.out.println("开始导入CVE数据");
//修改 es 地址
ImportCVE client = new ImportCVE();
String indexcve="cve";
// String indexeventbase="eventbase17";
//创建索引
//client.createCVEIndex(indexcve, indexcve);
//System.out.println("索引: "+indexcve +"已创建");
//eventbase
//client.createCVEIndexShijian(indexeventbase,indexeventbase);
//System.out.println("索引: "+indexeventbase +"已创建");
//导入数据
String fileName=name+".xlsx";
System.out.println(fileName);
File cvexlsx=new File(fileName);
// cvexlsx.createNewFile();
client.saveToCVEIndex(indexcve,indexcve,cvexlsx);
// client.deleteindex(indexcve);
//
// client.deleteindex(indexeventbase);
// client.createCVEIndex(index, type);
// client.deletedata(index, type);
System.out.println("导入CVE数据结束");
}
}