Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
aroad_aqsc
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
许言琪
aroad_aqsc
Commits
8e8581fc
提交
8e8581fc
authored
4月 15, 2019
作者:
董吉利
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加模块
1、解析excel文档存入elasticsearch 2、elasticsearch分词检索 3、elasticsearch路段、桥梁匹配查询
上级
75006bbe
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
830 行增加
和
0 行删除
+830
-0
pom.xml
pom.xml
+118
-0
LuKeBridge.java
...ramework/galaxy/aroad/trafficbasic/domain/LuKeBridge.java
+59
-0
LuKeHighWay.java
...amework/galaxy/aroad/trafficbasic/domain/LuKeHighWay.java
+81
-0
ReadExcelBridge.java
...k/galaxy/aroad/trafficbasic/infodata/ReadExcelBridge.java
+55
-0
EsUtil_fanshe.java
...mework/galaxy/aroad/trafficbasic/utils/EsUtil_fanshe.java
+285
-0
ExcelUtil_fanshe.java
...ork/galaxy/aroad/trafficbasic/utils/ExcelUtil_fanshe.java
+232
-0
没有找到文件。
pom.xml
浏览文件 @
8e8581fc
...
@@ -106,6 +106,124 @@
...
@@ -106,6 +106,124 @@
<artifactId>
fastjson
</artifactId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.28
</version>
<version>
1.2.28
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.elasticsearch.client
</groupId>
<artifactId>
transport
</artifactId>
<version>
6.0.0
</version>
</dependency>
<dependency>
<groupId>
org.apache.hbase
</groupId>
<artifactId>
hbase-client
</artifactId>
<version>
1.3.1
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml-schemas
</artifactId>
<version>
3.9
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml
</artifactId>
<version>
3.8
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi
</artifactId>
<version>
3.8
</version>
</dependency>
<!--log4j需要的2个包-->
<dependency>
<groupId>
log4j
</groupId>
<artifactId>
log4j
</artifactId>
<version>
1.2.17
</version>
</dependency>
<dependency>
<groupId>
commons-logging
</groupId>
<artifactId>
commons-logging
</artifactId>
<version>
1.2
</version>
</dependency>
<!-- javacommonslogging到slf4j的动态转换-->
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
jcl-over-slf4j
</artifactId>
<version>
1.7.25
</version>
</dependency>
<!--slf4j和log4j的整合输出为log4j-->
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-log4j12
</artifactId>
<version>
1.7.25
</version>
</dependency>
<!--不管是javautillogging还是log4j都会接收过来-->
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
<version>
1.7.25
</version>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-test
</artifactId>
<version>
4.2.4.RELEASE
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.11
</version>
<scope>
test
</scope>
</dependency>
<!--json三种包-->
<dependency>
<groupId>
com.google.code.gson
</groupId>
<artifactId>
gson
</artifactId>
<version>
2.8.2
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.47
</version>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-databind
</artifactId>
<version>
2.9.1
</version>
</dependency>
<!-- jsp/servlet/jstl的jar包 -->
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
servlet-api
</artifactId>
<version>
3.0-alpha-1
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
jstl
</artifactId>
<version>
1.2
</version>
</dependency>
<dependency>
<groupId>
javax.servlet.jsp
</groupId>
<artifactId>
jsp-api
</artifactId>
<version>
2.1
</version>
<scope>
provided
</scope>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>
5.1.38
</version>
</dependency>
<!-- 数据库连接池:druid -->
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
druid
</artifactId>
<version>
1.0.18
</version>
</dependency>
<dependency>
<groupId>
commons-lang
</groupId>
<artifactId>
commons-lang
</artifactId>
<version>
2.6
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/com/elephant/framework/galaxy/aroad/trafficbasic/domain/LuKeBridge.java
0 → 100644
浏览文件 @
8e8581fc
package
com
.
elephant
.
framework
.
galaxy
.
aroad
.
trafficbasic
.
domain
;
import
java.io.Serializable
;
/**
* @Description: 桥梁javabean
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
class
LuKeBridge
implements
Serializable
{
private
String
id
;
private
String
qiaoliangmingcheng
;
private
String
qiaoliangdaima
;
public
LuKeBridge
()
{
}
public
LuKeBridge
(
String
id
,
String
qiaoliangmingcheng
,
String
qiaoliangdaima
)
{
this
.
id
=
id
;
this
.
qiaoliangmingcheng
=
qiaoliangmingcheng
;
this
.
qiaoliangdaima
=
qiaoliangdaima
;
}
public
String
getId
()
{
return
id
;
}
public
String
getQiaoliangmingcheng
()
{
return
qiaoliangmingcheng
;
}
public
String
getQiaoliangdaima
()
{
return
qiaoliangdaima
;
}
public
void
setId
(
String
id
)
{
this
.
id
=
id
;
}
public
void
setQiaoliangmingcheng
(
String
qiaoliangmingcheng
)
{
this
.
qiaoliangmingcheng
=
qiaoliangmingcheng
;
}
public
void
setQiaoliangdaima
(
String
qiaoliangdaima
)
{
this
.
qiaoliangdaima
=
qiaoliangdaima
;
}
@Override
public
String
toString
()
{
return
"LuKeBridge{"
+
"id='"
+
id
+
'\''
+
", qiaoliangmingcheng='"
+
qiaoliangmingcheng
+
'\''
+
", qiaoliangdaima='"
+
qiaoliangdaima
+
'\''
+
'}'
;
}
}
src/main/java/com/elephant/framework/galaxy/aroad/trafficbasic/domain/LuKeHighWay.java
0 → 100644
浏览文件 @
8e8581fc
package
com
.
elephant
.
framework
.
galaxy
.
aroad
.
trafficbasic
.
domain
;
import
java.io.Serializable
;
/**
* @Description: 路段javabean
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
class
LuKeHighWay
implements
Serializable
{
private
String
id
;
private
String
luxianbianhao
;
private
String
luxianmingcheng
;
private
String
qidianmingcheng
;
private
String
zhidianmingcheng
;
public
LuKeHighWay
()
{
}
public
LuKeHighWay
(
String
id
,
String
luxianbianhao
,
String
luxianmingcheng
,
String
qidianmingcheng
,
String
zhidianmingcheng
)
{
this
.
id
=
id
;
this
.
luxianbianhao
=
luxianbianhao
;
this
.
luxianmingcheng
=
luxianmingcheng
;
this
.
qidianmingcheng
=
qidianmingcheng
;
this
.
zhidianmingcheng
=
zhidianmingcheng
;
}
public
String
getId
()
{
return
id
;
}
public
String
getLuxianbianhao
()
{
return
luxianbianhao
;
}
public
String
getLuxianmingcheng
()
{
return
luxianmingcheng
;
}
public
String
getQidianmingcheng
()
{
return
qidianmingcheng
;
}
public
String
getZhidianmingcheng
()
{
return
zhidianmingcheng
;
}
public
void
setId
(
String
id
)
{
this
.
id
=
id
;
}
public
void
setLuxianbianhao
(
String
luxianbianhao
)
{
this
.
luxianbianhao
=
luxianbianhao
;
}
public
void
setLuxianmingcheng
(
String
luxianmingcheng
)
{
this
.
luxianmingcheng
=
luxianmingcheng
;
}
public
void
setQidianmingcheng
(
String
qidianmingcheng
)
{
this
.
qidianmingcheng
=
qidianmingcheng
;
}
public
void
setZhidianmingcheng
(
String
zhidianmingcheng
)
{
this
.
zhidianmingcheng
=
zhidianmingcheng
;
}
@Override
public
String
toString
()
{
return
"LuKeHighWay{"
+
"id='"
+
id
+
'\''
+
", luxianbianhao='"
+
luxianbianhao
+
'\''
+
", luxianmingcheng='"
+
luxianmingcheng
+
'\''
+
", qidianmingcheng='"
+
qidianmingcheng
+
'\''
+
", zhidianmingcheng='"
+
zhidianmingcheng
+
'\''
+
'}'
;
}
}
src/main/java/com/elephant/framework/galaxy/aroad/trafficbasic/infodata/ReadExcelBridge.java
0 → 100644
浏览文件 @
8e8581fc
package
com
.
elephant
.
framework
.
galaxy
.
aroad
.
trafficbasic
.
infodata
;
import
com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeBridge
;
import
com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeHighWay
;
import
com.elephant.framework.galaxy.aroad.trafficbasic.utils.EsUtil_fanshe
;
import
java.util.ArrayList
;
/**
* @program: pingxiang_basedata
* @description: ${description}
* @author: dongjili
* @create: 2019-03-21 11:15
**/
public
class
ReadExcelBridge
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
createIndexAnd2Hbase
();
}
public
static
void
createIndexAnd2Hbase
()
throws
Exception
{
//解析excel封装成list集合返回
// List<LuKeBridge> luKeBridges = ExcelUtil_fanshe.readPathBridge("D:\\路科基础数据\\2018年桥梁明细表(带GUID).xlsx");
/*for (LuKeBridge luKeBridge : luKeBridges) {
System.out.println(luKeBridge.toString());
}*/
//解析excel封装成list集合返回
//List<LuKeHighWay> luKeHighWays = ExcelUtil_fanshe.readPathHighWay("D:\\路科基础数据\\2018年路段信息表(带GUID).xlsx");
/* for (LuKeHighWay luKeHighWay : luKeHighWays) {
System.out.println(luKeHighWay.toString());
}*/
//elasticsearch索引中插入数据
//EsUtil_fanshe.indexBridge(luKeBridges);
//EsUtil_fanshe.indexHighWay(luKeHighWays);
//分词查询桥梁数据返回list集合
ArrayList
<
LuKeBridge
>
luKeBridges
=
EsUtil_fanshe
.
queryBridge
(
"曹县"
,
"basicbridgedata"
,
"2018"
,
0
,
2000000000
);
/* for (LuKeBridge luKeBridge : luKeBridges) {
System.out.println(luKeBridge.toString());
}*/
//分词查询路段数据返回list集合
ArrayList
<
LuKeHighWay
>
lukehighways
=
EsUtil_fanshe
.
queryHighWay
(
"曹县"
,
"basichighwaydata"
,
"2018"
,
0
,
2000000000
);
/* for (LuKeHighWay lukehighway : lukehighways) {
System.out.println(lukehighway.toString());
}*/
//查询索引库中所有的桥梁id
ArrayList
<
String
>
basicbridgeIds
=
EsUtil_fanshe
.
queryIds
(
"basicbridgedata"
,
"2018"
,
0
,
2000000000
);
/* for (String basicbridgeId : basicbridgeIds) {
System.out.println(basicbridgeId);
}*/
//查询索引库中所有的路段id
ArrayList
<
String
>
basichighwayIds
=
EsUtil_fanshe
.
queryIds
(
"basichighwaydata"
,
"2018"
,
0
,
2000000000
);
//分词检索出来的数据是否是路段数据或者桥梁数据
String
s
=
EsUtil_fanshe
.
ifInclude
(
basicbridgeIds
,
basichighwayIds
,
luKeBridges
,
lukehighways
);
System
.
out
.
println
(
s
);
}
}
src/main/java/com/elephant/framework/galaxy/aroad/trafficbasic/utils/EsUtil_fanshe.java
0 → 100644
浏览文件 @
8e8581fc
package
com
.
elephant
.
framework
.
galaxy
.
aroad
.
trafficbasic
.
utils
;
import
com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeBridge
;
import
com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeHighWay
;
import
com.google.gson.Gson
;
import
org.elasticsearch.action.bulk.BulkRequestBuilder
;
import
org.elasticsearch.action.bulk.BulkResponse
;
import
org.elasticsearch.action.index.IndexRequestBuilder
;
import
org.elasticsearch.action.search.SearchRequestBuilder
;
import
org.elasticsearch.action.search.SearchResponse
;
import
org.elasticsearch.client.transport.TransportClient
;
import
org.elasticsearch.common.settings.Settings
;
import
org.elasticsearch.common.transport.TransportAddress
;
import
org.elasticsearch.common.xcontent.XContentType
;
import
org.elasticsearch.index.query.QueryBuilders
;
import
org.elasticsearch.search.SearchHit
;
import
org.elasticsearch.search.SearchHits
;
import
org.elasticsearch.transport.client.PreBuiltTransportClient
;
import
java.lang.reflect.Constructor
;
import
java.lang.reflect.Field
;
import
java.net.InetAddress
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
/**
* @Description: 自定义elasticsearch工具类,
* 1、实现elasticsearch集群的连接
* 2、路段、桥梁索引数据插入
* 3、路段、桥梁分词检索
* 4、分词检索出来的数据是否是路段数据或者桥梁数据
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
class
EsUtil_fanshe
{
public
static
TransportClient
transportClient
;
public
static
Gson
gson
=
new
Gson
();
/**
* @Description: 创建连接
* @Param:
* @return: transportClient
* @Author: dongjili
* @Date:
*/
public
static
TransportClient
getClient
()
throws
Exception
{
Settings
settings
=
Settings
.
builder
().
put
(
"cluster.name"
,
"myes"
).
build
();
if
(
transportClient
==
null
)
{
synchronized
(
EsUtil_fanshe
.
class
)
{
// 创建Client连接对象
transportClient
=
new
PreBuiltTransportClient
(
settings
)
.
addTransportAddress
(
new
TransportAddress
(
InetAddress
.
getByName
(
"192.168.6.100"
),
9300
))
.
addTransportAddress
(
new
TransportAddress
(
InetAddress
.
getByName
(
"192.168.6.110"
),
9300
))
.
addTransportAddress
(
new
TransportAddress
(
InetAddress
.
getByName
(
"192.168.6.120"
),
9300
));
}
}
return
transportClient
;
}
/**
* @Description: 桥梁索引表插入数据
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
static
void
indexBridge
(
List
<
LuKeBridge
>
luKeBridges
)
throws
Exception
{
// 建立文档
//prepareIndex一次创建一个索引,bulkIndex就可以将多个文档合并在一起之后,发送一次请求
BulkRequestBuilder
bulk
=
getClient
().
prepareBulk
();
for
(
LuKeBridge
luKeBridge
:
luKeBridges
)
{
String
json
=
gson
.
toJson
(
luKeBridge
);
IndexRequestBuilder
requestBuilder
=
transportClient
.
prepareIndex
(
"basicbridgedata"
,
"2018"
,
luKeBridge
.
getId
()).
setSource
(
json
,
XContentType
.
JSON
);
bulk
.
add
(
requestBuilder
);
}
BulkResponse
bulkItemResponses
=
bulk
.
get
();
System
.
out
.
println
(
bulkItemResponses
.
status
());
}
/**
* @Description: 路段索引表插入数据
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
static
void
indexHighWay
(
List
<
LuKeHighWay
>
luKeHighWays
)
throws
Exception
{
// 建立文档
BulkRequestBuilder
bulk
=
getClient
().
prepareBulk
();
for
(
LuKeHighWay
luKeHighWay
:
luKeHighWays
)
{
String
json
=
gson
.
toJson
(
luKeHighWay
);
IndexRequestBuilder
requestBuilder
=
transportClient
.
prepareIndex
(
"basichighwaydata"
,
"2018"
,
luKeHighWay
.
getId
()).
setSource
(
json
,
XContentType
.
JSON
);
bulk
.
add
(
requestBuilder
);
}
BulkResponse
bulkItemResponses
=
bulk
.
get
();
//System.out.println(bulkItemResponses.status());
}
/**
* @Description: 释放资源
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
static
void
close
()
{
//释放资源
if
(
transportClient
!=
null
)
{
transportClient
.
close
();
}
}
/**
* @Description: 桥梁全字段查询
* @Param:
* @return: ArrayList<LuKeBridge> luKeBridges
* @Author: dongjili
* @Date:
*/
public
static
ArrayList
<
LuKeBridge
>
queryBridge
(
String
keyword
,
String
index
,
String
type
,
int
start
,
int
row
)
throws
Exception
{
// 搜索数据
SearchRequestBuilder
searchRequestBuilder
=
getClient
().
prepareSearch
(
index
).
setTypes
(
type
).
setFrom
(
start
).
setSize
(
row
);
//isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度)
if
(
org
.
apache
.
commons
.
lang
.
StringUtils
.
isNotBlank
(
keyword
))
{
//multiMatchQuery多个字段匹配某一个值
//multiMatchQuery(text,fields)其中的fields是字段的名字,可以写好几个,每一个中间用逗号分隔
searchRequestBuilder
.
setQuery
(
QueryBuilders
.
multiMatchQuery
(
keyword
,
"id"
,
"qiaoliangmingcheng"
,
"qiaoliangdaima"
));
}
String
[]
bridgeNames
=
{
"id"
,
"qiaoliangmingcheng"
,
"qiaoliangdaima"
};
SearchResponse
response
=
searchRequestBuilder
.
get
();
//获取命中次数,查询结果有多少对象
SearchHits
hits
=
response
.
getHits
();
ArrayList
<
LuKeBridge
>
luKeBridges
=
new
ArrayList
<>();
for
(
SearchHit
hit
:
hits
)
{
//System.out.println(hit.getSourceAsString());
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
//LuKeBridge luKeBridge = new LuKeBridge();
//获取该类在堆区的Class对象
Class
<?>
clazz
=
Class
.
forName
(
"com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeBridge"
);
//获取私有的构造方法
Constructor
<?>
constructor
=
clazz
.
getDeclaredConstructor
();
//暴力破解
constructor
.
setAccessible
(
true
);
//实例化对象
LuKeBridge
luKeBridge
=
(
LuKeBridge
)
constructor
.
newInstance
();
//获得私有的属性对象
Field
[]
beanFiled
=
clazz
.
getDeclaredFields
();
luKeBridge
.
setId
(
hit
.
getId
());
for
(
int
a
=
1
;
a
<
bridgeNames
.
length
;
a
++)
{
//暴力破解
beanFiled
[
a
].
setAccessible
(
true
);
//把查询到的每一个字段的值赋给luKeBridge的每一个属性
beanFiled
[
a
].
set
(
luKeBridge
,
sourceAsMap
.
get
(
bridgeNames
[
a
])
==
null
?
""
:
sourceAsMap
.
get
(
bridgeNames
[
a
]).
toString
());
}
luKeBridges
.
add
(
luKeBridge
);
}
return
luKeBridges
;
}
/**
* @Description: 路段全字段查询
* @Param:
* @return: ArrayList<LuKeHighWay> lukehighways
* @Author: dongjili
* @Date:
*/
public
static
ArrayList
<
LuKeHighWay
>
queryHighWay
(
String
keyword
,
String
index
,
String
type
,
int
start
,
int
row
)
throws
Exception
{
//搜索数据
SearchRequestBuilder
searchRequestBuilder
=
getClient
().
prepareSearch
(
index
).
setTypes
(
type
).
setFrom
(
start
).
setSize
(
row
);
//isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度)
if
(
org
.
apache
.
commons
.
lang
.
StringUtils
.
isNotBlank
(
keyword
))
{
//multiMatchQuery多个字段匹配某一个值
//multiMatchQuery(text,fields)其中的fields是字段的名字,可以写好几个,每一个中间用逗号分隔
searchRequestBuilder
.
setQuery
(
QueryBuilders
.
multiMatchQuery
(
keyword
,
"id"
,
"luxianbianhao"
,
"luxianmingcheng"
,
"qidianmingcheng"
,
"zhidianmingcheng"
));
}
String
[]
highwayNames
=
{
"id"
,
"luxianbianhao"
,
"luxianmingcheng"
,
"qidianmingcheng"
,
"zhidianmingcheng"
};
SearchResponse
response
=
searchRequestBuilder
.
get
();
//获取命中次数,查询结果有多少对象
SearchHits
hits
=
response
.
getHits
();
ArrayList
<
LuKeHighWay
>
lukehighways
=
new
ArrayList
<>();
for
(
SearchHit
hit
:
hits
)
{
Map
<
String
,
Object
>
sourceAsMap
=
hit
.
getSourceAsMap
();
//LuKeHighWay luKeHighWay = new LuKeHighWay();
//获取该类在堆区的Class对象
Class
<?>
clazz
=
Class
.
forName
(
"com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeHighWay"
);
//获取私有的构造方法
Constructor
<?>
constructor
=
clazz
.
getDeclaredConstructor
();
//暴力破解
constructor
.
setAccessible
(
true
);
//实例化对象
LuKeHighWay
luKeHighWay
=
(
LuKeHighWay
)
constructor
.
newInstance
();
//获取私有的属性字段
Field
[]
beanFiled
=
clazz
.
getDeclaredFields
();
luKeHighWay
.
setId
(
hit
.
getId
());
for
(
int
a
=
1
;
a
<
highwayNames
.
length
;
a
++)
{
//暴力破解
beanFiled
[
a
].
setAccessible
(
true
);
//给路段属性复制
beanFiled
[
a
].
set
(
luKeHighWay
,
sourceAsMap
.
get
(
highwayNames
[
a
])
==
null
?
""
:
sourceAsMap
.
get
(
highwayNames
[
a
]).
toString
());
}
lukehighways
.
add
(
luKeHighWay
);
}
return
lukehighways
;
}
/**
* @Description: 获取路段所有的id
* @Param:
* @return: 返回查询的路段所有id或桥梁所有id
* @Author: dongjili
* @Date:
*/
public
static
ArrayList
<
String
>
queryIds
(
String
index
,
String
type
,
int
start
,
int
row
)
throws
Exception
{
//定义集合封装查询的ids
ArrayList
<
String
>
trafficIds
=
new
ArrayList
<>();
//搜索数据
SearchRequestBuilder
searchRequestBuilder
=
getClient
().
prepareSearch
(
index
).
setTypes
(
type
).
setFrom
(
start
).
setSize
(
row
);
//matchAllQuery查询所有数据
searchRequestBuilder
.
setQuery
(
QueryBuilders
.
matchAllQuery
());
SearchResponse
response
=
searchRequestBuilder
.
get
();
SearchHits
hits
=
response
.
getHits
();
for
(
SearchHit
hit
:
hits
)
{
String
id
=
hit
.
getId
();
trafficIds
.
add
(
id
);
}
return
trafficIds
;
}
/**
* @Description: 分词检索出来的数据是否是路段数据或者桥梁数据
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
static
String
ifInclude
(
ArrayList
<
String
>
basicbridgeIds
,
ArrayList
<
String
>
basichighwayIds
,
ArrayList
<
LuKeBridge
>
luKeBridges
,
ArrayList
<
LuKeHighWay
>
lukehighways
)
{
ArrayList
<
String
>
trafficIds
=
new
ArrayList
<>();
//查询所有的桥梁id封装在trafficIds集合中
for
(
LuKeBridge
luKeBridge
:
luKeBridges
)
{
String
luKeBridgeId
=
luKeBridge
.
getId
();
trafficIds
.
add
(
luKeBridgeId
);
}
//查询所有的路段id封装在trafficIds集合中
for
(
LuKeHighWay
lukehighway
:
lukehighways
)
{
String
lukehighwayId
=
lukehighway
.
getId
();
trafficIds
.
add
(
lukehighwayId
);
}
HashSet
<
String
>
hashSet
=
new
HashSet
<>();
//判断桥梁、路段全字段查询出来的id是否在trafficIds集合中,返回各字段名称
for
(
String
trafficId
:
trafficIds
)
{
//System.out.println(trafficId);
if
(
basicbridgeIds
.
contains
(
trafficId
))
{
hashSet
.
add
(
"桥梁明细表"
);
continue
;
}
else
if
(
basichighwayIds
.
contains
(
trafficId
))
{
hashSet
.
add
(
"路段明细表"
);
continue
;
}
}
return
hashSet
.
toString
();
}
}
src/main/java/com/elephant/framework/galaxy/aroad/trafficbasic/utils/ExcelUtil_fanshe.java
0 → 100644
浏览文件 @
8e8581fc
package
com
.
elephant
.
framework
.
galaxy
.
aroad
.
trafficbasic
.
utils
;
import
com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeBridge
;
import
com.elephant.framework.galaxy.aroad.trafficbasic.domain.LuKeHighWay
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.DateUtil
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.xssf.usermodel.XSSFRow
;
import
org.apache.poi.xssf.usermodel.XSSFSheet
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.lang.reflect.Constructor
;
import
java.lang.reflect.Field
;
import
java.text.DecimalFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
/**
* @Description: 解析excel获取单元格的值封装成javabean
* @Param:
* @return:
* @Author: dongjili
* @Date:
*/
public
class
ExcelUtil_fanshe
{
public
static
FileInputStream
fw
;
public
static
XSSFWorkbook
wb
;
public
static
XSSFSheet
sheet
;
/**
* @Description: 根据桥梁明细表路径获取桥梁值的集合
* @Param:
* @return: List<LuKeBridge> luKeBridges
* @Author: dongjili
* @Date:
*/
public
static
List
<
LuKeBridge
>
readPathBridge
(
String
path
)
throws
Exception
{
//要想把excel中的每一行数据转换成javabean对象则用反射技术
//javabean中的属性个数要与excel中的列数一样,不然可能报错,属性顺序与列的顺序也要一样
fw
=
new
FileInputStream
(
new
File
(
path
));
// 读取的文件
//(1)判断文件后缀名是xls,还是xlsx
//(2)如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook
wb
=
new
XSSFWorkbook
(
fw
);
XSSFRow
row
=
null
;
List
<
LuKeBridge
>
luKeBridges
=
new
ArrayList
<
LuKeBridge
>();
//循环excel中所有的 sheet
for
(
int
i
=
0
;
i
<
wb
.
getNumberOfSheets
();
i
++)
{
sheet
=
wb
.
getSheetAt
(
i
);
//循环每一个sheet中的每一行
for
(
int
j
=
1
;
j
<=
sheet
.
getLastRowNum
();
j
++)
{
row
=
sheet
.
getRow
(
j
);
//LuKeHighWay luKeHighWay = new LuKeHighWay();
//获取该类在堆区的Class对象
Class
<?>
clazz
=
Class
.
forName
(
"com.pinxiang.domain.LuKeBridge"
);
//获取私有的构造方法
Constructor
<?>
constructor
=
clazz
.
getDeclaredConstructor
();
//暴力破解
constructor
.
setAccessible
(
true
);
//实例化对象
LuKeBridge
luKeBridge
=
(
LuKeBridge
)
constructor
.
newInstance
();
//获取javabean的各个属性
Field
[]
beanFiled
=
clazz
.
getDeclaredFields
();
//循环每一行中的所有列,就是得到单元格中的数据
for
(
int
z
=
0
;
z
<
row
.
getLastCellNum
();
z
++)
{
try
{
//强制反射,让private 的属性也可以访问
beanFiled
[
z
].
setAccessible
(
true
);
//把得到的属性进行赋值,就是把读取到的单元格中的数据赋给对应的属性
//CREATE_NULL_AS_BLANK 如何为空就创建一个空串
if
(
beanFiled
[
z
].
getType
().
equals
(
String
.
class
))
{
beanFiled
[
z
].
set
(
luKeBridge
,
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
)));
}
else
if
(
beanFiled
[
z
].
getType
().
equals
(
Integer
.
class
))
{
beanFiled
[
z
].
set
(
luKeBridge
,
Integer
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled
[
z
].
getType
().
equals
(
Long
.
class
.
getName
()))
{
beanFiled
[
z
].
set
(
luKeBridge
,
Long
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled
[
z
].
getType
().
equals
(
Float
.
class
))
{
beanFiled
[
z
].
set
(
luKeBridge
,
Float
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled
[
z
].
getType
().
equals
(
Double
.
class
))
{
beanFiled
[
z
].
set
(
luKeBridge
,
Double
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled
[
z
].
getType
().
equals
(
Byte
.
class
))
{
beanFiled
[
z
].
set
(
luKeBridge
,
Byte
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled
[
z
].
getType
().
equals
(
Boolean
.
class
))
{
beanFiled
[
z
].
set
(
luKeBridge
,
Boolean
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled
[
z
].
getType
().
equals
(
Date
.
class
))
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
Date
date
=
null
;
try
{
date
=
sdf
.
parse
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
)));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
beanFiled
[
z
].
set
(
luKeBridge
,
date
);
}
else
{
beanFiled
[
z
].
set
(
luKeBridge
,
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
)));
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
luKeBridges
.
add
(
luKeBridge
);
}
}
return
luKeBridges
;
}
/**
* @Description: 根据路段明细表路径获取路段值的集合
* @Param:
* @return: List<LuKeHighWay> luKeHighWays
* @Author: dongjili
* @Date:
*/
public
static
List
<
LuKeHighWay
>
readPathHighWay
(
String
path
)
throws
Exception
{
//要想把excel中的每一行数据转换成javabean对象则用反射技术
//javabean中的属性个数要与excel中的列数一样,不然可能报错,属性顺序与列的顺序也要一样
fw
=
new
FileInputStream
(
new
File
(
path
));
// 读取的文件
//(1)判断文件后缀名是xls,还是xlsx
//(2)如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook
wb
=
new
XSSFWorkbook
(
fw
);
XSSFRow
row
=
null
;
List
<
LuKeHighWay
>
luKeHighWays
=
new
ArrayList
<
LuKeHighWay
>();
//循环excel中所有的 sheet
for
(
int
i
=
0
;
i
<
wb
.
getNumberOfSheets
();
i
++)
{
sheet
=
wb
.
getSheetAt
(
i
);
//循环每一个sheet中的每一行
for
(
int
j
=
1
;
j
<=
sheet
.
getLastRowNum
();
j
++)
{
row
=
sheet
.
getRow
(
j
);
//LuKeHighWay luKeHighWay = new LuKeHighWay();
Class
<?>
clazz
=
Class
.
forName
(
"com.pinxiang.domain.LuKeHighWay"
);
Constructor
<?>
constructor
=
clazz
.
getDeclaredConstructor
();
//暴力破解
constructor
.
setAccessible
(
true
);
LuKeHighWay
luKeHighWay
=
(
LuKeHighWay
)
constructor
.
newInstance
();
Field
[]
beanFiled2
=
clazz
.
getDeclaredFields
();
//循环每一行中的所有列,就是得到单元格中的数据
for
(
int
z
=
0
;
z
<
row
.
getLastCellNum
();
z
++)
{
try
{
//强制反射,让private 的属性也可以访问
beanFiled2
[
z
].
setAccessible
(
true
);
//把得到的属性进行赋值,就是把读取到的单元格中的数据赋给对应的属性
/* Row.MissingCellPolicy RETURN_NULL_AND_BLANK
Row.MissingCellPolicy RETURN_BLANK_AS_NULL
Row.MissingCellPolicy CREATE_NULL_AS_BLANK*/
if
(
beanFiled2
[
z
].
getType
().
equals
(
String
.
class
))
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
)));
}
else
if
(
beanFiled2
[
z
].
getType
().
equals
(
Integer
.
class
))
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
Integer
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled2
[
z
].
getType
().
equals
(
Long
.
class
.
getName
()))
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
Long
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled2
[
z
].
getType
().
equals
(
Float
.
class
))
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
Float
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled2
[
z
].
getType
().
equals
(
Double
.
class
))
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
Double
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled2
[
z
].
getType
().
equals
(
Byte
.
class
))
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
Byte
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled2
[
z
].
getType
().
equals
(
Boolean
.
class
))
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
Boolean
.
valueOf
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
))));
}
else
if
(
beanFiled2
[
z
].
getType
().
equals
(
Date
.
class
))
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
Date
date
=
null
;
try
{
date
=
sdf
.
parse
(
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
)));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
beanFiled2
[
z
].
set
(
luKeHighWay
,
date
);
}
else
{
beanFiled2
[
z
].
set
(
luKeHighWay
,
ExcelUtil_fanshe
.
getCellValue
(
row
.
getCell
(
z
,
Row
.
CREATE_NULL_AS_BLANK
)));
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
luKeHighWays
.
add
(
luKeHighWay
);
}
}
return
luKeHighWays
;
}
/**
* @Description: 传入单元格值转换成string字符串输出
* @Param:
* @return: string类型的单元格值
* @Date:
*/
private
static
String
getCellValue
(
Cell
cell
)
{
Object
result
=
""
;
if
(
cell
!=
null
)
{
switch
(
cell
.
getCellType
())
{
case
Cell
.
CELL_TYPE_STRING
:
//字符串
result
=
cell
.
getStringCellValue
();
break
;
case
Cell
.
CELL_TYPE_NUMERIC
:
//数字
if
(
DateUtil
.
isCellDateFormatted
(
cell
))
{
Date
theDate
=
cell
.
getDateCellValue
();
SimpleDateFormat
dff
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
result
=
dff
.
format
(
theDate
);
}
else
{
//DecimalFormat数字格式化,获取单元格中真正的数据类型
DecimalFormat
df
=
new
DecimalFormat
(
"#.###########"
);
result
=
df
.
format
(
cell
.
getNumericCellValue
());
}
break
;
case
Cell
.
CELL_TYPE_BOOLEAN
:
//boolean
result
=
cell
.
getBooleanCellValue
();
break
;
case
Cell
.
CELL_TYPE_FORMULA
:
//公式
result
=
cell
.
getCellFormula
();
break
;
case
Cell
.
CELL_TYPE_ERROR
:
//故障
result
=
cell
.
getErrorCellValue
();
break
;
case
Cell
.
CELL_TYPE_BLANK
:
//空值
break
;
default
:
break
;
}
}
return
result
.
toString
();
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论