计算智博会PM的(中兴的5g和4g,华为的5g和4g)数据到redis中
程序员文章站
2022-05-02 10:07:54
...
这是 需求大慨分析和我的总体逻辑思路
package data;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import redis.clients.jedis.Jedis;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class index01{
static double [] kpi123_4G;
static double [] kpi123_5G;
static double sum12_4G;
static double sum12_5G;
static double sum11_4G;
static double sum11_5G;
/**
* 存放读取csv的路径
* @param DirPath
* @return
* @throws IOException
*/
//拿取csv数据
public static List<String> getCSV(String DirPath) throws IOException {
//获取存放文件的目录
File dirpath = new File(DirPath);
//将该目录下的所有文件放置在一个File类型的数组中
File[] filelist = dirpath.listFiles();
//创建一个空的file类型的list集合,用于存放符合条件的文件
List<File> newfilelist = new ArrayList<>();
//遍历filelist,如果是文件则存储在新建的list集合中
for (File file : filelist) {
if (file.isFile()){
newfilelist.add(file);
}
}
//新建list集合,用于存放读取到的数据
List<String> list = new ArrayList<>();
//读取文件内容
String s = null;
int count=0;
for (File file : newfilelist) {
//判断:如果是CSV文件,
if (file.toString().endsWith(".csv")){
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((s = bufferedReader.readLine())!=null){
//把读取到的数据存放在list集合
list.add(System.lineSeparator()+s);
}
}
}
return list;
}
//读取字典数据并判断Csveci和字典eci相同的数据
public static void CsvEciAndzdEci(String Csvfilepath,String zdfilepath,String net) throws Exception {
//读取字典数据
File csv = new File(zdfilepath);
BufferedReader br = new BufferedReader(new FileReader(csv));
String line = "";
//创建list集合,用于存放读取到的字典数据
List<String> allString = new ArrayList<>();
//将数据逐条读取到list集合
while ((line = br.readLine()) != null)
{
allString.add(line);
}
//创建存放5g数据的list
ArrayList<String> NRlist = new ArrayList<>();
//创建存放4g数据的list
ArrayList<String> FDDlist = new ArrayList<>();
//把数据分别插入到4G和5G的list中
for (String s : allString) {
// System.out.println(s);
String[] split = s.split("\t");
if (split[0].equals("FDD")){
//封装4g数据
FDDlist.add(split[1]);
}
else if (split[0].equals("NR")){
//封装5g数据
NRlist.add(split[1]);
}
}
//新建一个map集合,用于存放过滤之后的csv中的数据
Map<String, String[]> use_4GMap = new HashMap<>();
//新建一个map集合,用于存放过滤之后的csv中的数据
Map<String, String[]> use_5GMap = new HashMap<>();
//调用getCSV方法,得到csvlist(csv所有数据)
List<String> csvlist = getCSV(Csvfilepath);
//遍历csvlist,以逗号分隔,拿到eci字段
for (String csvall : csvlist) {
String[] split = csvall.split(",");
String csveci = split[22];
//遍历字典中4G的eci
for (String FDDeci : FDDlist) {
//判断:字典的eci与csv中的eci是否相同,如果相同就把CSV中需要的数据存放在数组里面
if (FDDeci.equals(csveci)) {
// csv所有的数据按,号进行切割
String[] CsvSplit = csvall.split(",");
//把分隔后的csv所有的数据放进数组
//CsvSplit[0]=k01 CsvSplit[1]=k02 CsvSplit[4]=k0
// k01 k02 k04 k05 k22 k21 k12 k11
String[] name = {CsvSplit[0], CsvSplit[1], CsvSplit[3], CsvSplit[4], CsvSplit[23], CsvSplit[20], CsvSplit[11], CsvSplit[10]};
//存放 key为字典的eci value为数组(csv中所需的字段)
use_4GMap.put(FDDeci, name);
}
}
//遍历字典中5G的eci
for (String NReci : NRlist) {
//判断:字典的eci与csv中的eci是否相同,如果相同就把CSV中需要的数据存放在数组里面
if (NReci.equals(csveci)) {
// csv所有的数据按,号进行切割
String[] CsvSplit = csvall.split(",");
//把分隔后的csv所有的数据放进数组
//CsvSplit[0]=k01 CsvSplit[1]=k02 CsvSplit[4]=k0
// k01 k02 k04 k05 k22 k21 k12 k11
String[] name = {CsvSplit[0], CsvSplit[1], CsvSplit[3], CsvSplit[4], CsvSplit[23], CsvSplit[20], CsvSplit[11], CsvSplit[10]};
//存放 key为字典的eci value为数组(csv中所需的字段)
use_5GMap.put(NReci, name);
System.out.println(NReci+" "+Arrays.toString(name));
}
}
}
// kpi为无线接通率 + kpi2为利用率+ kpi3为平均速率
if (net.equals("4G")){
kpiWirelessConnection_4G(use_4GMap);
kpi12And11sum_4G(use_4GMap);
}
if (net.equals("5G")){
kpiWirelessConnection_5G(use_5GMap);
kpi12And11sum_5G(use_5GMap);
}
}
//对4G的数据进行计算
public static void kpiWirelessConnection_4G(Map<String,String[]> useMap) throws Exception {
//kpi1为无线接通率
//总的k01值
double k01sum=0;
//总的k02值
double k02sum=0;
//总的k04值
double k04sum=0;
//总的k05值
double k05sum=0;
//kpi2为利用率
int count1 =0;
//总的k22的值
double k22=0;
//kpi3为平均速率
int count2 =0;
double k21=0;
//循环遍历map集合 进行计算
//获取Map中value的k01的值
Iterator<Map.Entry<String, String[]>> entries = useMap.entrySet().iterator();
while(entries.hasNext()){
Map.Entry<String, String[]> entry = entries.next();
String key = entry.getKey();
String[] value = entry.getValue();
// 获取Map中value的k01的值
if (value[0]!=null&&!value[0].contains("None")&&value[0]!=""&&value[1]!=null&&!value[1].contains("None")&&value[1]!=""&&value[2]!=null&&!value[2].contains("None")&&value[2]!=""&&value[3]!=null&&!value[3].contains("None")&&value[3]!=""){
//System.out.println(key+":"+Arrays.toString(value));
k01sum+=Double.parseDouble(value[0]);
//获取Map中value的k02的值
k02sum+=Double.parseDouble(value[1]);
//获取Map中value的k03的值
k04sum+=Double.parseDouble(value[2]);
//获取Map中value的k04的值
k05sum+=Double.parseDouble(value[3]);
}
if (value[4]!=""&&!value[4].contains("None")&&value[4]!=null) {
count1 ++;
k22 += Double.parseDouble(value[4]);
}
if (value[5]!=""&&!value[5].contains("None")&&value[5]!=null) {
count2 ++;
k21 += Double.parseDouble(value[5]);
}
}
double kpi1_4G = Math.round(((k01sum / k02sum) * (k04sum / k05sum))*100.00);
System.out.println("kpi1为无线接通率_4G="+kpi1_4G);
double kpi2_4G= k22/ count1;
System.out.println("kpi2利用率_4G="+kpi2_4G);
double kpi3_4G= k21/ count2;
System.out.println("kpi3为平均速率_4G="+ kpi3_4G);
kpi123_4G = new double[] {kpi1_4G,kpi2_4G,kpi3_4G};
}
//对5G的数据进行计算
public static void kpiWirelessConnection_5G (Map<String,String[]> useMap) {
//总的k01值
double k01sum=0;
//总的k02值
double k02sum=0;
//总的k04值
double k04sum=0;
//总的k05值
double k05sum=0;
//kpi2为利用率
int count1 =0;
//总的k22的值
double k22=0;
//kpi3为平均速率
int count2 =0;
double k21=0;
//循环遍历map集合 进行计算
//获取Map中value的k01的值
Iterator<Map.Entry<String, String[]>> entries = useMap.entrySet().iterator();
while(entries.hasNext()){
Map.Entry<String, String[]> entry = entries.next();
String key = entry.getKey();
String[] value = entry.getValue();
//获取Map中value的k01的值
if (value[0]!=null&&!value[0].contains("None")&&value[0]!=""&&value[1]!=null&&!value[1].contains("None")&&value[1]!=""&&value[2]!=null&&!value[2].contains("None")&&value[2]!=""&&value[3]!=null&&!value[3].contains("None")&&value[3]!=""){
//System.out.println(key+":"+Arrays.toString(value));
k01sum+=Double.parseDouble(value[0]);
//获取Map中value的k02的值
k02sum+=Double.parseDouble(value[1]);
//获取Map中value的k03的值
k04sum+=Double.parseDouble(value[2]);
//获取Map中value的k04的值
k05sum+=Double.parseDouble(value[3]);
}
if (value[4]!=""&&!value[4].contains("None")&&value[4]!=null) {
count1 ++;
k22 += Double.parseDouble(value[4]);
}
if (value[5]!=""&&!value[5].contains("None")&&value[5]!=null) {
count2 ++;
k21 += Double.parseDouble(value[5]);
}
}
double kpi1_5G = Math.round(((k01sum / k02sum) * (k04sum / k05sum))*100.00);
System.out.println("kpi1为无线接通率_5G="+kpi1_5G);
double kpi2_5G= k22/ count1;
String kpi2_5G_string = String.valueOf(kpi2_5G);
if (kpi2_5G_string.equals("NaN")){
kpi2_5G = 0;
}
System.out.println("kpi2利用率_5G="+kpi2_5G);
double kpi3_5G= k21/ count2;
String kpi3_5G_string = String.valueOf(kpi3_5G);
if (kpi3_5G_string.equals("NaN")){
kpi3_5G = 0;
}
System.out.println("kpi3为平均速率_5G="+ kpi3_5G);
kpi123_5G = new double[] {kpi1_5G,kpi2_5G,kpi3_5G};
}
/**
* K12 和 k11
* sun(k12)
* @throws Exception
*/
//4G 的K12和K11
public static void kpi12And11sum_4G(Map<String,String[]> useMap) throws Exception {
Iterator<Map.Entry<String, String[]>> entries = useMap.entrySet().iterator();
//sum k12
double k12_4G=0;
//sum k11
double k11_4G=0;
while(entries.hasNext()){
Map.Entry<String, String[]> entry = entries.next();
String key = entry.getKey();
String[] value = entry.getValue();
if (value[6]!=""&&!value[6].contains("None")&&value[6]!=null) {
k12_4G += Double.parseDouble(value[6]);
}
if (value[7]!=""&&!value[7].contains("None")&&value[7]!=null) {
k11_4G += Double.parseDouble(value[7]);
}
}
System.out.println("k12_4G = "+k12_4G);
System.out.println("k11_4G = "+k11_4G);
sum11_4G = k11_4G;
sum12_4G = k12_4G;
}
//5G 的K12和K11
public static void kpi12And11sum_5G(Map<String,String[]> useMap) throws Exception {
Iterator<Map.Entry<String, String[]>> entries = useMap.entrySet().iterator();
//sum k12
double k12_5G=0;
//sum k11
double k11_5G=0;
while(entries.hasNext()){
Map.Entry<String, String[]> entry = entries.next();
String key = entry.getKey();
String[] value = entry.getValue();
if (value[6]!=""&&!value[6].contains("None")&&value[6]!=null) {
k12_5G += Double.parseDouble(value[6]);
}
if (value[7]!=""&&!value[7].contains("None")&&value[7]!=null) {
k11_5G += Double.parseDouble(value[7]);
}
}
System.out.println("k12_5G = "+k12_5G);
System.out.println("k11_5G = "+k11_5G);
sum11_5G = k11_5G;
sum12_5G = k12_5G;
}
public static void adddata(Jedis jedis,Double time,String time11,String time12,String net) throws JSONException {
//4G
if (net.equals("4G")){
JSONObject jsonObject_4G = new JSONObject();
jsonObject_4G.put("kpi1",kpi123_4G[0]);
jsonObject_4G.put("kpi2",kpi123_4G[1]);
jsonObject_4G.put("kpi3",kpi123_4G[2]);
String s_4G = jsonObject_4G.toString();
jedis.zadd("k1_4GKpi",time,s_4G);
// System.out.println(s_4G);
}
//5G
if (net.equals("5G")){
JSONObject jsonObject_5G = new JSONObject();
jsonObject_5G.put("kpi1",kpi123_5G[0]);
jsonObject_5G.put("kpi2",kpi123_5G[1]);
jsonObject_5G.put("kpi3",kpi123_5G[2]);
String s_5G = jsonObject_5G.toString();
jedis.zadd("k1_5GKpi",time,s_5G);
}
// System.out.println(s_5G);
//sum11
double sum_11 = sum11_4G + sum11_5G;
// System.out.println("sum_11 = "+sum_11);
//sum12
double sum_12 = sum12_4G + sum12_5G;
// System.out.println("sum_12 = "+sum_12);
//写进redis
jedis.zincrby("k1_UserKpi",sum_12, time12);
jedis.zincrby("k1_FlowKpi",sum_11, time11);
}
public static void main(String[] args) throws Exception {
//设置连接redis的主机名
String host = args[0];
//设置端口号
int port = Integer.parseInt(args[1]);
//设置密码
String password = args[2];
//设置连接的数据库
int database = Integer.parseInt(args[3]);
//设置字典数据的路径
String zdPath = args[4];
//设置CSV数据的路径
String csvPath = args[5];
//设置网络类型
String net = args[6];
//设置K11的时间戳
String timestamp_11 = args[7];
//设置K12的时间戳
String timestamp_12 = args[8];
//创建jedis对象,设置主机名,端口号
Jedis jedis = new Jedis(host, port);
jedis.auth(password);//设置密码
jedis.select(database);//设置数据库
//调用方法
CsvEciAndzdEci(csvPath,zdPath,net);
//创建当前时间戳
Double time = Double.valueOf(new Date().getTime());
//通过对象调用API
adddata(jedis,time,timestamp_11,timestamp_12,net);
}
}
上一篇: 女人不爱你才会有这些表现,你知道吗?