紧接上条博客,加上 插入数据的代码。
solr的配置在上篇文章已经写了。
直接看代码吧 :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58public class insertData {
	public static void main(String[] args)
	{
	    BufferedReader br = null;
	    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	    SolrTemplate geoQueryTemplate= (SolrTemplate) ac.getBean("geoQueryTemplate");
	    String filePath = "C:\\Users\\userlqq\\Desktop\\aa.csv";
	    String connection = "geo";  
	    try
	    {
	    	DataInputStream csv = new DataInputStream(new FileInputStream(new File(filePath)));
	        br = new BufferedReader(new InputStreamReader(csv,"GBK"));
	    } catch (FileNotFoundException | UnsupportedEncodingException e)
	    {
	        e.printStackTrace();
	    }
	    String line = "";
	    try {
	            int all = 0;
	            int n = 10000;
	            List<GeoPosition> ls = new ArrayList();
	            while ((line = br.readLine()) != null)  //读取到的内容给line变量
	            {
	            	// 处理数据
	                String[] lineInfo = line.split("\\|");
	                if(lineInfo.length < 5){
	                	System.err.println("出错了:"+line);
	                	continue;
	                }
	                GeoPosition a = new GeoPosition();
	                a.setCategory(lineInfo[0]);
	                a.setId(lineInfo[1]);
	                a.setLocation(lineInfo[2]+","+lineInfo[3]);
	                a.setName(lineInfo[4].endsWith(",") ? lineInfo[4].substring(0, lineInfo[4].length()-1) : lineInfo[4]);
	                a.setPosition(lineInfo[2]+","+lineInfo[3]);
	                //  存储
	                ls.add(a);
	                //  每  n 个 插入一次
	                if(ls.size()==n){
//	                	System.out.println("commit一次,目前总数:"+all);
	                	geoQueryTemplate.saveBeans(connection, ls);
	                    geoQueryTemplate.commit(connection);
	                    ls.clear();
	                }
	                all++;
	            }
	            if(ls.size()>0){
	            	System.out.println("最后一次:"+ls.size());
	            	geoQueryTemplate.saveBeans(connection, ls);
	                geoQueryTemplate.commit(connection);
	            }
	            System.out.println(all+"条数据已经插入完成!");
	    } catch (IOException e)
	    {
	        e.printStackTrace();
	    }
	}
}
流程
首先读取excel文件 用while循环 将每条记录稍加处理后存入bean ,在将bean存入临时数组,当数组长度到达10000的时候 ,进行一次 批量导入(saveBeans) 在清空数组,重新填充。这样能极大的加快插入速度
在这里遇到了几个错误 ,都是url地址的问题 最终 在 saveBeans和commit方法都指定了 库名 才解决了问题。
如果遇到问题 ,请再三确认请求的地址是否错误。
接下来就可以去solr 的 web页面查看有没有数据了。