Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
7d277e84
Commit
7d277e84
authored
Aug 06, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update lucene pipeline
parent
29f8cd2e
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
37 deletions
+56
-37
Spider.java
...agic-core/src/main/java/us/codecraft/webmagic/Spider.java
+4
-2
ConsolePipeline.java
.../java/us/codecraft/webmagic/pipeline/ConsolePipeline.java
+0
-3
FilePipeline.java
...ain/java/us/codecraft/webmagic/pipeline/FilePipeline.java
+0
-3
LucenePipeline.java
...n/java/us/codecraft/webmagic/pipeline/LucenePipeline.java
+52
-23
ModelPipeline.java
.../main/java/us/codecraft/webmagic/model/ModelPipeline.java
+0
-3
FreemarkerPipeline.java
...va/us/codecraft/webmagic/pipeline/FreemarkerPipeline.java
+0
-3
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java
View file @
7d277e84
...
...
@@ -228,9 +228,11 @@ public class Spider implements Runnable, Task {
}
pageProcessor
.
process
(
page
);
addRequest
(
page
);
if
(!
page
.
getResultItems
().
isSkip
()){
for
(
Pipeline
pipeline
:
pipelines
)
{
pipeline
.
process
(
page
.
getResultItems
(),
this
);
}
}
sleep
(
site
.
getSleepTime
());
}
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/pipeline/ConsolePipeline.java
View file @
7d277e84
...
...
@@ -15,9 +15,6 @@ public class ConsolePipeline implements Pipeline{
@Override
public
void
process
(
ResultItems
resultItems
,
Task
task
)
{
if
(
resultItems
.
isSkip
()){
return
;
}
System
.
out
.
println
(
"get page: "
+
resultItems
.
getRequest
().
getUrl
());
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
resultItems
.
getAll
().
entrySet
())
{
if
(
entry
.
getValue
()
instanceof
Iterable
)
{
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/pipeline/FilePipeline.java
View file @
7d277e84
...
...
@@ -47,9 +47,6 @@ public class FilePipeline implements Pipeline {
if
(!
file
.
exists
())
{
file
.
mkdirs
();
}
if
(
resultItems
.
isSkip
())
{
return
;
}
try
{
PrintWriter
printWriter
=
new
PrintWriter
(
new
FileWriter
(
path
+
DigestUtils
.
md5Hex
(
resultItems
.
getRequest
().
getUrl
())
+
".html"
));
printWriter
.
println
(
"url:\t"
+
resultItems
.
getRequest
().
getUrl
());
...
...
webmagic-plugin/webmagic-lucene/src/main/java/us/codecraft/webmagic/pipeline/LucenePipeline.java
View file @
7d277e84
...
...
@@ -3,20 +3,26 @@ package us.codecraft.webmagic.pipeline;
import
org.apache.lucene.analysis.Analyzer
;
import
org.apache.lucene.analysis.standard.StandardAnalyzer
;
import
org.apache.lucene.document.Document
;
import
org.apache.lucene.document.Field
;
import
org.apache.lucene.document.TextField
;
import
org.apache.lucene.index.DirectoryReader
;
import
org.apache.lucene.index.IndexWriter
;
import
org.apache.lucene.index.IndexWriterConfig
;
import
org.apache.lucene.queryparser.classic.ParseException
;
import
org.apache.lucene.queryparser.classic.QueryParser
;
import
org.apache.lucene.search.IndexSearcher
;
import
org.apache.lucene.search.Query
;
import
org.apache.lucene.search.ScoreDoc
;
import
org.apache.lucene.store.Directory
;
import
org.apache.lucene.store.
FS
Directory
;
import
org.apache.lucene.store.
RAM
Directory
;
import
org.apache.lucene.util.Version
;
import
us.codecraft.webmagic.ResultItems
;
import
us.codecraft.webmagic.Task
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author yihua.huang@dianping.com <br>
...
...
@@ -24,41 +30,64 @@ import java.io.File;
* Time: 下午2:11 <br>
*/
public
class
LucenePipeline
implements
Pipeline
{
@Override
public
void
process
(
ResultItems
resultItems
,
Task
task
)
{
try
{
}
catch
(
Exception
e
)
{
private
Directory
directory
;
}
}
private
IndexWriter
indexWriter
;
private
Analyzer
analyzer
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Analyzer
analyzer
=
new
StandardAnalyzer
(
Version
.
LUCENE_44
);
// Directory directory = new RAMDirectory();
// To store an index on disk, use this instead:
Directory
directory
=
FSDirectory
.
open
(
new
File
(
"/data/webmagic/www.guoxue123.cn/"
));
private
void
init
()
throws
IOException
{
analyzer
=
new
StandardAnalyzer
(
Version
.
LUCENE_44
);
directory
=
new
RAMDirectory
();
IndexWriterConfig
config
=
new
IndexWriterConfig
(
Version
.
LUCENE_44
,
analyzer
);
IndexWriter
iwriter
=
new
IndexWriter
(
directory
,
config
);
Document
doc
=
new
Document
();
// String text = "This is the text to be indexed.";
// doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
// iwriter.addDocument(doc);
iwriter
.
close
();
indexWriter
=
new
IndexWriter
(
directory
,
config
);
indexWriter
.
close
();
}
public
LucenePipeline
()
{
try
{
init
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
// Now search the index:
public
List
<
Document
>
search
(
String
fieldName
,
String
value
)
throws
IOException
,
ParseException
{
List
<
Document
>
documents
=
new
ArrayList
<
Document
>();
DirectoryReader
ireader
=
DirectoryReader
.
open
(
directory
);
IndexSearcher
isearcher
=
new
IndexSearcher
(
ireader
);
// Parse a simple query that searches for "text":
QueryParser
parser
=
new
QueryParser
(
Version
.
LUCENE_44
,
"fieldname"
,
analyzer
);
Query
query
=
parser
.
parse
(
"经典"
);
QueryParser
parser
=
new
QueryParser
(
Version
.
LUCENE_44
,
fieldName
,
analyzer
);
Query
query
=
parser
.
parse
(
value
);
ScoreDoc
[]
hits
=
isearcher
.
search
(
query
,
null
,
1000
).
scoreDocs
;
// Iterate through the results:
for
(
int
i
=
0
;
i
<
hits
.
length
;
i
++)
{
Document
hitDoc
=
isearcher
.
doc
(
hits
[
i
].
doc
);
System
.
out
.
println
(
hitDoc
);
documents
.
add
(
hitDoc
);
}
ireader
.
close
();
directory
.
close
();
return
documents
;
}
@Override
public
void
process
(
ResultItems
resultItems
,
Task
task
)
{
if
(
resultItems
.
isSkip
()){
return
;
}
Document
doc
=
new
Document
();
Map
<
String
,
Object
>
all
=
resultItems
.
getAll
();
if
(
all
==
null
){
return
;
}
for
(
Map
.
Entry
<
String
,
Object
>
objectEntry
:
all
.
entrySet
())
{
doc
.
add
(
new
Field
(
objectEntry
.
getKey
(),
objectEntry
.
getValue
().
toString
(),
TextField
.
TYPE_STORED
));
}
try
{
indexWriter
.
addDocument
(
doc
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
webmagic-plugin/webmagic-misc/src/main/java/us/codecraft/webmagic/model/ModelPipeline.java
View file @
7d277e84
...
...
@@ -29,9 +29,6 @@ class ModelPipeline implements Pipeline {
@Override
public
void
process
(
ResultItems
resultItems
,
Task
task
)
{
if
(
resultItems
.
isSkip
())
{
return
;
}
for
(
Map
.
Entry
<
Class
,
PageModelPipeline
>
classPageModelPipelineEntry
:
pageModelPipelines
.
entrySet
())
{
Object
o
=
resultItems
.
get
(
classPageModelPipelineEntry
.
getKey
().
getCanonicalName
());
if
(
o
!=
null
)
{
...
...
webmagic-samples/src/main/java/us/codecraft/webmagic/pipeline/FreemarkerPipeline.java
View file @
7d277e84
...
...
@@ -40,9 +40,6 @@ public class FreemarkerPipeline implements Pipeline {
@Override
public
void
process
(
ResultItems
resultItems
,
Task
task
)
{
if
(
resultItems
.
isSkip
())
{
return
;
}
String
path
=
this
.
path
+
""
+
task
.
getUUID
()
+
"/"
;
File
file
=
new
File
(
path
);
if
(!
file
.
exists
())
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment