-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvImporter.java
More file actions
56 lines (40 loc) · 1.41 KB
/
Copy pathCsvImporter.java
File metadata and controls
56 lines (40 loc) · 1.41 KB
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
package Examples;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import com.csvreader.CsvReader;
public class CsvImporter {
private static final String urlString = "http://localhost:8983/solr";
private SolrServer solrServer;
public CsvImporter() throws SolrServerException, IOException {
if (solrServer == null) {
System.out.println("sahil");
solrServer = new HttpSolrServer(urlString);
solrServer.deleteByQuery("*:*");
}
}
public void addDocument(String csvFileName) throws IOException, SolrServerException
{
CsvReader items = new CsvReader(csvFileName);
items.readHeaders();
while(items.readRecord()){
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", items.get(0));
doc.addField("cname", items.get(1));
doc.addField("fname", items.get(2));
solrServer.add(doc);
}
solrServer.commit();
}
public static void main(String[] args) throws IOException, SolrServerException {
// TODO Auto-generated method stub
CsvImporter csv = new CsvImporter();
csv.addDocument(args[0]);
System.out.println("File is dumped");
}
}