Commit c756c73b by 303514581@qq.com

2018/12/28 消息流增加messageGzip、es配置修改,pom。xml-es-client升级1.5

parent 184d6555
......@@ -87,7 +87,7 @@
<dependency>
<groupId>com.zhiwei</groupId>
<artifactId>es-client</artifactId>
<version>1.3</version>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
......@@ -135,6 +135,13 @@
</exclusions>
</dependency>
<!-- jwt -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>2.2.0</version>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
......
......@@ -28,6 +28,15 @@ public class ESConfig {
public static String weiboMarkIndexName;
public static String weiboMarkType;
/** Es IP **/
// public static final String ESIP = "1.119.44.201";
public static final String ESIP = "202.107.192.94";
/** Es PORT **/
public static final int ESPORT = 9200;
/** Es 9300PORT **/
public static final int ESTCPPORT = 9300;
public static String getIp() {
return ip;
}
......
......@@ -9,6 +9,8 @@ import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.stereotype.Component;
import com.zhiwei.messageflow.config.ESConfig;
@Component
public class ESClient {
public static TransportClient client = ESClientHolder.getESClient();
......@@ -25,7 +27,8 @@ public class ESClient {
// 此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
try {
client.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("1.119.44.201"), 9300));
// new InetSocketTransportAddress(InetAddress.getByName("1.119.44.201"), 9300));
new InetSocketTransportAddress(InetAddress.getByName(ESConfig.ESIP), ESConfig.ESTCPPORT));
// new InetSocketTransportAddress(InetAddress.getByName("192.168.1.75"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
......
......@@ -16,6 +16,7 @@ import org.springframework.stereotype.Component;
import com.zhiwei.es.util.ESIndexesUtil;
import com.zhiwei.messageflow.ESGetCommonId;
import com.zhiwei.messageflow.config.ESConfig;
import com.zhiwei.messageflow.es.ESClient;
import com.zhiwei.messageflow.es.dao.ESDao;
import com.zhiwei.messageflow.mongo.bean.DirectGroup;
......@@ -30,7 +31,8 @@ public class ESDaoImpl implements ESDao {
// protected static ESIndexesUtil.ESIndexes indexes =
// ESIndexesUtil.create("192.168.1.75", 9200);
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
// protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create(ESConfig.ESIP, ESConfig.ESPORT);
@Override
public SearchHits getDataFromEs(List<String> keywords, int count, long startid, long endid, PlatformNew platform,
......
......@@ -27,7 +27,8 @@ public class TrackESDaoImpl implements TrackESDao {
TransportClient transportClient = ESClient.client;
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
// protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create(ESConfig.ESIP, ESConfig.ESPORT);
@Override
public SearchHits getkeyWordsTrackWeiboFromEs(String anyWord, String allWords, String color, int count, long start,
......
......@@ -27,6 +27,7 @@ import com.zhiwei.messageflow.mongo.bean.HuserInfoWeibo;
import com.zhiwei.messageflow.mongo.bean.HuserInfoWeixin;
import com.zhiwei.messageflow.mongo.dao.HuserInfoDao;
import com.zhiwei.messageflow.util.TimeUtil;
import com.zhiwei.messageflow.util.Tools;
@Component
public class HighLightFillingServiceImpl implements HighLightFillingService {
......@@ -415,10 +416,13 @@ public class HighLightFillingServiceImpl implements HighLightFillingService {
// searchHit.getHighlightFields();
String message;
String messageGzip="";
JSONObject res = new JSONObject();
try {
message = mapper.writeValueAsString(sourceHitMap);
res = JSONObject.parseObject(message);
messageGzip = Tools.gzip(message);
res.put("messageGzip", messageGzip);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
......
......@@ -18,7 +18,8 @@ import com.zhiwei.es.util.ESIndexesUtil;
*/
public class ESQueryUtil {
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
// protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create(ESConfig.ESIP, ESConfig.ESPORT);
public static final String ES_QUERY_AND = "must";
......
package com.zhiwei.messageflow.util;
import com.zhiwei.es.util.ESIndexesUtil;
import com.zhiwei.messageflow.config.ESConfig;
public class ESTransport {
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
// protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create("1.119.44.201", 9200);
protected static ESIndexesUtil.ESIndexes indexes = ESIndexesUtil.create(ESConfig.ESIP, ESConfig.ESPORT);
}
......@@ -2,6 +2,8 @@ package com.zhiwei.messageflow.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
......@@ -25,6 +27,8 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
......@@ -38,6 +42,8 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.auth0.jwt.internal.org.apache.commons.codec.binary.Base64;
public class Tools {
private static final Logger log = LoggerFactory.getLogger(Tools.class);
......@@ -1074,4 +1080,117 @@ public class Tools {
return res;
}
/**
* gzip压缩字符串
*
* @param primStr
* @return
*/
public static String gzip(String primStr) {
if (primStr == null || primStr.length() == 0) {
return primStr;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = null;
try {
gzip = new GZIPOutputStream(out);
gzip.write(primStr.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (gzip != null) {
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new String(new Base64().encode(out.toByteArray()));
}
/**
* 使用gzip进行解压缩
*
* @param compressedStr
* @return
*/
public static String gunzip(String compressedStr) {
if (compressedStr == null) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = null;
GZIPInputStream ginzip = null;
byte[] compressed = null;
String decompressed = null;
try {
compressed = new Base64().decode(compressedStr);
in = new ByteArrayInputStream(compressed);
ginzip = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ginzip != null) {
try {
ginzip.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}
public static byte[] gzip(byte[] data) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data);
gzip.finish();
gzip.close();
byte[] ret = bos.toByteArray();
bos.close();
return ret;
}
public static byte[] ungzip(byte[] data) throws Exception {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, num);
}
gzip.close();
bis.close();
byte[] ret = bos.toByteArray();
bos.flush();
bos.close();
return ret;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment