lighttpd configure java web server

我的apache 2.061在window 2003 下老是报告错误,虽然也可以使用但感觉很不爽.
错误入下,找了很久也没有办法,包括修复sockt也没用.
报告队列中的错误: 错误应用程序 Apache.exe,版本 2.0.61.200,错误模块 ntdll.dll,版本 5.2.3790.3959,错误地

一直想找一个替代apache的软件,最近看了lighttpd想用lighttpd来替代apache,先下放过资料.
下载地址.
http://ftp.dtech.hu/pub/WLMP-Project/LightTPD-Win32/



. When to use lighttpd


You can use lighttpd to



  • secure access to your application server
  • reduce load on your server by offloading static requests
  • load balance your application servers
  • use lighttpd’s spambot and bad bot blocking capabilities
  • get more request rewriting and redirecting flexibility
  • use the above flexibility to improve your search engine rankings
  • profit.

2. When not to use lighttpd


You might not like lighttpd if you



  • don’t like configuring software
  • use URL rewriting and ;jsessionid (though a patch is available for this problem).

3. lighttpd modules you need


The following lighty modules are needed:



  • mod_access
  • mod_redirect
  • mod_rewrite
  • mod_proxy

Add them to your server.modules section:

server.modules = (
“mod_accesslog”,
“mod_access”,
“mod_redirect”,
“mod_rewrite”,
“mod_proxy”
,
“mod_status”,
“mod_evhost”,
“mod_expire”
)

4. Denying access to JEE directories


The WEB-INF and META-INF directories shouldn’t be accessible through lighttpd. Files from your development environment also shouldn’t be visible.

url.access-deny = ( “WEB-INF”, “.classpath”, “.project”, “META-INF” )

5. Binding your application server to localhost


To prevent duplicate content penalties, your application server shouldn’t be visible from the web. Even if you run it on a high port, someone might eventually find it.


Binding a web site to localhost looks like this in Orion’s <name>-web-site.xml:

<web-site host=”127.0.0.1″ port=”12345″>
<frontend host=”johannburkard.de” port=”80″/>

Consult your documentation if you aren’t using Orion.


6. Redirecting www. to non-www. hosts


Even if you don’t really need to do this, I recommend doing so. Removing duplicate content will improve your rankings.


The following snippet redirects all visitors from www.<domain> to <domain> with a 301 permanent redirect.

$HTTP[“host”] =~ “^www\.(.*)___FCKpd___3quot; {
url.redirect = ( “^/(.*)” => “http://%1/$1” )
}

You should also redirect all additional domains (johannburkard.com, johann-burkard.org) to your main domain.


7. Proxying dynamic requests


We will use mod_proxy to proxy some requests to your Java application server.


Depending on your site’s structure, one of the following approaches will work better.


Simple JSP


If all you have is a bunch of Java Server Pages, the following mod_proxy rule is sufficient:

proxy.server = ( “.jsp” =>
(
( “host” => “127.0.0.1”,
“port” => “12345”
)
)
)

Note that the JSP must be actual files. You cannot use Servlets mapped to these URIs.


Applications


If you use Servlets or more complex applications, you can proxy URIs by prefix:

proxy.server = ( “/blog/” =>
(
( “host” => “127.0.0.1”,
“port” => “12345”
)
)
)

Proxying with exceptions


If most of your site is dynamic and you have a directory for static content (/assets, /static or so), you can proxy all requests except requests for static files:

$HTTP[“url”] !~ “^/static” {
proxy.server = ( “” =>
(
( “host” => “127.0.0.1”,
“port” => “12345”
)
)
)
}

8. Rewriting requests


lighttpd can dynamically rewrite requests. I mostly use this to use default.jsp as dynamic index file instead of index.html. Here’s an example:

url.rewrite-once = ( “^(.*)/___FCKpd___7quot; => “$1/default.jsp”,
“^(.*)/([;?]+.*)___FCKpd___7quot; => “$1/default.jsp$2” )

This is visible at gra0.com and internally rewrites all requests from / to /default.jsp (including jsessionid and query string).


mod_rewrite can also be used to make URLs shorter. For example, to remove the ?page=comments query string, I use the following:

url.rewrite-once = (
“^/blog/(.*)\.html___FCKpd___8quot; => “/blog/$1.html?page=comments”
)

9. Redirecting requests


You can use mod_redirect to redirect the user to a different URL. Contrary to mod_rewrite where the request is rewritten, a 301 permanent redirect will be sent to the browser.


In this example, I’m redirecting requests to an old domain to a new domain:

$HTTP[“host”] == “olddomain.com” {
url.redirect = (
“^/(.*)___FCKpd___9quot; => “http://newdomain.com/$1”
)
}

10. More things to be aware of



  • The only IP address in your application server log files should be 127.0.0.1. If you need the original address, log the X-FORWARDED-FOR header.
  • Don’t analyze both lighttpd and application server logs – lighty’s log files already contain all requests.
  • You might want to set up virtual hosts sooner or later.
  • Use mod_expire to make resources cacheable. Doing so can make your site a lot faster and save you money.

用mysqldumpslow分析mysql的slow query log

mysql有一个功能就是可以log下来运行的比较慢的sql语句,默认是没有这个log的,为了开启这个功能,要修改my.cnf或者在mysql启动的时候加入一些参数。如果在my.cnf里面修改,需增加如下几行

long_query_time = 1
log-slow-queries = /var/youpath/slow.log
log-queries-not-using-indexes

long_query_time 是指执行超过多久的sql会被log下来,这里是1秒。
log-slow-queries 设置把日志写在那里,可以为空,系统会给一个缺省的文件host_name-slow.log,我生成的log就在mysql的data目录
log-queries-not-using-indexes 就是字面意思,log下来没有使用索引的query。

把上述参数打开,运行一段时间,就可以关掉了,省得影响生产环境。

接下来就是分析了,我这里的文件名字叫host-slow.log。
先mysqldumpslow –help以下,俺主要用的是
-s ORDER what to sort by (t, at, l, al, r, ar etc), ‘at’ is default
-t NUM just show the top n queries
-g PATTERN grep: only consider stmts that include this string

-s,是order的顺序,说明写的不够详细,俺用下来,包括看了代码,主要有
c,t,l,r和ac,at,al,ar,分别是按照query次数,时间,lock的时间和返回的记录数来排序,前面加了a的时倒叙
-t,是top n的意思,即为返回前面多少条的数据
-g,后边可以写一个正则匹配模式,大小写不敏感的

mysqldumpslow -s c -t 20 host-slow.log
mysqldumpslow -s r -t 20 host-slow.log

上述命令可以看出访问次数最多的20个sql语句和返回记录集最多的20个sql。
mysqldumpslow -t 10 -s t -g “left join” host-slow.log
这个是按照时间返回前10条里面含有左连接的sql语句。

用了这个工具就可以查询出来那些sql语句是性能的瓶颈,进行优化,比如加索引,该应用的实现方式等。

Configure lighttpd alias (mod_alias)


This lighttpd module provides for mapping different parts of the host filesystem in the document tree. You can use it for mapping various directories. For example cgi-bin directory mapped to /var/lib/cgi-bin.


Configuration


Open your lighttpd configuration file:


vi /etc/lighttpd/lighttpd.conf


Append/add mod_ alias to list of server modules:
server.modules += ( “mod_alias” )


Examples


Add cgi-bin alias for doamin theos.in
alias.url = ( “/cgi-bin/” => “/home/lighttpd/theos.in/cgi-bin/” )


Browse all documents installed at /usr/share/doc/ directory with following alias:
alias.url = ( “/docs/” => “/usr/share/doc/” )
alias.url += ( “/stats/” => “/home/theos.in/http/webalizer/” )


Open a browser and type url http://theos.in/docs/ or http://your-domain.com/docs/


机子无故变慢?——硬盘的DMA模式变成了PIO模式

如果你的机子变得很慢,甚至连播放影片都有卡声音的现象,又差不

出任何病毒,怎么回事?很可能是你的机子的硬盘的DMA模式变成了PI

O模式了。

  硬盘的DMA模式大家应该都知道吧,硬盘的PATA模式有DMA33、DMA

66、DMA100和DMA133,最新的SATA-150都出来了!一般来说现在大多

数人用的还是PATA模式的硬盘,硬盘使用DMA模式相比以前的PIO模式

传输的速度要快2~8倍。DMA模式的起用对系统的性能起到了实质的作

用。以前有很多文章介绍过如何打开DMA模式,我在这里也就不多说了


  但笔者发现一个新的“问题”——Windows2000、XP、2003系统有

时会自行关闭硬盘的DMA模式,自动改用PIO模式运行!这就造成在使

用以上系统中硬盘性能突然下降,其中最明显的现象有:系统起动速

度明显变慢,一般来说正常WindowsXP系统启动时那个由左向右运动的

滑条最多走2~4次系统就能启动,但这一问题发生时可能会走5~8次

或更多!而且在运行系统时进行硬盘操作时明显感觉变慢,在运行一

些大的软件时CPU占用率时常达到100%而产生停顿,玩一些大型3D游戏

时(比如极品飞车6)画面时有明显停顿(很多人这时以为是自己的显

卡问题,当然如果你使用的是三年前的显卡玩这些游戏是有点老了,

但如果你用的是今年才买的GF显卡那就不是它的问题了),出现以上

问题时大家最好看看自己硬盘的DMA模式是不是被Windows系统自行关

闭了。查看自己的系统是否打开DMA模式:

  1. 双击“管理工具”,然后双击“计算机管理”;

  2. 单击“系统工具”,然后单击“设备管理器”;

  3. 展开“IDE ATA/ATAPI 控制器”节点;

  4. 双击您的“主要IDE控制器”;

  5. 点击“高级设置”。

  看到“设备0”,下面的传输模式应设为“DMA(若可用)”,再

下面“当前传输模式”,如果是“Ultra DMAMode*(*为数字,DMA33

为2,DMA66为4,DMA100为5、DMA133为6)”,那么你的系统正常,但

如果以前你自己设的是“UltraDMAMode

6”又没有改动,而现在是“Ultra DMA Mode 4”或“Ultra DMA

Mode2”更或者是“PIO模式”而且改不过来!可能就是系统自行关闭

了DMA模式了。

  分析问题:

  好了,先来说说问题是如何产生的。在Windows 2000/XP/2003中

有这样一个设定: WindowsIDE/ATAPI端口驱动程序 (Atapi.sys) 累

积收到总共6个超时或循环冗余检验(CRC)错误后,驱动程序将把通信

速度(传送模式)从最快的直接内存访问 (DMA) 模式分步骤降为较慢

的DMA模式。如果驱动程序继续收到超时或 CRC 错误,则驱动程序最

终将把传送模式降为最慢的模式(PIO模式)。问题就在这里!一般来

说一个正常的硬盘很少会有超时或循环冗余检验(CRC)错误,但是当我

们使用这些系统里的挂起(也有叫休眠)并恢复计算机后就很容易造

成超时或循环冗余检验(CRC)错误。因为系统设定的超时值为4秒,当

系统向 ATA 磁盘发出读取请求时如果硬盘回应时间超过 4秒的超时值

时才会产生超时或循环冗余检验(CRC)错误,但因为系统在挂起(也有

叫休眠)时硬盘是在停转状态中,恢复计算机时硬盘有个从停止到运

动的过程,这就很容易造成大部分硬盘回应时间超过4秒的超时值。也

就是说当我们使用计算机挂起6次后系统就会把通信速度(传送模式)

从最快的直接内存访问 (DMA)模式分步骤降为较慢的DMA 模式。如果

挂起6次以上则驱动程序最终将把传送模式降为最慢的模式(PIO 模式

)。这就是Windows系统突然变慢的原因。1

  解决问题:

  若要为受影响的设备重新启用典型DMA或更快DMA的传送模式,请

执行以下操作:

  1. 双击“管理工具”,然后双击“计算机管理”;

  2. 单击“系统工具”,然后单击“设备管理器”;

  3. 展开“IDE ATA/ATAPI 控制器”节点;

  4. 双击您要为其恢复典型 DMA 传送模式的控制器;

  5. 单击“驱动程序”选项卡;

  6. 单击“卸载”。

  当此过程完成后,重新启动您的计算机。当Windows重新启动后,

将重新枚举硬盘控制器,同时与该控制器相连的每个设备的传送模式

将重置为默认值。

  但这样只是把硬盘设备的传送模式重置为默认值,当我们再使用

挂起系统时系统又会自行关闭DMA模式!所以我们还要做以下操作:

  1. 单击“开始”,单击“运行”,键入 Regedit,然后单击“确

定”。

  2. 在注册表中找到并单击以下项:

  HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D3

6E96A-E325-11CE-BFC1-08002BE10318}001

  和

  HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D3

6E96A-E325-11CE-BFC1-08002BE10318}002

  3. 在“编辑”菜单上,指向“新建”,然后单击“DWORD 值”。

  4. 键入 ResetErrorCountersOnSuccess,然后按 Enter 键。

  5. 在“编辑”菜单上,单击“修改”。

  6. 键入 l,然后单击“确定”。

  7. 按照下列步骤操作,然后退出注册表编辑器:

  注意:上面注册表最后的项“{4D36E96A-E325-11CE-BFC1-08002B

E10318}”列出的带有“0001”编号的子项对应于一台仅有一个IDE控

制器的计算机的主要 IDE 通道和次要 IDE 通道。如果您的计算机有

两个 IDE 控制器,则对于每个控制器,主要 IDE通道和次要IDE 通道

的带有编号的子项分别是:“0001、0002、0003、0004、0005、0006

……”。

  为检查是否找到了正确的子项,请检查子项的 DriverDesc 值是

包含字符串值“主要 IDE通道”,还是包含字符串值“次要IDE 通道

”。

  建立以上注册表键值的目的是:

  在Windows中,使系统向 ATA 磁盘发出读取请求时的 4 秒的超时

值更改为10秒。还实现了一种较不严格的变通策略以在发生超时错误

和 CRC 错误时降低传送模式(从较快的 DMA 模式到较慢的DMA模式,

最终降为 PIO 模式)。以前的情况是,在每当累积发生的超时错误或

CRC 错误的总数达到 6次时,IDE/ATAPI端口驱动程序 (Atapi.sys)将

降低传送模式。当我们建立以上注册表键值实现新的策略后,Atapi.s

ys 仅在连续发生 6次超时错误或 CRC错误后才降低传送模式。

  这一新的策略实现后将大大降低系统降低或关闭DMA传送模式的机

会。现在我们可以安心地使用挂起系统和高性能的DMA硬盘模式,再也

不会发生系统性能突然下降的情况了。

盜版的 Windows XP 有福了!微軟發佈 VLK 大量授權序號!

盜版的 Windows XP PRO 作業系統使用者有福了!

微軟公佈了 VLK 大量授權序號。

更改序號後,無需使用網路上的破解驗證,

使用此序號者,皆可正大光明的通過微軟驗證!

以下序號皆為 VLK 大量授權序號:

HCQ9D-TVCWX-X9QRG-J4B2Y-GR2TT
MRX3F-47B9T-2487J-KWKMF-RPWBY
QC986-27D34-6M3TY-JJXP9-TBGMD
CM3HY-26VYW-6JRYC-X66GX-JVY2D
DP7CM-PD6MC-6BKXT-M8JJ6-RPXGJ
F4297-RCWJP-P482C-YY23Y-XH8W3   
HH7VV-6P3G9-82TWK-QKJJ3-MXR96

所有序號皆適用於 Windows XP/SP1/SP2

【使用說明】:

 1.開啟 KeyFinder, 點選 Option 的 Change Windows Key。

 2.輸入以上 VLK 大量授權序號,任選一組(一定要輸入正確,否會失敗)。

 3.完畢後點選 Change 會出現成功訊息,按下 OK 後,在按 Close。


 

      完畢後點選微軟驗證

巧用tmpfs加速你的linux服务器

巧用tmpfs加速你的linux服务器


今天从朋友高春辉那里又学了一招,就是使用tmpfs,我把他消化后用来实现虚拟磁盘来存放squid的缓存文件和php的seesion。速度快不少哦!


默认系统就会加载/dev/shm ,它就是所谓的tmpfs,有人说跟ramdisk(虚拟磁盘),但不一样。象虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储。而且传统的虚拟磁盘是个块设备,并需要一个 mkfs 之类的命令才能真正地使用它,tmpfs 是一个文件系统,而不是块设备;您只是安装它,它就可以使用了。


tmpfs有以下优势:
1。动态文件系统的大小,
2。tmpfs 的另一个主要的好处是它闪电般的速度。因为典型的 tmpfs 文件系统会完全驻留在 RAM 中,读写几乎可以是瞬间的。
3。tmpfs 数据在重新启动之后不会保留,因为虚拟内存本质上就是易失的。所以有必要做一些脚本做诸如加载,绑定的操作。


好了讲了一些大道理,大家看的烦了吧,还是讲讲我的应用吧:)


首先在/dev/stm建个tmp文件夹,然后与实际/tmp绑定


mkdir /dev/shm/tmp
chmod 1777 /dev/shm/tmp
mount –bind /dev/shm/tmp /tmp



1。squid的缓存目录设置


vi /etc/squid/squid.conf


修改成
cache_dir ufs /tmp 256 16 256
这里的第一个256表示使用256M内存,我觉得高性能LINUX双效防火墙HOWTO使用ramdisk的方法还不如直接使用tmpfs,至少每次启动不用mkfs,还可以动态改变大小。


然后重启一下服务,ok,现在所有的squid缓存文件都保存倒tmpfs文件系统里了,很快哦。



2。对php性能的优化


对于一个访问量大的以apache+php的网站,可能tmp下的临时文件都会很多,比如seesion或者一些缓存文件,那么你可以把它保存到tmpfs文件。


保存seesion的方法很简单了只要修改php.ini就行了,由于我已经把/dev/stm/tmp与/tmp绑定,所以不改写也行,至于php程序产生的缓存文件那只能改自己的php程序了:)


希望我的这个方法,能对你有所启发。


参考文档:使用虚拟内存(virtual memory,VM)文件系统和绑定安装


lucene的简单实例<一>

 说明一下,这一篇文章的用到的lucene,是用2.0版本的,主要在查询的时候2.0版本的lucene与以前的版本有了一些区别.
其实这一些代码都是早几个月写的,自己很懒,所以到今天才写到自己的博客上,高深的文章自己写不了,只能记录下一些简单的记录与点滴,其中的代码算是自娱自乐的,希望高手不要把重构之类的砸下来…

1、在windows系统下的的C盘,建一个名叫s的文件夹,在该文件夹里面随便建三个txt文件,随便起名啦,就叫”1.txt”,”2.txt”和”3.txt”啦
其中1.txt的内容如下:



Java代码


  1. 中华人民共和国  

  2. 全国人民  

  3. 2006年  

而”2.txt”和”3.txt”的内容也可以随便写几写,这里懒写,就复制一个和1.txt文件的内容一样吧

2、下载lucene包,放在classpath路径中
建立索引:



Java代码


  1. package lighter.javaeye.com;  

  2.   

  3. import java.io.BufferedReader;  

  4. import java.io.File;  

  5. import java.io.FileInputStream;  

  6. import java.io.IOException;  

  7. import java.io.InputStreamReader;  

  8. import java.util.Date;  

  9.   

  10. import org.apache.lucene.analysis.Analyzer;  

  11. import org.apache.lucene.analysis.standard.StandardAnalyzer;  

  12. import org.apache.lucene.document.Document;  

  13. import org.apache.lucene.document.Field;  

  14. import org.apache.lucene.index.IndexWriter;  

  15.   

  16. /** 

  17.  * author lighter date 2006-8-7 

  18.  */  

  19. public class TextFileIndexer {  

  20.     public static void main(String[] args) throws Exception {  

  21.         /* 指明要索引文件夹的位置,这里是C盘的S文件夹下 */  

  22.         File fileDir = new File(“c:\\s”);  

  23.   

  24.         /* 这里放索引文件的位置 */  

  25.         File indexDir = new File(“c:\\index”);  

  26.         Analyzer luceneAnalyzer = new StandardAnalyzer();  

  27.         IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,  

  28.                 true);  

  29.         File[] textFiles = fileDir.listFiles();  

  30.         long startTime = new Date().getTime();  

  31.           

  32.         //增加document到索引去  

  33.         for (int i = 0; i < textFiles.length; i++) {  

  34.             if (textFiles[i].isFile()  

  35.                     && textFiles[i].getName().endsWith(“.txt”)) {  

  36.                 System.out.println(“File “ + textFiles[i].getCanonicalPath()  

  37.                         + “正在被索引….”);  

  38.                 String temp = FileReaderAll(textFiles[i].getCanonicalPath(),  

  39.                         “GBK”);  

  40.                 System.out.println(temp);  

  41.                 Document document = new Document();  

  42.                 Field FieldPath = new Field(“path”, textFiles[i].getPath(),  

  43.                         Field.Store.YES, Field.Index.NO);  

  44.                 Field FieldBody = new Field(“body”, temp, Field.Store.YES,  

  45.                         Field.Index.TOKENIZED,  

  46.                         Field.TermVector.WITH_POSITIONS_OFFSETS);  

  47.                 document.add(FieldPath);  

  48.                 document.add(FieldBody);  

  49.                 indexWriter.addDocument(document);  

  50.             }  

  51.         }  

  52.         //optimize()方法是对索引进行优化  

  53.         indexWriter.optimize();  

  54.         indexWriter.close();  

  55.           

  56.         //测试一下索引的时间  

  57.         long endTime = new Date().getTime();  

  58.         System.out  

  59.                 .println(“这花费了”  

  60.                         + (endTime – startTime)  

  61.                         + ” 毫秒来把文档增加到索引里面去!”  

  62.                         + fileDir.getPath());  

  63.     }  

  64.   

  65.     public static String FileReaderAll(String FileName, String charset)  

  66.             throws IOException {  

  67.         BufferedReader reader = new BufferedReader(new InputStreamReader(  

  68.                 new FileInputStream(FileName), charset));  

  69.         String line = new String();  

  70.         String temp = new String();  

  71.           

  72.         while ((line = reader.readLine()) != null) {  

  73.             temp += line;  

  74.         }  

  75.         reader.close();  

  76.         return temp;  

  77.     }  

  78. }  


索引的结果:



Java代码


  1. File C:\s\1.txt正在被索引….  

  2. 中华人民共和国全国人民2006年  

  3. File C:\s\2.txt正在被索引….  

  4. 中华人民共和国全国人民2006年  

  5. File C:\s\3.txt正在被索引….  

  6. 中华人民共和国全国人民2006年  

  7. 这花费了297 毫秒来把文档增加到索引里面去!c:\s  


3、建立了索引之后,查询啦….



Java代码


  1. package lighter.javaeye.com;  

  2.   

  3. import java.io.IOException;  

  4.   

  5. import org.apache.lucene.analysis.Analyzer;  

  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;  

  7. import org.apache.lucene.queryParser.ParseException;  

  8. import org.apache.lucene.queryParser.QueryParser;  

  9. import org.apache.lucene.search.Hits;  

  10. import org.apache.lucene.search.IndexSearcher;  

  11. import org.apache.lucene.search.Query;  

  12.   

  13. public class TestQuery {  

  14.     public static void main(String[] args) throws IOException, ParseException {  

  15.         Hits hits = null;  

  16.         String queryString = “中华”;  

  17.         Query query = null;  

  18.         IndexSearcher searcher = new IndexSearcher(“c:\\index”);  

  19.   

  20.         Analyzer analyzer = new StandardAnalyzer();  

  21.         try {  

  22.             QueryParser qp = new QueryParser(“body”, analyzer);  

  23.             query = qp.parse(queryString);  

  24.         } catch (ParseException e) {  

  25.         }  

  26.         if (searcher != null) {  

  27.             hits = searcher.search(query);  

  28.             if (hits.length() > 0) {  

  29.                 System.out.println(“找到:” + hits.length() + ” 个结果!”);  

  30.             }  

  31.         }  

  32.     }  

  33.   

  34. }  


其运行结果:

引用

找到:3 个结果!

具体的API的用法,这里就不说了,具体的做法参考lucene的官方文档吧…

lucene简单实例<二>

 写文章的时候,感觉比较难写的就是标题,有时候不知道起什么名字好,反正这里写的都是关于lucene的一些简单的实例,就随便起啦.

Lucene 其实很简单的,它最主要就是做两件事:建立索引和进行搜索
来看一些在lucene中使用的术语,这里并不打算作详细的介绍,只是点一下而已—-因为这一个世界有一种好东西,叫搜索。

IndexWriter:lucene中最重要的的类之一,它主要是用来将文档加入索引,同时控制索引过程中的一些参数使用。

Analyzer:分析器,主要用于分析搜索引擎遇到的各种文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。

Directory:索引存放的位置;lucene提供了两种索引存放的位置,一种是磁盘,一种是内存。一般情况将索引放在磁盘上;相应地lucene提供了FSDirectory和RAMDirectory两个类。

Document:文档;Document相当于一个要进行索引的单元,任何可以想要被索引的文件都必须转化为Document对象才能进行索引。

Field:字段。

IndexSearcher:是lucene中最基本的检索工具,所有的检索都会用到IndexSearcher工具;

Query:查询,lucene中支持模糊查询,语义查询,短语查询,组合查询等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些类。

QueryParser: 是一个解析用户输入的工具,可以通过扫描用户输入的字符串,生成Query对象。

Hits:在搜索完成之后,需要把搜索结果返回并显示给用户,只有这样才算是完成搜索的目的。在lucene中,搜索的结果的集合是用Hits类的实例来表示的。

上面作了一大堆名词解释,下面就看几个简单的实例吧:
1、简单的的StandardAnalyzer测试例子



Java代码


  1. package lighter.javaeye.com;  

  2.   

  3. import java.io.IOException;  

  4. import java.io.StringReader;  

  5.   

  6. import org.apache.lucene.analysis.Analyzer;  

  7. import org.apache.lucene.analysis.Token;  

  8. import org.apache.lucene.analysis.TokenStream;  

  9. import org.apache.lucene.analysis.standard.StandardAnalyzer;  

  10.   

  11. public class StandardAnalyzerTest   

  12. {  

  13.     //构造函数,  

  14.     public StandardAnalyzerTest()  

  15.     {  

  16.     }  

  17.     public static void main(String[] args)   

  18.     {  

  19.         //生成一个StandardAnalyzer对象  

  20.         Analyzer aAnalyzer = new StandardAnalyzer();  

  21.         //测试字符串  

  22.         StringReader sr = new StringReader(“lighter javaeye com is the are on”);  

  23.         //生成TokenStream对象  

  24.         TokenStream ts = aAnalyzer.tokenStream(“name”, sr);   

  25.         try {  

  26.             int i=0;  

  27.             Token t = ts.next();  

  28.             while(t!=null)  

  29.             {  

  30.                 //辅助输出时显示行号  

  31.                 i++;  

  32.                 //输出处理后的字符  

  33.                 System.out.println(“第”+i+“行:”+t.termText());  

  34.                 //取得下一个字符  

  35.                 t=ts.next();  

  36.             }  

  37.         } catch (IOException e) {  

  38.             e.printStackTrace();  

  39.         }  

  40.     }  

  41. }  

显示结果:

引用

第1行:lighter
第2行:javaeye
第3行:com

提示一下:
StandardAnalyzer是lucene中内置的”标准分析器”,可以做如下功能:
1、对原有句子按照空格进行了分词
2、所有的大写字母都可以能转换为小写的字母
3、可以去掉一些没有用处的单词,例如”is”,”the”,”are”等单词,也删除了所有的标点
查看一下结果与”new StringReader(“lighter javaeye com is the are on”)”作一个比较就清楚明了。
这里不对其API进行解释了,具体见lucene的官方文档。需要注意一点,这里的代码使用的是lucene2的API,与1.43版有一些明显的差别。

2、看另一个实例,简单地建立索引,进行搜索



Java代码


  1. package lighter.javaeye.com;  

  2. import org.apache.lucene.analysis.standard.StandardAnalyzer;  

  3. import org.apache.lucene.document.Document;  

  4. import org.apache.lucene.document.Field;  

  5. import org.apache.lucene.index.IndexWriter;  

  6. import org.apache.lucene.queryParser.QueryParser;  

  7. import org.apache.lucene.search.Hits;  

  8. import org.apache.lucene.search.IndexSearcher;  

  9. import org.apache.lucene.search.Query;  

  10. import org.apache.lucene.store.FSDirectory;  

  11.   

  12. public class FSDirectoryTest {  

  13.   

  14.     //建立索引的路径  

  15.     public static final String path = “c:\\index2”;  

  16.   

  17.     public static void main(String[] args) throws Exception {  

  18.         Document doc1 = new Document();  

  19.         doc1.add( new Field(“name”“lighter javaeye com”,Field.Store.YES,Field.Index.TOKENIZED));  

  20.   

  21.         Document doc2 = new Document();  

  22.         doc2.add(new Field(“name”“lighter blog”,Field.Store.YES,Field.Index.TOKENIZED));  

  23.   

  24.         IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);  

  25.         writer.setMaxFieldLength(3);  

  26.         writer.addDocument(doc1);  

  27.         writer.setMaxFieldLength(3);  

  28.         writer.addDocument(doc2);  

  29.         writer.close();  

  30.   

  31.         IndexSearcher searcher = new IndexSearcher(path);  

  32.         Hits hits = null;  

  33.         Query query = null;  

  34.         QueryParser qp = new QueryParser(“name”,new StandardAnalyzer());  

  35.           

  36.         query = qp.parse(“lighter”);  

  37.         hits = searcher.search(query);  

  38.         System.out.println(“查找\”lighter\” 共” + hits.length() + “个结果”);  

  39.   

  40.         query = qp.parse(“javaeye”);  

  41.         hits = searcher.search(query);  

  42.         System.out.println(“查找\”javaeye\” 共” + hits.length() + “个结果”);  

  43.   

  44.     }  

  45.   

  46. }  

运行结果:



Java代码


  1. 查找“lighter” 共2个结果  

  2. 查找“javaeye” 共1个结果  

Linux user commands

Every user who has access to a Linux system needs a login and a password. Each user must belong to a primary group and for security or access purposes can belong to several secondary groups.


In order to create new logins, modify or delete users, you must already be logged in as root.  The root login is the highest level and only certain individuals should have access to the root account.


useradd – Adding a new user

Options:

  • -d home directory
  • -s starting program (shell)
  • -p password
  • -g (primary group assigned to the users)
  • -G (Other groups the user belongs to)
  • -m (Create the user’s home directory

Example: To add a new user with



  • a primary group of users
  • a second group mgmt
  • starting shell /bin/bash
  • password of xxxx
  • home directory of roger
  • create home directory
  • a login name of roger

useradd -gusers -Gmgmt -s/bin/shell -pxxxx -d/home/roger -m roger


 



usermod – Modifying existing user


Options:



  • -d home directory
  • -s starting program (shell)
  • -p password
  • g (primary group assigned to the users)
  • -G (Other groups the user belongs to)

Example: To add the group ‘others’ to the user roger


usermod -Gothers roger



userdel – Deleting a user


Options:



  • -r (remove home directory)

Example: To remove the user ‘roger’ and his home directory


userdel -r roger



passwd – User’s Password

Options:

  • user’s name (Only required if you are root and want to change another user’s password)

Example: To change the password for the account you are currently logged in as…



passwd
Enter existing password
Enter new password
Enter new password again (to validate)


Example: To change the password for the user ‘roger’ (only you are logged in as root)…



passwd roger
Enter existing password
(can be either roger’s password or root’s password)
Enter new password
Enter new password again (to validate)



Where user and group information stored


User names and primary groups are stored in /etc/passwd. This file can be directly edited using the ‘vi’ editor, although this is not recommended. Format of the file is…



  • User name (normally all lower case)
  • Password (encrypted – only contains the letter ‘x’)
  • User ID (a unique number of each user)
  • Primary Group ID
  • Comment (Normally the person’s full name)
  • Home directory (normally /home/<user name>
  • Default shell (normally /bin/bash)

Each field is separated by a colon.


Passwords for each user are stored in /etc/shadow. This file should only be changed using the passwd command.


Group information is stored in /etc/group. This file can be directly edited using the ‘vi’ editor. Format of the file is…



  • Group name
  • Group password (hardly ever used)
  • Group ID
  • User names (separated by commas)

Each field is separated by a colon.


Default files


When a new user is created, the default files and directories that are created are stored in /etc/skel.


This directory can be modified to fit your needs. Modifications only effect new users and does not change anything for existing users.



su – Switch User


To switch to another user, use the su command. This is most commonly used to switch to the root account.



Example: To switch to root account…
su
Enter root’s passwd


Example: To switch to the user ‘roger’…
su roger
Enter roger’s or root’s passwd


To return to original user, enter exit