Commit d3059ac0 authored by 李凯's avatar 李凯

first commit

parents
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;
public class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if (mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if (mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.syc.slm</groupId>
<artifactId>sync-tools</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sync-tools</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<gson.version>2.8.6</gson.version>
<influxdb-java.version>2.4</influxdb-java.version>
<httpclient.version>4.5.2</httpclient.version>
<httpmime.version>4.5.2</httpmime.version>
<httpcore.version>4.4.4</httpcore.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- influxdb架包 -->
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>${influxdb-java.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpmime.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.syc.slm.tools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SyncInfluxToLogApplication {
public static void main(String[] args) {
SpringApplication.run(SyncInfluxToLogApplication.class, args);
}
}
package com.syc.slm.tools.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
public class InfluxdbConfig {
@Value("${influxdb.host}")
public String host;
@Value("${influxdb.port}")
public String port;
@Value("${influxdb.username}")
public String username;
@Value("${influxdb.password}")
public String password;
}
package com.syc.slm.tools.service;
public interface ISyncService {
void sync();
}
package com.syc.slm.tools.service.impl;
import com.google.gson.Gson;
import com.syc.slm.tools.service.ISyncService;
import com.syc.slm.tools.vo.DataVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.*;
import java.util.*;
/**
* lin
*/
@Service
public class SyncServiceImpl implements ISyncService {
@Value("${current.syncPath}")
private String uploadPath;
@Value("${current.service}")
private String service;
@Value("#{'${current.containsContent}'.split(',')}")
private Set<String> containsContent;
//# todo 啊凯补充
@Override
public void sync() {
List<DataVo> dataVos = readFileList(uploadPath);
}
private List<DataVo> readFileList(String filepath){
List<DataVo> datas=new ArrayList<>();
try {
File file = new File(filepath);
if (!file.isDirectory()) {
datas.addAll(readFile(file));
} else {
String[] filelist = file.list();
if(filelist!=null && filelist.length>0) {
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
datas.addAll(readFile(file));
} else {
readFileList(filepath + "\\" + filelist[i]);
}
}
}
}
} catch (Exception e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return datas;
}
private List<DataVo> readFile( File readfile){
List<DataVo> datas=new ArrayList<>();
try {
System.out.println("文件");
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath=" + readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());
BufferedReader in = new BufferedReader(new FileReader(readfile));
String line ;
while ((line=in.readLine())!=null){
if(containsContent.stream().anyMatch(line::contains) && line.contains(service)){
datas.add(initData(line));
}
}
}catch (Exception e) {
e.printStackTrace();
}
return datas;
}
private DataVo initData(String data){
DataVo vo = new DataVo();
String time =data.substring(data.indexOf("time"),data.indexOf("database"));
vo.setTime(time.replaceAll("time:\\[","").replaceAll("\\]","").trim());
String database=data.substring(data.indexOf("database"),data.indexOf("measurement"));
vo.setDatabase(database.replaceAll("database: \\[","").replaceAll("\\],","").trim());
String measurement=(data.substring(data.indexOf("measurement"),data.indexOf("data-tags")));
vo.setMeasurement(measurement.replaceAll("measurement: \\[","").replaceAll("\\],","").trim());
String dataTags=(data.substring(data.indexOf("data-tags"),data.indexOf("data-fields")));
String dataTagsTemp =dataTags.replaceAll("data-tags: \\[\\{","").replaceAll("\\}\\],","");
String[] dataTagsArray = dataTagsTemp.split(",");
Map<String, String> dataTagsMaps = new HashMap<>();
Arrays.stream(dataTagsArray).forEach(x -> {
String[] split = x.split("=");
dataTagsMaps.put(split[0], split[1].trim());
});
vo.setTags(dataTagsMaps);
String dataFields=(data.substring(data.indexOf("data-fields"),data.indexOf("Exception")));
String dataFieldsTemp =dataFields.replaceAll("data-fields: \\[\\{","").replaceAll("\\}\\],","");
String[] dataFieldsArray = dataFieldsTemp.split(",");
Map<String,String> dataFieldsMaps= new HashMap<>();
Arrays.stream(dataFieldsArray).forEach(x -> {
String[] split = x.split("=");
dataFieldsMaps.put(split[0], split[1].trim());
});
vo.setFields(dataFieldsMaps);
System.out.println(new Gson().toJson(vo));
return vo;
}
}
package com.syc.slm.tools.utils;
import com.syc.slm.tools.config.InfluxdbConfig;
import okhttp3.OkHttpClient;
import org.apache.http.ssl.SSLContexts;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* 创建时间:2018-01-28
* <p>修改时间:2018-01-28
* <p>类说明:TOTO
*
* @author luke
* @version 1.0
*/
@Component
public class InfluxUtils {
@Autowired
private InfluxdbConfig influxdbConfig;
private InfluxDB influxDB;
@PostConstruct
private void init() {
if (influxdbConfig.port.equals("8635")) {
// 华为云: https + getBuilder
influxDB = InfluxDBFactory.connect(String.format("https://%s:%s", influxdbConfig.host, influxdbConfig.port),
influxdbConfig.username,
influxdbConfig.password,
getBuilder());
} else {
influxDB = InfluxDBFactory.connect(String.format("http://%s:%s", influxdbConfig.host, influxdbConfig.port),
influxdbConfig.username,
influxdbConfig.password);
}
}
/**
* 插入
*
* @param measurement 表
* @param tags 标签
* @param fields 字段
*/
public void insert(String database, String measurement, Map<String, String> tags, Map<String, Object> fields) {
Point.Builder builder = Point.measurement(measurement);
builder.tag(tags);
builder.fields(fields);
influxDB.write(database, "", builder.build());
}
/**
* 批量插入
*
* @param measurement 表
* @param tagsList 标签
* @param fieldsList 字段
*/
public void insert(String database,
String measurement,
List<Map<String, String>> tagsList,
List<Map<String, Object>> fieldsList) {
BatchPoints.Builder bpBuilder = BatchPoints.database(database);
if (fieldsList != null && fieldsList.size() > 0) {
for (int i = 0; i < fieldsList.size(); i++) {
Point.Builder builder = Point.measurement(measurement);
builder.tag(tagsList.get(i));
builder.fields(fieldsList.get(i));
bpBuilder.point(builder.build());
}
}
influxDB.write(bpBuilder.build());
}
/**
* 按时间插入数据
*
* @param measurement 表
* @param tags 标签
* @param fields 字段
*/
public void insertByTime(String database,
String measurement,
Map<String, String> tags,
Map<String, Object> fields,
long time) {
Point.Builder builder = Point.measurement(measurement);
builder.tag(tags);
builder.fields(fields);
builder.time(time, TimeUnit.MILLISECONDS);
influxDB.write(database, "", builder.build());
}
/**
* 查询
*
* @param command 查询语句
* @return
*/
public QueryResult query(String database, String command) {
return influxDB.query(new Query(command, database));
}
/**
* 免除SSL
*
* @return
*/
private static OkHttpClient.Builder getBuilder() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(defaultSslSocketFactory(), defaultTrustManager());
builder.hostnameVerifier(noopHostnameVerifier());
return builder;
}
private static X509TrustManager defaultTrustManager() {
return new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
};
}
private static SSLSocketFactory defaultSslSocketFactory() {
try {
SSLContext sslcontext = SSLContexts.createDefault();
sslcontext.init(null, new TrustManager[] {defaultTrustManager()}, new SecureRandom());
return sslcontext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static HostnameVerifier noopHostnameVerifier() {
return new HostnameVerifier() {
@Override
public boolean verify(final String s, final SSLSession sslSession) {
return true;
}
};
}
}
package com.syc.slm.tools.vo;
import lombok.Data;
import java.util.Map;
@Data
public class DataVo {
private String time;
private String database;
private String measurement;
private Map<String,String> tags;
private Map<String,String> fields;
}
influxdb:
host: 139.159.254.185
port: 8086
username: syc
password: syc
current:
syncPath: C:\\Users\\Administrator\\Desktop\\logs\\
containsContent: 5eec2f61496bac073bb6dbec_0d95b361804041e59a92e7f37ecd400e,5eec2f61496bac073bb6dbec_300dba559974424fb4c82dc2ccf61d21
service: com.syc.core.di.service.InfluxService
server:
port: 2021
package com.syc.slm.tools;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SyncInfluxToLogApplicationTests {
@Test
void contextLoads() {
}
}
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