1. 程式人生 > >利用Solr建立HBase的二級索引

利用Solr建立HBase的二級索引

 public void insertSolr(Put put) {
  CloudSolrServer cloudSolrServer;
  final String zkHost = "IP:2181,IP:2181,IP:2181";
  final int zkConnectTimeout = 1;
  cloudSolrServer = new CloudSolrServer(zkHost);
  cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);
  cloudSolrServer.connect();
  cloudSolrServer.setDefaultCollection("student");
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", Bytes.toString(put.getRow()));
  for (KeyValue c : put.getFamilyMap().get(Bytes.toBytes("info"))) {
   String key = Bytes.toString(c.getQualifier());
   String value = Bytes.toString(c.getValue());
   System.out.println("key = " + key);
   System.out.println("value = " + value);
   if (key.equalsIgnoreCase("name") || key.equalsIgnoreCase("address")) {
    Map<String, Object> oper = new HashMap<String, Object>();
    oper.put("set", value);
    doc.addField(key, oper);
   }
  }
  try {
   cloudSolrServer.add(doc);
   cloudSolrServer.commit(true, true, true);
   cloudSolrServer.shutdown();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   System.out.println("11111");
   cloudSolrServer.shutdown();
   putData("temp_student", put);
   // e.printStackTrace();
  }
 }