五、使用 SOLRJ 向 SOLR 增加14万条索引记录

如何创建索引

 
solr 提供了一种方式向其中增加索引的界面,但是呢。。。不太方便,也和实际工作环境不相符合。 
实际工作环境一般都是从数据库里读取数据,然后加入到索引的。很少会通过界面添加索引,因为这样维护更新删除也不方便,尤其是数据量比较大的时候。 
那么本教材就会讲解,如何通过程序把数据加入到Solr 索引里。
如何创建索引

SolrJ

 
Solr 支持通过各种各样的语言(如php,javascript, c#, )把数据加入到索引里,因为本教程主要是基于Java的,所以会使用一个第三方工具SolrJ,使用 Java 语言来把数据加入到索引里。
 

Product.java

 
准备实体类来存放产品信息
注: 每个字段上都有@Field 注解,用来告诉Solr 这些和 how2java core里的字段对应
 
 
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
58
59
60
61
62
63
package how2java;
 
import org.apache.solr.client.solrj.beans.Field;
 
public class Product {
     
    @Field
    int id;
    @Field
    String name;
    @Field
    String category;
    @Field
    float price;
    @Field
    String place;
    @Field
    String code;
     
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getPlace() {
        return place;
    }
    public void setPlace(String place) {
        this.place = place;
    }
 
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    @Override
    public String toString() {
        return "Product [id=" + id + ", name=" + name + ", category=" + category + ", price=" + price + ", place="
                + place + ", code=" + code + "]";
    }
 
}

ProductUtil

 
工具类,把 140k_products.txt 文本文件,转换为泛型是Product的集合
 
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
package how2java;
 
import java.awt.AWTException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.commons.io.FileUtils;
     
public class ProductUtil {
     
    public static void main(String[] args) throws IOException, InterruptedException, AWTException {
 
        String fileName = "140k_products.txt";
         
        List<Product> products = file2list(fileName);
         
        System.out.println(products.size());
             
    }
 
    public static List<Product> file2list(String fileName) throws IOException {
        File f = new File(fileName);
        List<String> lines = FileUtils.readLines(f,"UTF-8");
        List<Product> products = new ArrayList<>();
        for (String line : lines) {
            Product p = line2product(line);
            products.add(p);
        }
        return products;
    }
     
    private static Product line2product(String line) {
        Product p = new Product();
        String[] fields = line.split(",");
        p.setId(Integer.parseInt(fields[0]));
        p.setName(fields[1]);
        p.setCategory(fields[2]);
        p.setPrice(Float.parseFloat(fields[3]));
        p.setPlace(fields[4]);
        p.setCode(fields[5]);
        return p;
    }
 
}

SolrUtil

工具类,用来把产品集合批量增加到Solr. 这里就用到了SolrJ第三方包里的api了。
 
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
package how2java;
import java.io.IOException;
import java.util.List;
 
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;
 
public class SolrUtil {
    public static SolrClient client;
    private static String url;
    static {
        url = "http://localhost:8983/solr/how2java";
        client = new HttpSolrClient.Builder(url).build();
    }
 
    public static <T> boolean batchSaveOrUpdate(List<T> entities) throws SolrServerException, IOException {
 
        DocumentObjectBinder binder = new DocumentObjectBinder();
        int total = entities.size();
        int count=0;
        for (T t : entities) {
            SolrInputDocument doc = binder.toSolrInputDocument(t);
            client.add(doc);
            System.out.printf("添加数据到索引中,总共要添加 %d 条记录,当前添加第%d条 %n",total,++count);
        }
        client.commit();
        return true;
    }
 
}

TestSolr4j

 
得到14万个产品对象,然后通过SolrUtil 工具类提交到Solr 服务器
1
2
3
4
5
6
7
8
9
10
11
package how2java;
import java.io.IOException;
import java.util.List;
import org.apache.solr.client.solrj.SolrServerException;
 
public class TestSolr4j {
    public static void main(String[] args) throws SolrServerException, IOException {
        List<Product> products = ProductUtil.file2list("140k_products.txt");
        SolrUtil.batchSaveOrUpdate(products);
    }
}

验证提交效果

 
打开
 
http://127.0.0.1:8983/solr/#/how2java
 

左边点击 Query -> 点击 Execute Query 查询之后,可以看到右侧显示查询结果,总数是 147939 条
验证提交效果
下载地址:140k_products.rar   :http://download.how2j.cn/1688/140k_products.rar
      solr4j.rar  : http://download.how2j.cn/1690/solr4j.rar

posted on 2019-01-04 11:09  我是司  阅读(577)  评论(0)    收藏  举报

导航