- 浏览: 1364932 次
- 性别:
- 来自: 陕西.西安
文章分类
最新评论
-
dare_:
经过试验 设置之后反而更慢没有调用ensureCapacity ...
ensureCapacity()方法提高ArrayList的初始化速度 -
wangchao9053:
[flash=200,200][url][img][list] ...
Only the original thread that created a view hierarchy can touch its views的相关 -
cyb504:
考虑将rb文件代码隐藏:我先使用命令jrubyc将所有rb文件 ...
Ruby学习十 JRuby中调用java代码 -
4562xse3460:
大哥,您这个写反了,差点误导我,我觉得看着就不对。百度第一条就 ...
portrait表示纵向,landscape表示横向 -
yin138:
portrait是肖像画,即竖屏,landscape是风景画, ...
portrait表示纵向,landscape表示横向
http://blog.sina.com.cn/s/blog_473f94590100mwns.html
Solr有个很方便的处理器叫DataImportHandler,可以通过配置配置db-data-config.xml配置各种数据源然后
从中导入数据进行索引,很方便我们进行开发.但是之前从数据库导入数据一直有个问题,就是如果数据库中数据过大,就会导致内存溢出.自己经过阅读源码以及发邮件到Solr邮件列表,终于找到了解决办法,这里拿出来共享.
这里我的Solr版本是Solr1.4.0,数据库是Sql Server2005.其他数据库可能有些不适用(请在其他数据库运行成功的同学也分享下),但根据这个思路应该都有自己的解决方案.
DataImportHandler中从数据库导入数据进行索引主要通过JDBC进行处理.由于自己对JDBC的认识浅薄,一直认为JDBC是一次性将要查询的数据从数据库中数据读取过去.但没有想到其实从数据库获取数据其实也是以流的形式,可以一段段的获取.也就是可以在客户端每获取一条再从流中取新的一条数据如此取完为止(这里感谢高手提示).由于使用的数据库是Sql Server2005,本想通过它的sqljdbc.jar中获取些提示(尝试下源码没有成功),从Jar中大概发现Sql Server有这样的设置,于是上微软官网的MSDN获取到了答案(URL:http://msdn.microsoft.com/zh-cn/library/ms378663(SQL.90).aspx):
Sets the default cursor type that is used for all result sets that are created by using this SQLServerDataSource object.
复制
public void setSelectMethod(java.lang.String selectMethod)
参数
selectMethod
A String value that contains the default cursor type.
备注
The selectMethod is the default cursor type that is used for a result set. This property is useful when you are dealing with large result sets and do not want to store the whole result set in memory on the client side. By setting the property to "cursor," you can create a server-side cursor that can fetch smaller chunks of data at a time. If the selectMethod property is not set, getSelectMethod returns the default value of "direct".
同时这样做的话也要设置Connection的两个属性:ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY.但跟踪源码发现Solr中已经有这样的设置.于是可以直接在db-data-config.xml中配置即可完成,具体配置如下:
<dataSource name="dsSqlServer" type="JdbcDataSource" driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" batchSize="3000" url="jdbc:sqlserver://192.168.1.5:1433; DatabaseName=testDatabase;responseBuffering=adaptive;selectMethod=cursor" user="sa" password="12345" />
其中只要在URL中加上responseBuffering=adaptive;selectMethod=cursor即可,无论多大的表根据这个配置Solr都可以从中读取数据并索引完成,当然,前提是不发生什么故障,如网络故障.这一想法也得到了Solr开发人员的验证,以下是他们给我回复的邮件:
That's not really true. DataImportHandler streams the result from database query and adding documents into index. So it shouldn't load all database data into memory. Disabling autoCommit, warming queries and spellcheckers usually decreases required amount of memory during
indexing process.Please share your hardware details, jvm options, solrconfig and schema configuration, etc.
但如此还是会造成一种隐患,就是如果表数据太大.如此会导致Solr一直在那获取数据并且索引.一旦发生什么故障如网络问题,Solr之前所做的索引就会前功尽弃,因为我们根本不知道它索引了多少条,索引到了哪里(后来看代码其实这种担忧是多余的,一旦发生网络异常,Solr会抛出异常,然后进行回滚,将索引恢复到索引之前的状态,也就是说之前做的一切根本
是白做,而Lucene写索引的速度又是相当慢,在如此长的时间内发生不可预见的问题绝对有可能).同时Solr要写完一次索引才会通知IndexSearcher让IndexReader去reopen,所以在那么长一段时间内所索引的数据你是无法搜索到.于是自己想通过ID(Solr里设置的<uniqueKey>)分批来处理.但这个ID是数字要更方便,比如设置个起点,和一次索引多少条.如起点为1,一次索引10000条。如此Solr会从1-10000,10001-20000,如此一直不停的索引下去.本想自己继承个DataImportHandler去处理,后来Solr开发者给我回复的邮件告诉我,Solr通过配置也完全可以实现,邮件内容具体如下:
You can _batch_ import your data using full import command by providing additional request parameter (see
http://wiki.apache.org/solr/DataImportHandler#Accessing_request_parameters), i.e.
query="SELECT * FROM my_table ORDER BY id LIMIT 1000000 OFFSET ${dataimporter.request.offset}"
and then calling full-import command several times:
1) /dataimport?clean=true&offset=0
2) /dataimport?clean=false&offset=1000000
3) /dataimport?clean=false&offset=2000000
etc
于是我在db-data-config.xml中用如下配置:
<entity name="TestEntity" dataSource="dsSqlServer" pk="Id" query="SELECT Id,Title,Author,Content,Url,AddOn FROM Test WHERE Id>=${dataimporter.request.offset} And Id<=${dataimporter.request.offset}+10000" >
同时运用
1) /dataimport?clean=true&offset=0
2) /dataimport?clean=false&offset=1000000
3) /dataimport?clean=false&offset=2000000
这些URL去请求SOlr进行索引.Solr会读取offset这个参数区建立query查询语句,然后去获取这些查询语句的数据去索引.但如此最好还是自己写个类做下控制.可以一开始获取表的总结果条数然后通过以上循环迭代完成.其中里面的+10000也可以设置为一个参数,如${dataimporter.request.intervalRows}表示一次获取多少条,然后在URL里加&intervalRows这个参数,具体我还还没试过.同时由于索引还是个长期的过程,所以这里最好记录好每次索引索引成功的间隔.比如在30000-40000索引成功的时候将他们记录日志.如此在索引40000-50000的时候一旦发生问题,我们可以通过日志获取上一次成功索引的间隔数,再从这个间隔去重新生成URL去索引数据.
以上希望起个抛砖引玉的作用,能对大家有所帮助,如果还有什么问题请留言。同时我这里处理的仅仅是SqlServer2005,其他数据库上操作成功的请分享下。同时如果你可以保证你间隔获取的数据如10000-20000这些数据不会导致内存溢出,那么你也可以不使用responseBuffering=adaptive;selectMethod=cursor这种配置.直接通过设置请求URL的offset来处理.
http://msdn.microsoft.com/zh-cn/library/aa342326
发表评论
-
sql server中的real数据类型
2011-07-07 16:23 3136今天做项目的时候遇到一个笔试题,问道:“sql server中 ... -
sql中deallocate是什么
2011-07-07 16:08 3402http://zhidao.baidu.com/questio ... -
collate Latin1_General_CI_AS
2011-07-07 11:22 1362collate Latin1_General_CI_AS ... -
SQL Native Client][SQL Server]无法将函数单元 'sp_sqlagent_get_startup_info' 添加到组件 'Agen
2011-07-02 01:18 1109http://zhidao.baidu.com/questio ... -
SET ANSI_NULLS ON 和 SET QUOTED_IDENTIFIER ON 什么意思
2011-07-01 08:58 2313http://blog.sina.com.cn/s/blog_ ... -
MSSQL"以前的某个程序安装已在安装计算机上创建挂起的文件操作
2010-03-15 14:11 3055MSSQL"以前的某个程序安装已在安装计算机上创建 ... -
select top 10 * from article where IsDel=0 order by id desc
2010-01-13 15:30 1936access top 的用法: select top ... -
Oracle, DB2 及 MySQL 分页查询写法
2009-09-27 11:18 2497下面列出了Oracle, DB2 ... -
java.sql.SQLException: ORA-00600:
2009-08-08 14:33 1524java.sql.SQLException: ORA-006 ... -
ORA-00911: invalid character
2009-08-08 13:37 5022练习jdbc的时候,创建表报错: ORA-00911: in ... -
ORA-00957: duplicate column name
2008-08-26 11:55 9084--- The error occurred in com/i ... -
oracle中建立job定期运行存储过程总结
2008-08-21 13:59 11422目前,流行的主流数据 ... -
oracle无效列名 无效数字 的错误-
2008-08-20 19:53 4327oracle中,where 语句如:tea_code='123 ... -
ORACLE 中ROWNUM用法总结! (转)
2008-04-15 11:33 1167ORACLE 中ROWNUM用法总结! ... -
Oracle 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数,其他函数)
2008-04-10 15:11 1731cast 是进行类型转换的, 可以针对各种Oracle数据类型 ... -
Oracle 游标使用大全
2008-03-24 11:37 1937我们将讨论各种用于访问ORACLE数据库的DDL和TCL语句。 ... -
PowerDesigner 11 使用心得
2008-03-17 11:05 23401、安装PD v11.0版 2、由p ... -
SQL SERVER中用CREATE INDEX 命令创建索引
2008-03-17 10:45 11349CREATE INDEX 既可以创建一 ... -
利用Power Designer 11创建数据库表结构时的错误与警告
2008-03-17 10:31 2451Table Existence of index Table ... -
Oracle SQL 语句中对Like的优化
2008-02-22 15:45 5397[color=red]1、尽量不要使用 like '%..%' ...
相关推荐
A multistage adaptive thresholding method,通俗易懂的算法
adaptive finite element method adaptive finite element method adaptive finite element method
Effective Interlaced FFT Computation Scheme For Adaptive Integral Method
资源受限环境下的适应性窗口多点乘计算方法,孙达志,,在几个主要公钥密码系统的实现中,基本而开销巨大的操作是计算定义在椭圆曲线上的某个有限加法群的多点乘问题。本文提出了一种适
An Adaptive Steganographic Method Using Additive Noise
Adaptive denoising method to improve aberration measurement performance
无线网络控制系统中用于能耗控制的自适应采样方法,王艳,纪志成,在无线网络环境中,节点的能量限制是影响控制性能和系统运行的主要问题。基于能量消耗的分析给出采样周期和寿命期限的关系。然后
A Novel Adaptive Method for the Analysis of Next-,GWAS 与全基因组相关。很值得一看
Nonlinear and Adaptive Control Design 电子书 Nonlinear and Adaptive Control Design 电子书
Sybase Adaptive Server快速入门
Adaptive Method for Nonsmooth Nonnegative Matrix Factorization
用python实现Adaptive Lasso
Adaptive Server 参考手册 包含有关 Sybase® Adaptive Server ® Enterprise 和 Transact-SQL® 语言的四本指南: • 构件块 介绍了 Transact-SQL 的各个组成部分:数据类型、内置 函数、全局变量、表达式、标识符...
Engineering Adaptive Software Systems: Communications of NII Shonan Meetings ISBN-10 书号: 9811321841 ISBN-13 书号: 9789811321849 Edition 版本: 1st ed. 2019 Release Finelybook 出版日期: 2019-01-15 ...
Subband Adaptive Filtering Theory and Implementation
Adaptive Filter Theory 5th 第五版 非扫描 高清pdf
Adaptive Optics Engineering Handbook.
Sybase Adaptive Server Anywhere 6.0 新测有效...win7用兼容模式运行