-
TB级社工库搭建
2013年07月16日 安全技术 热度1611 查看评论
习科论坛挖来的,虽然我也看不太懂,先留着吧
复制代码世界的本质,就是数据 --不知道谁说的
我们由上面一句话来引入我们今天所要讨论的主题。
这几年最火的项目是什么?当然是各种海量据量的处理,存储。我们已经迎来了大数据时代
大数据的科学价值和社会价值体现在于可以将数据转化为经济价值的来源。当然不仅仅是这样。复制代码2009年H1N1流感爆发的时候,谷歌通过45亿数据建模分析每天保存的30亿条搜索指令,率先于疾病控制中心,判断出流感的传播的源头
2012年Farecast恭喜2000亿条飞行数据记录,分析出每一条航线上每一架飞机内的每一个位置一年内最便宜的价位。
....
当然,上述大数据的例子都设计到数据挖掘,这是我们以后可以讨论的
我相信,各位资深黑阔手上有不少裤子,像我们这种电脑硬盘才320G的渣渣,估计一个裤子就能装满- -。裤子大了,数据量大了,相信在查找的时候,那种卡个
一天半夜才搜索出结果的感觉,肯定是非常不爽。
不知道大家看没看过道哥写的一篇关于中国地下某黑客:中国黑客传说:游走在黑暗中的精灵
文中有一段是这样的:对于V来说,想知道谁的密码,只需要在库中简单的查询一下,马上就得到了。有时候密码经过了哈希算法的处理,需要经过彩虹表的反查,或者V会拿到一个云计算中心去用分布式计算框架破解(对技术有疑惑或者有兴趣的读者可以参阅拙作《白帽子讲Web安全》)。所以V能够直接查到女孩的邮箱密码。
V估计使用的就是某搜索引擎框架(或者其他方法),搜索分布式存储的裤子。如果有加密,则通过分布式计算框架进行破解(比如hadoop) 由于我自己手上的几个项目,有涉及到搭建分布式计算集群(hadoop),分布式缓存nosql数据库(redis),数据挖掘R语言,搜索引擎(lucene) 等等,所以对这方面稍微了解一点点、
所以今天我们的主题是,咱也装个b,搭建一个超快速TB级的社工库,简单的说,就是自己搭建一个搜索引擎,利用搜索引擎快速查找海量裤子中的数据(裤子当然得自己找),由于不涉及到数据挖掘,所以相对来说比较简单。
我们来看一下数据单位的换算关系:复制代码最小的基本单位是Byte,按顺序给出所有单位:bit、Byte、KB、MB、GB、TB、PB、EB、ZB、YB、DB、NB,
它们按照进率1024(2的十次方)来计算:
1Byte = 8 Bit
1 KB = 1,024 Bytes
1 MB = 1,024 KB = 1,048,576 Bytes
1 GB = 1,024 MB = 1,048,576 KB = 1,073,741,824 Bytes
1 TB = 1,024 GB = 1,048,576 MB = 1,073,741,824 KB = 1,099,511,627,776 Bytes
那么我们要用到的搜索引擎框架是基于Lucene的Hibernate Search.
lucene查询效率分析:复制代码http://www.stanford.edu/class/archive/cs/cs276a/cs276a.1032/projects/reports/dsu800.pdf
使用lucene的公司有apple,ibm,eclipse,wiki,twitter等等
我们先来看看twitter对搜索性能的要求:复制代码Our demands on the new system are immense: With over 1,000 TPS (Tweets/sec) and 12,000 QPS (queries/sec) = over 1 billion queries per day (!) we already put a very high load on our machines. As we want the new system to last for several years, the goal was to support at least an order of magnitude more load.
所以大家不用质疑lucene搜索引擎的效率。由于Lucene是不完整的搜索引擎,它是一个java的工具包。所以需要我们去开发。下面我们来开始进入今天最重要的部分。搭建以及编码。
噢,忘了说,我们用的语言是java,搭配SSH框架,mysql数据库(不使用数据库,直接索引文件也是可以的).
首先导入所有jar包,包括lucene:
SSH配置文件,包含了数据库的账号密码连接信息:接着创建一个被搜索类(在实体上标志@Indexed,表示被索引),我们假设为Victim,拥有id和name两个字段属性:复制代码<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DAO -->
<context:annotation-config/>
<context:component-scan base-package="cn.edu">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/h_search" />
<property name="username" value="root" />
<property name="password" value="123" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>cn.edu.fjnu.Victim</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
current_session_context_class=thread
javax.persistence.validation.mode=none
hibernate.format_sql=true
hibernate.use_sql_comments=true
hibernate.search.default.directory_provider=filesystem
hibernate.search.default.indexBase=E:\\java\\lucene_index
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
好了,我们可以进行测试。默认情况下,在你进行数据库插入更新等操作,Lucene会自动监听并且更新索引:复制代码@Indexed
@Entity
public class Victim {
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id
@GeneratedValue
private Integer id;
@Field(index=Index.YES, store=Store.YES)
private String name;
public Victim() {}
// standard getters/setters follow here
}
我们可以用luke这个工具来查看lucene生成的索引:复制代码@Test
public void testAdd() {
SessionFactory sessionFactory = (SessionFactory) TestUtil.ctx
.getBean("sessionFactory");
Session session = sessionFactory.openSession();
int i = 20;
Transaction trans = null;
trans = session.beginTransaction();
while (i < 40) {
Victim victim= new Victim();
author.setName("王尼玛" + i);
i++;
session.save(victim);
}
trans.commit();
}
成功生成索引。
我们可以通过以下方法查询:当然我们只是在自己一台机子上,lucene支持分布式扩展,也就是说,你加入的机子越多,速度也越来越快。理论上无限量扩展。包括把裤子扔到分布式存储上。复制代码@Test
public void testSearch() {
SessionFactory sessionFactory = (SessionFactory) TestUtil.ctx
.getBean("sessionFactory");
Session session = sessionFactory.openSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity( Victim.class ).get();
org.apache.lucene.search.Query query = qb
.keyword()
//要搜素name字段
.onFields("name")
.matching("王尼玛")
.createQuery();
// wrap Lucene query in a org.hibernate.Query
org.hibernate.Query hibQuery =
fullTextSession.createFullTextQuery(query, Book.class);
// execute search,返回结果到result中
List result = hibQuery.list();
相关日志:
- 搜索
- 文章归档
-
- 2023年7月 (19)
- 2023年6月 (20)
- 2023年5月 (4)
- 2022年11月 (11)
- 2022年10月 (9)
- 2022年7月 (22)
- 2022年6月 (39)
- 2022年5月 (17)
- 2022年4月 (1)
- 2017年3月 (1)
- 2016年11月 (1)
- 2015年11月 (6)
- 2015年8月 (24)
- 2015年7月 (43)
- 2015年6月 (28)
- 2015年5月 (34)
- 2015年4月 (38)
- 2015年3月 (35)
- 2015年2月 (28)
- 2015年1月 (31)
- 2014年12月 (14)
- 2014年11月 (8)
- 2014年10月 (8)
- 2014年9月 (7)
- 2014年8月 (13)
- 2014年7月 (22)
- 2014年6月 (26)
- 2014年5月 (14)
- 2014年4月 (16)
- 2014年3月 (13)
- 2014年2月 (17)
- 2014年1月 (23)
- 2013年12月 (19)
- 2013年11月 (18)
- 2013年10月 (17)
- 2013年9月 (15)
- 2013年8月 (21)
- 2013年7月 (15)
- 2010年8月 (1)
- 最近发表
- 站点信息
-
- 文章总数:782
- 页面总数:2
- 分类总数:14
- 标签总数:522
- 评论总数:359
- 浏览总数:624719