安装 Elasticsearch
升级系统后安装 Oracle Java 7,既然 Elasticsearch 官方推荐使用 Oracle JDK 7 就不要尝试 JDK 8 和 OpenJDK 了:
$ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:webupd8team/java $ sudo apt-get update $ sudo apt-get install oracle-java7-installer
加入 Elasticsearch 官方源后安装 elasticsearch:
$ wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | apt-key add - $ sudo echo "deb http://packages.elasticsearch.org/elasticsearch/1.1/debian stable main" >> /etc/apt/sources.list $ sudo apt-get update $ sudo apt-get install elasticsearch
加入到系统启动文件并启动 elasticsearch 服务,用 curl 测试一下安装是否成功:
$ sudo update-rc.d elasticsearch defaults 95 1 $ sudo /etc/init.d/elasticsearch start $ curl -X GET 'http://localhost:9200' { "status" : 200, "name" : "Fer-de-Lance", "version" : { "number" : "1.1.1", "build_hash" : "f1585f096d3f3985e73456debdc1a0745f512bbc", "build_timestamp" : "2014-04-16T14:27:12Z", "build_snapshot" : false, "lucene_version" : "4.7" }, "tagline" : "You Know, for Search" }
Elasticsearch 的集群和数据管理界面 Marvel 非常赞,可惜只对开发环境免费,如果这个工具也免费就无敌了,安装很简单,完成后重启服务访问 http://192.168.2.172:9200/_plugin/marvel/ 就可以看到界面:
$ sudo /usr/share/elasticsearch/bin/plugin -i elasticsearch/marvel/latest $ sudo /etc/init.d/elasticsearch restart * Stopping Elasticsearch Server [ OK ] * Starting Elasticsearch Server [ OK ]
安装 Python 客户端驱动
和 MongoDB 一样,我们一般用程序和 Elasticsearch 交互,Elasticsearch 也支持多种语言的客户端驱动,这里仅安装 Python 驱动,其他语言可以参考官方文档。
$ sudo apt-get install python-pip $ sudo pip install elasticsearch
写个简单程序把 gene_info.txt 的数据导入到 Elasticsearch:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, os.path, sys, re import csv, time, string from datetime import datetime from elasticsearch import Elasticsearch def import_to_db(): data = csv.reader(open('gene_info.txt', 'rb'), delimiter='\t') data.next() es = Elasticsearch() for row in data: doc = { 'tax_id': row[0], 'GeneID': row[1], 'Symbol': row[2], 'LocusTag': row[3], 'Synonyms': row[4], 'dbXrefs': row[5], 'chromosome': row[6], 'map_location': row[7], 'description': row[8], 'type_of_gene': row[9], 'Symbol_from_nomenclature_authority': row[10], 'Full_name_from_nomenclature_authority': row[11], 'Nomenclature_status': row[12], 'Other_designations': row[13], 'Modification_date': row[14] } res = es.index(index="gene", doc_type='gene_info', body=doc) def main(): import_to_db() if __name__ == "__main__": main()
Kibana 是一个功能强大的数据显示客户端,通过插件方式和 Elasticsearch 集成在一起,安装很容易,下载解压就可以了,然后重启 Elasticsearch 服务访问 http://192.168.2.172:9200/_plugin/kibana/ 就能看到界面:
$ wget https://download.elasticsearch.org/kibana/kibana/kibana-3.0.1.tar.gz $ tar zxvf kibana-3.0.1.tar.gz $ sudo mv kibana-3.0.1 /usr/share/elasticsearch/plugins/_site $ sudo /etc/init.d/elasticsearch restart