Sphinx 入门

sphinx说是性能不错

高速索引
在现代CPU上可达10 MB/秒(英文),在启用了中文分词后,建立索引的速度可达300K/s;
高速搜索
在2-4 GB的文本建立的索引上搜索,平均0.1秒内获得结果;
可处理大数据量
在单一CPU上,实测最高可对100GB的文本建立索引,单一索引可包括100M文件
支持分布式搜索
支持主从式的分布式搜索,支持单一节点失效不影响整个搜索系统

而且支持php,java,python
就是本地需要启个服务,然后其他的程序通过连这个服务搜索。默认端口9312

首先本地windows环境测试,
直接下载官方最新版本sphinx2.2.6
http://sphinxsearch.com/downloads/release/
配置方式网上有很多,我试的是mysql索引
比如这篇:
http://www.cnblogs.com/ainiaa/archive/2010/12/21/1912459.html

1,建立索引
下载好目录中有个sphinx.conf.in
改名成sphinx.conf 去掉id,配置下里面的mysql参数
然后运行bin\indexer.exe 就可以了

2,启动服务
运行bin\searchd.exe [可以写个索引名]

3,搜索
网上有很多search 关键词 搜索的例子
这个版本已经没有这个exe了。
提供了一个php的api
官方两个例子比较复杂
找了个简单的例子:
SetServer(‘localhost’,9312);
$result = $s->Query(‘key’);
//var_dump($result);
echo json_encode($result);
?>

其中
Query ( $query, $index=”*”, $comment=”” )
支持3个参数,
$query=查询关键词
$index=索引名,有多个表都有索引的话可以通过这里过滤下
$comment=应该是要查询的字段

不过问题来了。。介个不支持中文
http://www.coreseek.cn
搜索中文分词出现了coreseek,一开始以为是个分词工具,
但其实是sphinx中文分词版,里面带完整的sphinx.而且呢,里面的例子更方便,直接带cmd测试工具。。
配置文件也全中文说明,太方便了。

就是版本老的可以,最后一次更新是2013-11-26 年,Sphinx 2.2.1-beta 与 2.1.3-release 发布。

接下来用这个版本测试了下中文分词搜索,好用了!给个好评。

基本命令:
http://blog.sina.com.cn/s/blog_705e4fdc0101ajog.html

6、创建更新所有索引
$sudo /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/dict/csft_mysql.conf –all –rotate
如果配置正确的话,现在辅助表sph_counter中已经添加了一条数据

7、更新增量索引
$sudo /usr/local/coreseek/bin/indexer delta -c /usr/local/coreseek/dict/csft_mysql.conf –rotate

8、合并增量索引到主索引
$sudo /usr/local/coreseek/bin/indexer –merge -c /usr/local/coreseek/dict/csft_mysql.conf –rotate

9、启动sphinx的守护进程searchd
$sudo /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/dict/csft_mysql.conf

centos 安装参考
http://www.cnblogs.com/huailian/archive/2013/06/26/2987231.html

安装mmseg的时候,./configure出现错误:config.status: error: cannot find input file: src/Makefile.in
网上的文章是:
aclocal
libtoolize –force (libtoolize –f)
automake –add-missing (automake –a)
autoconf
autoheader
make clean

括号中是我的版本有些属性差异
运行aclocal
warning: macro `AM_PROG_LIBTOOL’ not found in library centos
需要安装
yum install libtool

安装coreseek的时候报错:

checking for CFLAGS needed for pthreads... none
checking for LIBS needed for pthreads... -lpthread
checking for pthreads... found
checking for pthread_mutex_timedlock... yes
checking whether to compile with MySQL support... yes
checking for mysql_config... mysql_config
checking for mysql_real_connect... no
checking for mysql_real_connect... no
checking MySQL include files... configure: error: missing include files.

******************************************************************************
ERROR: cannot find MySQL include files.

Check that you do have MySQL include files installed.
The package name is typically ‘mysql-devel’.

If include files are installed on your system, but you are still getting
this message, you should do one of the following:

1) either specify includes location explicitly, using –with-mysql-includes;
2) or specify MySQL installation root location explicitly, using –with-mysql;
3) or make sure that the path to ‘mysql_config’ program is listed in
your PATH environment variable.

To disable MySQL support, use –without-mysql option.
******************************************************************************

然后尝试安装mysql-devel报错:

warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID 4520afa9: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt

rpm -Uhv http://dag.wieers.com/packages/rpmforge-release/rpmforge-release-0.3.4-1.el4.rf.i386.rpm
rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-6

尼玛,疯了, 数据源问题,先不整这个了,去掉mysql支持安装下,最后终于装上了,但不支持mysql
换另一个服务器试下。。。
换了个服务器,安装成功。
查询乱码需要修改字符集
charset_type = utf-8

 

 

排序和分页:

java代码排序

public static void main(String[] args){
List l;
Object a;
SphinxClient clint=new SphinxClient();
try {
clint.SetSortMode(4, "id desc");
SphinxResult result= clint.Query("a");
System.out.println(result.matches);
for(int i=0;i<result.matches.length;i++){
SphinxMatch match= result.matches[i];
l=match.attrValues;
for(int j=0;j<l.size();j++){
a=l.get(j);
System.out.print(a+"|");

}
System.out.println("");
}
} catch (SphinxException e) {
e.printStackTrace();
}
}

 

php代码排序

require 'sphinxapi.php';
$s = new SphinxClient();
$s->SetServer('localhost',9312);
$s->SetSortMode( SPH_SORT_EXTENDED, "group_id DESC,@id DESC" );
$result = $s->Query('a');
echo json_encode($result);

java-ok php排序无效,为毛啊,api有问题么
换新api提示
searchd error: client version is higher than daemon version (client is v.1.30, daemon is v.1.23)
不匹配

卧槽,发现firefox json插件自动帮我排序,谢谢。我错怪api了。

然后测试下分页
$s->SetLimits(1,5,10000);
这样

基本使用上就没有啥问题了。

AngularJS 笔记

http://www.angularjs.cn/tag/AngularJS
看到了入门10,先记录下。。

十分强大,mvc齐全方面,做gurb的东西非常合适,
可以完全基于js的mvc开发网站,服务器端只需要开发json接口就可以了。
真的是太方便了。
问题是跟jquery用了同样的命名空间$, 虽然宣称两个方向不一样AngularJs很强大,但很多插件用不了了。。jquery的地位太重了。

看nio的感觉

看了这文:
http://blog.sina.com.cn/s/blog_81c2545a01011afh.html

感觉nio就是socket的优化升级版本,可以不阻塞。。大概是这意思。
总觉得跟jsm有点像,
但nio更属于socket那块的东西。
jms的apache mq直接处理的是消息,有点redis的意思

mysql存储引擎总结

主要就两个 myisam 和 innodb

myisam这里有详细的例子:
http://hfhwan.iteye.com/blog/375611
总结:
没有事物,表锁,读写互斥的锁,锁只能锁当前session,不影响其他用户,适合读多于写的数据。
存在一个系统变量 concurrent_insert 允许并发插入的设置

innodb的例子:
http://blog.chinaunix.net/uid-24111901-id-2627857.html
总结:
有事务,可以行锁,行锁前提是有索引否则又变成了表锁,而且锁上别的session就影响了。适合写入修改多的情况。
锁是对索引加的,两个查询相同索引的内容也会有影响。。。
涉及到钱的用这个

网站性能优化

总结下自己的简单看法。
最简单的方式,增加缓存,能让读取速度有明显的提升,程序改动也不大。

然后就是某些逻辑比较复杂的操作,就需要修改比较多的代码了。
用客户端的说法就是,主线程不能锁。
复杂的逻辑放队列中,用户不一定要立即出现结果,重要的是不阻塞。
数据库mysql读写分离。能提高性能。

有新的想法以后再补上。

jvm优化

可能出现的异常:
OutOfMemoryError:java heap size
OutOfMemoryError: PermGen space”

两种都是内存溢出,
heap size是说申请不到新的内存了,这个很常见,检查应用或调整堆内存大小。
“PermGen space”是因为永久存储区满了,这个也很常见,一般在热发布的环境中出现,是因为每次发布应用系统都不重启,久而久之永久存储区中的死对象太多导致新对象无法申请内存,一般重新启动一下即可。

tomcat设置自动加载的时候可能会出现

在声明的时候注意点,别在循环里声明新对象,不用了给设null,如果还不够就得考虑增加jvm最大内存数了。。

为什么会产生StackOverflowError?
答:因为一个线程把Stack内存全部耗尽了,一般是递归函数造成的。

参考:
http://blog.csdn.net/chinajane163/article/details/7784521

ActiveMQ helloworld

apache官网下载了5.9.1很扯淡的,3次无法解压成功,换了4次镜像,算是解压好了。。。
activemq.bat运行显示:

WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directo
ry: E:\workspace\apache-activemq-5.9.1\bin\..\data\kahadb only has 35873 mb of u
sable space – resetting to maximum available disk space: 35873 mb
ERROR | Temporary Store limit is 51200 mb, whilst the temporary data directory:
E:\workspace\apache-activemq-5.9.1\bin\..\data\localhost\tmp_storage only has 35
873 mb of usable space – resetting to maximum available 35873 mb.

出了个错误,不过能正常使用,
目前还不知道有什么影响。

后台地址
http://localhost:8161/admin
admin/admin
可以正常打开

运行webappdemo
提示
missing brokerURL (specified via org.apache.activemq.brokerURL init-Param) or embedded broker

呃呃,又找了个简单例子:
代码来源:http://blog.csdn.net/jason5186/article/details/9196041
发送:
public class JmsSender {
public static void main(String[] args) throws JMSException{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(“tcp://127.0.0.1:61616”);

Connection connection = connectionFactory.createConnection();
connection.start();
;// connection.

Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(“Test.foo”);

MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for(int i=0; i<100; i++) { int id = i+1; ObjectMessage message = session.createObjectMessage(); message.setObject(new User(id, "张三"+id, "123456")); producer.send(message); } session.commit(); session.close(); connection.close(); } } 之后能在后台看见队列中的内容 接收 public class JmsReceiver { public static void main(String[] args) throws JMSException, InterruptedException{ ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("Test.foo"); MessageConsumer consumer = session.createConsumer(destination); //listener 方式 consumer.setMessageListener(new MessageListener() { public void onMessage(Message msg) { ObjectMessage message = (ObjectMessage) msg; //TODO something.... try { User user = (User) message.getObject(); System.out.println("收到消息:"+user); } catch (JMSException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { session.commit(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); TimeUnit.MINUTES.sleep(1); session.close(); connection.close(); } }

servlet中的监听器

http://www.imooc.com/learn/271

慕客好地方,比较全面的说了下监听器的功能和作用。

作用1:
启动的时候初始化,比如像spring那样可以预先加载需要调用的class文件,避免用的时候直接classforname影响性能。
作用2:
统计在线用户,可以监听session的创建和销毁,创建时session监听获得不到ip需要从request监听中获取。

另外Servlet3 中可以通过注解增加监听,不需要修改web.xml。运行环境需要java6,tomcat7。

就酱。