方法/步骤
建立客户端连接
集群名称默认为elasticsearch,没有修改过无需setting可以建立连接:
Client client = new TransportClient()addTransportAddress(new InetSocketTransportAddress("172200196", 9300));
如果修改过集群的名称:
Settings settings = ImmutableSettingssettingsBuilder()
put("clustername", "elasticsearch_01")build();
Client client = new TransportClient(settings)
addTransportAddress(new InetSocketTransportAddress("172200196", 9300));
创建索引
public void createIndex(String index){
clientadmin()indices()create(new CreateIndexRequest(index))actionGet();
// waitForYellow
clientadmin()cluster()health(new ClusterHealthRequest(index)
waitForYellowStatus())
actionGet();
}
创建mapping,和curl中完全对应,同样指定分析器为ik
public void createMapping(String index,String type) throws IOException{
XContentBuilder builder = XContentFactoryjsonBuilder()
startObject()
startObject(type)
startObject("_all")
field("indexAnalyzer", "ik")
field("searchAnalyzer", "ik")
field("term_vector", "no")
field("store", "false")
endObject()
startObject("properties")
startObject("content")
field("type", "string")
field("store", "no")
field("term_vector", "with_positions_offsets")
field("indexAnalyzer", "ik")
field("searchAnalyzer", "ik")
field("include_in_all", "true")
field("boost", 9)
endObject()
endObject()
endObject()
endObject();
PutMappingRequest mapping = RequestsputMappingRequest(index)type(type)source(builder);
clientadmin()indices()putMapping(mapping)actionGet();
}
索引一些数据,创建成功isCreated()返回true
public void createData(String index,String type){
List<String> jsondata = ElasticsearchTestgetInitJsonData();
for(int i=0; i<jsondatasize(); i++){
IndexResponse indexResp = clientprepareIndex()
setIndex(index)setType(type)setId(i+1+"")
setSource(jsondataget(i))execute()actionGet();
boolean isCreated = indexRespisCreated();
Systemoutprintln("是否成功创建数据isCreated:"+isCreated);
}
}
查询数据方法
public void queryData(String index,String type){
QueryBuilder queryBuilder = QueryBuilderstermQuery("content", "中国");
SearchResponse searchResponse = clientprepareSearch(index)setTypes(type)
setQuery(queryBuilder)
execute()
actionGet();
SearchHits hits = searchResponsegetHits();
Systemoutprintln("查询到记录数:" + hitsgetTotalHits());
SearchHit[] searchHists = hitsgetHits();
for(SearchHit sh : searchHists){
Systemoutprintln("content:"+shgetSource()get("content"));
}
}
在main方法中调用
创建索引、mapping、数据
按条件查询,显示查询结果。
同时可以打开head界面查看下执行效果
网页信息在requestsget(xxxx)text里。好好看requests的文档。get返回的是一个response对象,里面有各种变量,你需要的是其中叫text的那一个。你直接print这个response对象的结果完全取决于开发者对__repr__或者__str__的重写情况。
刚好有个相似的,改动改动发给你!
searchasp:
<html>
<head>
<title></title>
</head>
<body bgcolor="#ffffff" topmargin="0" leftmargin="0">
<div align=center>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#000000">
<tr>
<td bgcolor="#ffffff">
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" height="1">
<tr>
<td colspan="2" align="middle" height="62">
<table width="580" border="0" cellpadding="5" cellspacing="0" class="text">
<tr>
<td class="text" width="145">
</td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2" align="middle" height="1">
<div align=center>
<table width="80%" border="0" cellpadding="3" cellspacing="1" bgcolor="#666666">
<tr>
<td bgcolor="#e3edff" align="middle" class="text">
<!-- 查询 -->
<form action="showasp" method="post" target="_blank" id="form1" name="form1">
<table border="1" cellpadding="0" cellspacing="0" width="90%" class="text" bordercolor="#8fbee8">
<tr>
<td width="12%"> 姓名</td>
<td width="88%"><input name="bookname" size="30" maxlength="100" ><input name="booktype" type="radio" value="1">精确<input name="booktype" type="radio" value="2" checked>模糊</td>
</tr>
<tr>
<td width="12%">身份z</td>
<td width="88%"><input name="publisher" size="30" maxlength="50"
><input name="pubtype" type="radio" value="1">精确<input name="pubtype" type="radio" value="2" checked>模糊</td>
</tr>
<tr>
<td width="12%"> 日期</td>
<td width="88%"><input name="author" size="30" maxlength="100" ><input name="auttype" type="radio" value="1">精确<input name="auttype" type="radio" value="2" checked>模糊 </td>
</tr>
<tr>
<td width="12%"> 工资</td>
<td width="88%"><input name="money" size="30" maxlength="100" ><input name="moneytype" type="radio" value="1">精确<input name="moneytype" type="radio" value="2" checked>模糊 </td>
</tr>
<tr>
<td width="100%" colspan="2">
<p align="center"><a href="showasp">查找</a></p>
</td>
</tr>
</table>
</form></td></tr></table></div><br></td></tr></table></td></tr></table></div>
</body>
</html>
————————————————————
showasp:
<% @language=VBScript %>
<!--#include file="adovbsinc" -->
<%
dim bname,btype,pname,ptype,aname,atype,mname,mtype,continue,currentpage,rowcount,i
continue=0
currentpage=Trim(Request("gopage"))
If currentpage="" then currentpage=1
bname=Trim(Request("bookname"))
btype=Trim(Request("booktype"))
pname=Trim(Request("publisher"))
ptype=Trim(Request("pubtype"))
aname=Trim(Request("author"))
atype=Trim(Request("auttype"))
mname=Trim(Request("money"))
mtype=Trim(Request("moneytype"))
IF bname="" and pname="" and aname="" and mname="" then ResponseRedirect "请填入信息!"
mySQL="select from aa where "
IF bname<>"" then
IF btype=1 then
mySQL=mySQL & "姓名='" & bname & "'"
continue=1
else
mySQL=mySQL & "姓名 like '%" & bname & "%'"
continue=1
End IF
End IF
IF pname<>"" then
IF continue=1 then mySQL=mySQL & "and "
IF ptype=1 then
mySQL=mySQL & "身份z号='" & pname & "'"
continue=1
else
mySQL=mySQL & "身份z号 like '%" & pname & "%'"
continue=1
End IF
End IF
IF aname<>"" then
IF continue=1 then mySQL=mySQL & "and "
IF atype=1 then
mySQL=mySQL & "参加工作日期='" & aname & "'"
else
mySQL=mySQL & "参加工作日期 like '%" & aname & "%'"
End IF
End IF
IF mname<>"" then
IF continue=1 then mySQL=mySQL & "and "
IF mtype=1 then
mySQL=mySQL & "基本工资='" & mname & "'"
else
mySQL=mySQL & "基本工资 like '%" & mname & "%'"
End IF
End IF
%>
<html>
<head>
<title></title>
<style>
</style>
</head>
<body>
<table border="0" width="778" cellspacing="0" style="color:white;font-size:13px">
<tr height="100" valign="middle"><td align="center"><img border="0" src="images/titlegif" WIDTH="370" HEIGHT="90"></td></tr>
<tr height="280" valign="top"><td width="100%">
<table border="1" width="100%" cellspacing="0" style="color:black;font-size:12px;border-style:solid">
<tr align="center">
<td width="10%"><big>姓名</big></td>
<td width="30%"><big>身份z号</big></td>
<td width="20%"><big>参加工作日期</big></td>
<td width="10%"><big>基本工资</big></td>
</tr>
<%
Set Con=ServerCreateObject("ADODBConnection")
conOpen "DSN=xhnewmdb;DBQ=D:\\xhnewmdb;DriverId=25;FIL=MSAccess;MaxBufferSize=2048;"
set RS=ServerCreateObject("ADODBRecordSet")
RSOpen mySQL,Con,adOpenStatic
RSPageSize=15
IF not RSEOF then RSAbsolutePage=cInt(currentpage)
rowcount=0
While not RSEOF and rowcount<RSPageSize
year1=Year(RS("参加工作日期"))
month1=Month(RS("参加工作日期"))
day1=Day(RS("参加工作日期"))
%>
<tr>
<td width="10%"><%=(RS("姓名"))%></td>
<td width="30%"><%=(RS("身份z号"))%></td>
<td width="20%" align="center"><%=(year1 & "年" & month1 & "月" & day1 & "日")%></td>
<td width="10%" align="center"><%=(RS("基本工资"))%></td>
</tr>
<%
rowcount=rowcount+1
RSMoveNext
Wend
For i=rowcount+1 to RSPageSize
%>
<tr>
<td width="10%"> </td>
<td width="30%"> </td>
<td width="20%"> </td>
<td width="10%"> </td>
</tr>
<%
Next
%>
</table>
</td></tr>
<tr><td height="25" align="right" valign="middle">
<font color="black">第</font><font color="teal">
<%
Responsewrite(currentpage)
%>
</font><font color="black">页/</font>
<font color="black">共</font><font color="teal">
<%
IF RSPageCount>0 then
Responsewrite(RSPageCount)
else
Responsewrite("1")
End IF
%>
</font><font color="black">页</font>
<%
dim prepage,nextpage
prepage=cInt(currentpage)-1
nextpage=cInt(currentpage)+1
IF cInt(currentpage)>1 then
Responsewrite("<a href=showaspbookname=" & bname & "&booktype=" & btype & "&publisher=" & pname & "&pubtype=" & ptype & "&author=" & aname & "&auttype=" & atype & "&money=" & mname & "&moneytype=" & mtype & "&gopage=" & prepage & ">上一页</a>" )
else
Responsewrite("<font color=#666666>上一页</font>")
End IF
%>
<%
IF cInt(currentpage)<RSPageCount then
Responsewrite("<a href=showaspbookname=" & bname & "&booktype=" & btype & "&publisher=" & pname & "&pubtype=" & ptype & "&author=" & aname & "&auttype=" & atype & "&money=" & mname & "&moneytype=" & mtype & "&gopage=" & nextpage & ">下一页</a>" )
else
Responsewrite("<font color=#666666>下一页</font>")
End IF
ConClose
set RS=NOTHING
%>
<p align="center"><a href="javascript:windowclose()">关闭窗口</a></p>
</td></tr>
</table>
</body>
</html>
没人喜欢做问答题,谁都喜欢做选择题!要自己勤动手!
一般是这样做的:
<!--#include file="connasp"-->
<%
set rs=serverCreateObject("adodbrecordset") '(建立recordset对象)
sqlstr="select from yhc" '---->(message为数据库中的一个数据表,即你要显示的数据所存放的数据表)
rsopen sqlstr,conn,1,1' ---->(表示打开数据库的方式)
rsmovefirst '---->(将指针移到第一条记录)
reaponsewrite("<table>")
while not rseof '---->(判断指针是否到末尾)
responsewrite("<tr><td>")
responsewrite(rs("id"))' ---->(显示数据表yhc中的id字段)
responsewrite("</td></tr>")
rsmovenext' ---->(将指针移动到下一条记录)
wend '---->(循环结束)
responsewrite("</table>")
rsclose
connclose'这几句是用来关闭数据库
set rs=nothing
set conn=nothing
%>
这样就可以用responsewrite()构造出一个表格了!!!
以上就是关于elasticsearch java//创建客户端对象 TransportClient client = new TransportClient(settings);全部的内容,包括:elasticsearch java//创建客户端对象 TransportClient client = new TransportClient(settings);、Python爬虫如何获取网页Network中某个文件的response、ASP数据库查询等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)