Commit 8cb1d4e7 by zhangxiaoyan

get scraw data from s3

parent 6e758498
......@@ -3,5 +3,12 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>
\ No newline at end of file
......@@ -65,7 +65,7 @@
<!-- 单位是分钟-->
<mail.valid_time>3</mail.valid_time>
<report.url>http://localhost:8082</report.url>
<report.url>http://ec2-54-222-233-230.cn-north-1.compute.amazonaws.com.cn:9010</report.url>
<!--Redis setting-->
<redis.key.validtime>120</redis.key.validtime>
<redis.ip.requesttimes>5</redis.ip.requesttimes>
......
package common.service;
import java.util.List;
/**
* description:
*
* @author nolan
* @date 13/09/2017
*/
public interface IOpenApiService
{
void listInfo4AppData(String ds);
}
package common.service.impl;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import common.service.IOpenApiService;
import dmp.model.OtPkgDevStats;
import dmp.repository.OtPkgDevStatsRepository;
import org.anarres.lzo.LzoAlgorithm;
import org.anarres.lzo.LzoDecompressor;
import org.anarres.lzo.LzoInputStream;
import org.anarres.lzo.LzoLibrary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.AwsS3Util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* description:
*
* @author nolan
* @date 13/09/2017
*/
@Service
public class OpenApiServiceImpl
implements IOpenApiService
{
private static Logger logger = LoggerFactory.getLogger(OpenApiServiceImpl.class);
@Autowired OtPkgDevStatsRepository otPkgDevStatsRepository;
@Override
public void listInfo4AppData(String ds)
{
String skey = "export/pkg_dev_stats/ds=" + ds + "/";
final AwsS3Util s3Util = AwsS3Util.getInstance();
List itemList = AwsS3Util.getInstance().getS3Keys("reyunbpu", skey);
if (itemList == null || itemList.size() == 0) {
return;
}
for (int i = 0; i < itemList.size(); i++) {
String s3key = String.valueOf(itemList.get(i));
readS3File(s3Util, s3key);
}
}
private void readS3File(AwsS3Util s3Util, String s3key)
{
InputStream inputStream = null;
BufferedReader br = null;
try {
final byte[] fileBytes = s3Util.downloadBytesFromS3("reyunbpu", s3key);
inputStream = new ByteArrayInputStream(fileBytes);
if (s3key.endsWith(".lzo_deflate")) {
LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
LzoDecompressor deCompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);
LzoInputStream stream = new LzoInputStream(inputStream, deCompressor);
br = new BufferedReader(new InputStreamReader(stream));
}
else {
br = new BufferedReader(new InputStreamReader(inputStream));
}
String line = null;
List<OtPkgDevStats> resultList = new ArrayList<>();
while ((line = br.readLine()) != null) {
// System.out.println(line);
Object[] array = Splitter.on("\t").trimResults().splitToList(line).toArray();
if (array.length == 3) {
OtPkgDevStats opds = new OtPkgDevStats();
opds.setPkgname(array[0].toString());
opds.setDevNum(Long.valueOf(array[1].toString()));
opds.setDs(array[2].toString());
resultList.add(opds);
if (resultList.size() >= 2000) {
System.out.println(resultList);
otPkgDevStatsRepository.save(resultList);
resultList = new ArrayList<>();
}
}
}
}
catch (Exception e) {
logger.error("读取s3文件错误", e);
}
finally {
try {
if (br != null) {
br.close();
}
if (inputStream != null) {
inputStream.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
......@@ -147,6 +147,7 @@ public class ReportCallable
data_sb.append(" and ").append("ds<='").append(startDate).append("'");
}
String result = "";
String newResult = "";
if (isTotal) {
result = String.format("select count(1) as totalcount from ("+sql+") t", app_sb.length()>0?" where ":"",
app_sb.length()>0?app_sb.substring(5):"",
......
package util;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.google.common.collect.Lists;
import common.service.impl.OpenApiServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class AwsS3Util
{
private static final Logger logger = LoggerFactory.getLogger(AwsS3Util.class);
private static AwsS3Util awsS3Util = new AwsS3Util();
private AmazonS3 s3;
private AwsS3Util() {
String accessKey = "AKIAPSKINGJFIZHEFNWQ";
String secretKey = "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";
if ((accessKey != null) && (secretKey != null)) {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
ClientConfiguration config = new ClientConfiguration();
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort");
if (proxyHost != null && proxyPort != null) {
config.setProxyHost(proxyHost);
config.setProxyPort(Integer.valueOf(proxyPort));
}
if (s3 == null)
s3 = new AmazonS3Client(credentials, config);
s3.setRegion(com.amazonaws.regions.Region.getRegion(Regions.CN_NORTH_1));
}
}
public static AwsS3Util getInstance() {
return awsS3Util;
}
public AmazonS3 getAmazonS3() {
return s3;
}
public List<String> getS3Keys(String bucket, String prefix) {
List<String> rtnList = Lists.newArrayList();
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
.withBucketName(bucket)
.withPrefix(prefix));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
rtnList.add(objectSummary.getKey());
}
return rtnList;
}
public long uploadStreamToS3(String bucket, String s3key, InputStream in, String contentType, long contentLength)
throws IOException {
ObjectMetadata md = new ObjectMetadata();
if (contentType != null)
md.setContentType(contentType);
md.setContentLength(contentLength);
AwsS3Util.getInstance().getAmazonS3().putObject(bucket, s3key, in, md);
return contentLength;
}
public long uploadStringToS3(String s3bucket, String s3key, String str)
throws IOException {
final byte[] bytes = str.getBytes("UTF-8");
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
try {
return uploadStreamToS3(s3bucket, s3key, bis, "text/plain; charset=UTF-8", bytes.length);
} finally {
IOUtil.close(bis);
}
}
public String downloadStringFromS3(String bucket, String s3key) throws IOException {
return new String(downloadBytesFromS3(bucket, s3key), "UTF-8");
}
public byte[] downloadBytesFromS3(String bucket, String s3key) throws IOException {
InputStream is = null;
ByteArrayOutputStream bos = null;
try {
is = downloadStreamFromS3(bucket, s3key);
bos = new ByteArrayOutputStream();
IOUtil.copy(is, bos);
return bos.toByteArray();
} finally {
if (bos != null)
IOUtil.close(bos);
if (is != null)
IOUtil.close(is);
}
}
public InputStream downloadStreamFromS3(String bucket, String s3key) {
return getS3Object(bucket, s3key).getObjectContent();
}
public void deleteS3Object(String bucket, String s3key) {
AwsS3Util.getInstance().getAmazonS3().deleteObject(bucket, s3key);
}
private S3Object getS3Object(String bucket, String s3key) {
return AwsS3Util.getInstance().getAmazonS3().getObject(bucket, s3key);
}
public static void main(String[] args) {
OpenApiServiceImpl openApiService = new OpenApiServiceImpl();
openApiService.listInfo4AppData("2017-12-21");
}
}
\ No newline at end of file
package util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
/**
* Created by sunhao on 17/5/9.
* IO工具类
*/
public class IOUtil
{
public static void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (Exception ignored) {
}
}
}
public static void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (Exception ignored) {
}
}
}
public static void close(Writer os) {
if (os != null) {
try {
os.close();
} catch (Exception ignored) {
}
}
}
public static void close(Reader is) {
if (is != null) {
try {
is.close();
} catch (Exception ignored) {
}
}
}
public static void deleteAllFiles(File directory) {
File[] files = directory.listFiles();
for (File file : files) {
if (!file.delete()) {
// System.out.println("Cannot delete " + file);
}
}
}
public static String readString(InputStream input, String encoding)
throws IOException {
InputStreamReader ir = (encoding == null ? new InputStreamReader(input) : new InputStreamReader(input, encoding));
StringWriter sw = new StringWriter();
try {
copy(ir, sw);
return sw.toString();
} finally {
close(sw);
}
}
public static long copy(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024 * 4];
int len;
long count = 0;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
count += len;
}
return count;
}
public static long copy(Reader in, Writer out)
throws IOException {
char[] buffer = new char[1024 * 4];
int len;
long count = 0;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
count += len;
}
return count;
}
public static long copy(File src, File dest)
throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
try {
return copy(fis, fos);
} finally {
close(fis);
close(fos);
}
}
public static byte[] objToBytes(Serializable obj)
throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bos);
try {
oo.writeObject(obj);
return bos.toByteArray();
} finally {
close(oo);
close(bos);
}
}
public static Object objFromBytes(byte[] bytes)
throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bis);
try {
return oi.readObject();
} finally {
close(oi);
close(bis);
}
}
public static void objToStream(OutputStream os, Serializable obj)
throws IOException {
ObjectOutputStream oo = new ObjectOutputStream(os);
try {
oo.writeObject(obj);
} finally {
close(oo);
}
}
public static Object objFromStream(InputStream is)
throws IOException, ClassNotFoundException {
ObjectInputStream oi = new ObjectInputStream(is);
try {
return oi.readObject();
} finally {
close(oi);
}
}
public static void saveObject(File file, Serializable obj)
throws IOException {
objToStream(new BufferedOutputStream(new FileOutputStream(file)), obj);
}
public static Object loadObject(File file)
throws IOException, ClassNotFoundException {
return objFromStream(new BufferedInputStream(new FileInputStream(file)));
}
}
......@@ -28,11 +28,14 @@
<task:scheduled ref="trackAccountTask" method="task" cron="0 32 16 * * ?"/>
</task:scheduled-tasks>
<bean id="SyncAppDataTask" class="tkio.task.SyncAppDataTask"></bean>
<bean id="SyncAppDataTask" class="common.task.SyncAppDataTask"></bean>
<task:scheduled-tasks>
<!--//定时同步短链数据(每5分钟执行一次)-->
<task:scheduled ref="SyncAppDataTask" method="syncAppData" cron="0 38 20 * * ?"/>
<task:scheduled ref="SyncAppDataTask" method="syncAppInfo" cron="0 54 20 * * ?"/>
<task:scheduled ref="SyncAppDataTask" method="syncAppDeviceNum" cron="0 09 21 * * ?"/>
</task:scheduled-tasks>
</beans>
......@@ -94,7 +94,7 @@
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->
<prop key="hibernate.connection.CharSet">utf8</prop>
......
......@@ -10,11 +10,14 @@
<description>调度配置</description>
<bean id="tkioAccountTask" class="tkio.task.AccountTask"></bean>
<task:scheduled-tasks>
<!--//定时同步短链数据(每5分钟执行一次)-->
<task:scheduled ref="tkioAccountTask" method="task" cron="0 21 15 * * ?"/>
<task:scheduled ref="tkioAccountTask" method="task" cron="0 40 14 * * ?"/>
</task:scheduled-tasks>
<bean id="tkioNoticeTask" class="tkio.task.NoticeTask"></bean>
<task:scheduled-tasks>
<task:scheduled ref="tkioNoticeTask" method="task" cron="0 33 16 * * ?"/>
</task:scheduled-tasks>
<bean id="trackAccountTask" class="track.task.AccountTask"></bean>
......@@ -25,11 +28,14 @@
<task:scheduled ref="trackAccountTask" method="task" cron="0 32 16 * * ?"/>
</task:scheduled-tasks>
<bean id="SyncAppDataTask" class="tkio.task.SyncAppDataTask"></bean>
<bean id="SyncAppDataTask" class="common.task.SyncAppDataTask"></bean>
<task:scheduled-tasks>
<!--//定时同步短链数据(每5分钟执行一次)-->
<task:scheduled ref="SyncAppDataTask" method="syncAppData" cron="0 38 20 * * ?"/>
<task:scheduled ref="SyncAppDataTask" method="syncAppInfo" cron="0 54 20 * * ?"/>
<task:scheduled ref="SyncAppDataTask" method="syncAppDeviceNum" cron="0 09 21 * * ?"/>
</task:scheduled-tasks>
</beans>
......@@ -94,7 +94,7 @@
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->
<prop key="hibernate.connection.CharSet">utf8</prop>
......
report.url=http://localhost:8082
report.url=http://ec2-54-222-233-230.cn-north-1.compute.amazonaws.com.cn:9010
awsneed=${awsneed}
profile.table=${profile.table}
event.table=${event.table}
......
......@@ -29,19 +29,21 @@ common\controller\UserController.class
exception\TipException.class
common\context\AppUtils.class
util\RedisUtilNew.class
tkio\task\SyncAppDataTask.class
common\model\User.class
tkio\repository\IsNotifiedRepository.class
track\repository\TrackChannelRepository.class
tkio\service\impl\TkioAccountServiceImpl.class
util\DateUtil.class
common\service\impl\OpenApiServiceImpl.class
common\model\AppCategory.class
tkio\repository\ChannelRepository.class
security\RedisLoginStatusManager.class
util\IOUtil.class
common\model\Notice.class
dic\RoleTypeEnum.class
common\repository\AppInfoRepository.class
tkio\model\Channel.class
common\task\SyncAppDataTask.class
common\service\impl\MenuServiceImpl.class
common\repository\UserLogRepository.class
common\model\Account4Web.class
......@@ -60,6 +62,7 @@ common\repository\ReminderRepository.class
common\controller\NoticeController.class
util\StringUtil.class
track\model\Channel.class
util\AwsS3Util.class
track\model\Campaign.class
security\interceptor\AuthorizationInterceptor.class
security\annotation\CurrentAccount.class
......@@ -95,6 +98,7 @@ common\service\NoticeService.class
common\model\RoleType.class
tkio\task\AccountTask.class
security\annotation\Authorization.class
common\service\IOpenApiService.class
util\IP$1.class
tkio\task\NoticeTask.class
common\repository\CodeRepository.class
......
......@@ -28,7 +28,8 @@ E:\IDEA_workspace\manager\src\main\java\tkio\model\RoleAuthDetail.java
E:\IDEA_workspace\manager\src\main\java\tkio\model\SalesManLeader.java
E:\IDEA_workspace\manager\src\main\java\tkio\model\PackageType.java
E:\IDEA_workspace\manager\src\main\java\track\controller\TrackAccountController.java
E:\IDEA_workspace\manager\src\main\java\tkio\task\SyncAppDataTask.java
E:\IDEA_workspace\manager\src\main\java\common\controller\NoticeController.java
E:\IDEA_workspace\manager\src\main\java\common\service\impl\NoticeServiceImpl.java
E:\IDEA_workspace\manager\src\main\java\security\exception\MethodNotSupportException.java
E:\IDEA_workspace\manager\src\main\java\util\IPAddrUtil.java
E:\IDEA_workspace\manager\src\main\java\util\HttpClientUtil.java
......@@ -48,6 +49,7 @@ E:\IDEA_workspace\manager\src\main\java\track\model\Campaign.java
E:\IDEA_workspace\manager\src\main\java\track\repository\TrackChannelRepository.java
E:\IDEA_workspace\manager\src\main\java\common\model\RoleType.java
E:\IDEA_workspace\manager\src\main\java\exception\TipException.java
E:\IDEA_workspace\manager\src\main\java\common\task\SyncAppDataTask.java
E:\IDEA_workspace\manager\src\main\java\common\repository\TrackAccount4WebRepository.java
E:\IDEA_workspace\manager\src\main\java\util\ResultModel.java
E:\IDEA_workspace\manager\src\main\java\track\model\Account.java
......@@ -66,14 +68,17 @@ E:\IDEA_workspace\manager\src\main\java\common\repository\PaymentRepository.java
E:\IDEA_workspace\manager\src\main\java\common\model\UserLog.java
E:\IDEA_workspace\manager\src\main\java\util\DBUtil.java
E:\IDEA_workspace\manager\src\main\java\track\model\Channel.java
E:\IDEA_workspace\manager\src\main\java\common\model\Notice.java
E:\IDEA_workspace\manager\src\main\java\tkio\repository\CampaignRepository.java
E:\IDEA_workspace\manager\src\main\java\tkio\repository\ChannelRepository.java
E:\IDEA_workspace\manager\src\main\java\util\CipherUtil.java
E:\IDEA_workspace\manager\src\main\java\security\TokenManager.java
E:\IDEA_workspace\manager\src\main\java\common\repository\NoticeRepository.java
E:\IDEA_workspace\manager\src\main\java\tkio\model\Account.java
E:\IDEA_workspace\manager\src\main\java\tkio\model\RoleAuth.java
E:\IDEA_workspace\manager\src\main\java\common\service\MenuService.java
E:\IDEA_workspace\manager\src\main\java\util\StringUtil.java
E:\IDEA_workspace\manager\src\main\java\common\service\NoticeService.java
E:\IDEA_workspace\manager\src\main\java\track\repository\TrackAccountRepository.java
E:\IDEA_workspace\manager\src\main\java\security\cors\CorsFilter.java
E:\IDEA_workspace\manager\src\main\java\util\DateUtil.java
......@@ -85,8 +90,10 @@ E:\IDEA_workspace\manager\src\main\java\security\resolvers\LoginUserMethodArgume
E:\IDEA_workspace\manager\src\main\java\common\model\Menu.java
E:\IDEA_workspace\manager\src\main\java\common\model\TrackAccount4Web.java
E:\IDEA_workspace\manager\src\main\java\common\repository\RoleTypeRepository.java
E:\IDEA_workspace\manager\src\main\java\tkio\task\NoticeTask.java
E:\IDEA_workspace\manager\src\main\java\common\model\Payment.java
E:\IDEA_workspace\manager\src\main\java\common\repository\MenuRepository.java
E:\IDEA_workspace\manager\src\main\java\common\service\impl\OpenApiServiceImpl.java
E:\IDEA_workspace\manager\src\main\java\common\repository\AppInfoRepository.java
E:\IDEA_workspace\manager\src\main\java\common\service\UserService.java
E:\IDEA_workspace\manager\src\main\java\tkio\service\impl\TkioAccountServiceImpl.java
......@@ -100,6 +107,7 @@ E:\IDEA_workspace\manager\src\main\java\common\repository\ReminderRepository.jav
E:\IDEA_workspace\manager\src\main\java\exception\NotFoundException.java
E:\IDEA_workspace\manager\src\main\java\tkio\model\DataAuth.java
E:\IDEA_workspace\manager\src\main\java\security\RedisLoginStatusManager.java
E:\IDEA_workspace\manager\src\main\java\common\service\IOpenApiService.java
E:\IDEA_workspace\manager\src\main\java\util\CharacterUtils.java
E:\IDEA_workspace\manager\src\main\java\track\service\TrackAccountService.java
E:\IDEA_workspace\manager\src\main\java\common\model\City.java
......@@ -108,9 +116,11 @@ E:\IDEA_workspace\manager\src\main\java\common\model\KeyValue.java
E:\IDEA_workspace\manager\src\main\java\tkio\model\App.java
E:\IDEA_workspace\manager\src\main\java\common\repository\AppCategoryRepository.java
E:\IDEA_workspace\manager\src\main\java\dmp\repository\TagCrawlerAppsWandoujiaRepository.java
E:\IDEA_workspace\manager\src\main\java\util\IOUtil.java
E:\IDEA_workspace\manager\src\main\java\dic\OperateObjectTypeEnum.java
E:\IDEA_workspace\manager\src\main\java\dic\RoleTypeEnum.java
E:\IDEA_workspace\manager\src\main\java\tkio\service\impl\AccountFlowRestrictServiceImpl.java
E:\IDEA_workspace\manager\src\main\java\util\AwsS3Util.java
E:\IDEA_workspace\manager\src\main\java\common\model\BackVisit.java
E:\IDEA_workspace\manager\src\main\java\common\service\impl\AppServiceImpl.java
E:\IDEA_workspace\manager\src\main\java\util\RedisUtilNew.java
......
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