一个大数据的demo

时间:2022-03-10 08:11:23
 package test.admin;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class Algorithm { // 每列的和
public static double one_row = 0,two_row = 0,three_row= 0,four_row=0,five_row=0,six_row=0;
// 每列的除数
public static double one_row_i = 0,two_row_i = 0,three_row_i=0,four_row_i=0,five_row_i=0,six_row_i=0;
// 每列的结果
public static double one_column = 0,two_column = 0,three_column= 0,four_column=0,five_column=0,six_column=0;
// 读取txt计算(加法与除法)四种样式 最后的结果
public static String one_style = "",two_style="",three_style="",four_style=""; // 每列的和
public static double one_row_divisions = 0,two_row_divisions = 0,three_row_divisions= 0,four_row_divisions=0;
// 每列的除数
public static double one_row_j = 0,two_row_j = 0,three_row_j=0,four_row_j=0;
// 读取txt计算(乘法与触发)四种样式 最后的结果
public static String one_division = "",two_division="",three_division="",four_division=""; public static double one = 0,two=0,three=0,four=0,five=0,six=0; public static double one_s = 0,two_s=0,three_s=0,four_s=0; public static double one_i = 0,two_i=0,three_i=0,four_i=0,five_i=0,six_i=0; static List<String> listArr=new ArrayList<String>();
// 多个文件的集合
static List<String> listAddArr=new ArrayList<String>(); static String[] retArr = new String[4];
static String[] retOne = null;
static String[] retTwo = null;
static String[] retThree = null;
static String[] retFour = null;
static String[] divisionArr = null;
static String[] divisionName = null;
static String[] splitValue = null;
static String value=null;
static String lineValue=""; private static BufferedReader brf;
private static FileWriter fw; /**
* 获取目录下的所有TXT文件
*/
public static List<String> getFileList(File file) { List<String> result = new ArrayList<String>(); if (!file.isDirectory()) {
System.out.println(file.getAbsolutePath());
result.add(file.getAbsolutePath());
} else {
File[] directoryList = file.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isFile() && file.getName().indexOf("txt") > -1) {
return true;
} else {
return false;
}
}
});
for (int i = 0; i < directoryList.length; i++) {
result.add(directoryList[i].getPath());
}
} return result;
} public static void main(String[] args) throws Exception {
String centerValue="";
int Js = 0;
int Js_two = 0;
System.out.print("请输入文件读取路径:");
Scanner sc = new Scanner(System.in);
String strv = sc.next();
// 向量相加的 文件位置
String FILE_IN = strv;
File f = new File(FILE_IN);
List<String> list = new ArrayList<String>();
// 调用方法获取txt文件集合与路径
list = getFileList(f);
// 如果向量相加 目录下 有两个TXT文件 需要做第二次向量相加 操作
retOne = new String[list.size()];
retTwo = new String[list.size()];
retThree = new String[list.size()];
retFour = new String[list.size()];
// 循环调用 目录下所有TXT文件
for (String l : list) {
brf = new BufferedReader(new FileReader(new File(l)));
String line = null;
// 读取每一行 内容
while ((line = brf.readLine()) != null) {
String str = line.substring(line.length() - 9,
line.length());
// 根据 四种分类不同做不同的操作
if (str.equals("[1,0,0,0]")) {
// 获取 第一种类型的 结果
one_style = Algorithm.VectorAdd(line);
} else if (str.equals("[0,1,0,0]")){
// 获取 第二种类型的 结果
two_style = Algorithm.VectorAdd(line);
}else if (str.equals("[0,0,1,0]")){
// 获取 第三种类型的 结果
three_style = Algorithm.VectorAdd(line);
}else if (str.equals("[0,0,0,1]")){
// 获取 第四种类型的 结果
four_style = Algorithm.VectorAdd(line);
}
}
// 第二次向量相加并做除法的 集合
retOne[Js] = one_style;
retTwo[Js] = two_style;
retThree[Js] = three_style;
retFour[Js] = four_style;
Js++;
// 重置 one_style two_style 等 参数
resetStyle();
// 重置 one_row 等 参数
resetParameters();
}
// 调用 第二个向量加法
one_style = Algorithm.VectorAdds(retOne);
// 重置 one_row 等 参数
resetParameters();
two_style = Algorithm.VectorAdds(retTwo);
resetParameters();
three_style = Algorithm.VectorAdds(retThree);
resetParameters();
four_style = Algorithm.VectorAdds(retFour);
resetParameters(); System.out.println(one_style+"===[1,0,0,0]");
System.out.println(two_style+"===[0,1,0,0]");
System.out.println(three_style+"===[0,0,1,0]");
System.out.println(four_style+"===[0,0,0,1]");
// 向量点乘 的集合
retArr[0] = one_style;
retArr[1] = two_style;
retArr[2] = three_style;
retArr[3] = four_style; System.out.print("请输入文件读取路径:");
Scanner sc1 = new Scanner(System.in);
String strv1 = sc1.next();
// 向量点乘 的文件位置
String FILE_INFO = strv1;
File f1 = new File(FILE_INFO);
List<String> list1 = new ArrayList<String>();
// 调用方法获取txt文件集合与路径
list1 = getFileList(f1);
divisionArr = new String[list1.size()];
divisionName = new String[list1.size()];
for (int i = 0; i < list1.size(); i++) {
String[] split = list1.get(i).split("\\\\");
String txtName = split[split.length-1];
divisionName[i] = txtName.substring(0, txtName.length()-4);
}
for (String l1 : list1) {
brf = new BufferedReader(new FileReader(new File(l1)));
String line = null;
// 读取每一行内容
while ((line = brf.readLine()) != null) {
//调用点乘方法
one_division = Algorithm.VectorDivision(line,retArr);
centerValue+=one_division;
} centerValue = centerValue.substring(0,centerValue.length()-6);
divisionArr[Js_two]=centerValue;
centerValue="";
Js_two++;
}
// 向量点乘结果 做向量加法
for (int i = 0; i < divisionArr.length; i++) {
value = divisionArr[i];
splitValue = value.split("\\[diff\\]");
value = value.substring(value.length());
for (int j = 0; j < splitValue.length; j++) {
String oneArr = splitValue[j];
String[] split = oneArr.split(",");
for (int n = 0; n < split.length; n++) {
// 取值
double values = Double.valueOf(split[n]).doubleValue();
if(n==0){
one_row_divisions +=values;
one_row_j++;
} else if (n==1) {
two_row_divisions +=values;
two_row_j++;
} else if (n==2){
three_row_divisions +=values;
three_row_j++;
} else if (n==3){
four_row_divisions+=values;
four_row_j++;
}
}
} one_row_divisions = one_row_divisions / one_row_j;
two_row_divisions = two_row_divisions / two_row_j;
three_row_divisions = three_row_divisions / three_row_j;
four_row_divisions = four_row_divisions / four_row_j;
//打印最终结果
System.out.println(one_row_divisions+","+two_row_divisions+","+three_row_divisions+","+four_row_divisions);
one_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/one_row_divisions;
two_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/two_row_divisions;
three_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/three_row_divisions;
four_s = (one_row_divisions+two_row_divisions+two_row_divisions+four_row_divisions)/four_row_divisions;
lineValue+="["+divisionName[i]+"]["+one_s+","+two_s+","+three_s+","+four_s+"]\r\n";;
resetStyle();
}
System.out.print("请输入文件输出路径:");
Scanner sc2 = new Scanner(System.in);
String strv2 = sc2.next();
fw = new FileWriter(strv2+"\\file.txt");
BufferedWriter bf = new BufferedWriter(fw);
bf.write(lineValue);
bf.flush();
} /**
* 向量点乘方法
*/
private static String VectorDivision(String line, String[] addArr) {
double one_row_division=0;
double two_row_division=0;
double three_row_division=0;
double four_row_division=0;
String ret="";
String[] strArr = line.split("\\]\\[");
String subs = strArr[1].substring(0, strArr[1].length()-1);
String[] split = subs.split(",");
for (int i = 0; i < addArr.length; i++) {
String[] split2 = addArr[i].split(",");
for (int j = 0; j < split2.length; j++) {
double file2value = Double.valueOf(split[j]).doubleValue();
double one_style2value = Double.valueOf(split2[j]).doubleValue();
if(i==0){
one_row_division += file2value*one_style2value;
} else if(i==1){
two_row_division += file2value*one_style2value;
} else if(i==2){
three_row_division += file2value*one_style2value;
} else if(i==3){
four_row_division += file2value*one_style2value;
}
}
}
ret=one_row_division+","+two_row_division+","+three_row_division+","+four_row_division+"[diff]";
return ret;
} /**
* 向量加法
*/
static String VectorAdd(String line) { String[] strArr = line.split("\\]\\["); String[] numArr = strArr[1].split(",");
for (int i = 0; i < numArr.length; i++) {
double doubleValue = Double.valueOf(numArr[i]).doubleValue();
if(i==0){
one_row +=doubleValue;
one_row_i++;
} else if (i==1) {
two_row +=doubleValue;
two_row_i++;
} else if (i==2){
three_row +=doubleValue;
three_row_i++;
} else if (i==3){
four_row+=doubleValue;
four_row_i++;
} else if (i==4){
five_row+=doubleValue;
five_row_i++;
} else if (i==5){
six_row+=doubleValue;
six_row_i++;
} }
one_column = one_row / one_row_i;
two_column = two_row / two_row_i;
three_column = three_row / three_row_i;
four_column = four_row / four_row_i;
five_column = five_row / five_row_i;
six_column = six_row / six_row_i;
String result = one_column+","+two_column+","+three_column+","+four_column+","+five_column+","+six_column;
return result;
}
/**
* 两个TXT文件调用向量加法
*/
static String VectorAdds(String[] line) {
for (int i = 0; i < line.length; i++) {
if(line[i]!=""){
String[] split = line[i].split(",");
for (int j = 0; j < split.length; j++) {
// 取值
double value = Double.valueOf(split[j]).doubleValue();
if(j==0){
one +=value;
one_i++;
} else if (j==1) {
two +=value;
two_i++;
} else if (j==2){
three +=value;
three_i++;
} else if (j==3){
four+=value;
four_i++;
} else if (j==4){
five+=value;
five_i++;
} else if (j==5){
six+=value;
six_i++;
}
}
}
} one_column = one / one_i;
two_column = two / two_i;
three_column = three / three_i;
four_column = four / four_i;
five_column = five / five_i;
six_column = six / six_i;
String result = one_column+","+two_column+","+three_column+","+four_column+","+five_column+","+six_column;
return result;
} /**
* 重置参数
* @return
*/
static void resetParameters() {
one_row_i = 0;
two_row_i = 0;
three_row_i=0;
four_row_i=0;
five_row_i=0;
six_row_i=0;
one_row = 0;
two_row = 0;
three_row=0;
four_row=0;
five_row=0;
six_row=0;
one_i = 0;
two_i = 0;
three_i=0;
four_i=0;
five_i=0;
six_i=0;
one = 0;
two = 0;
three=0;
four=0;
five=0;
six=0;
} /**
* 重置参数
* @return
*/
static void resetStyle(){
splitValue=null;
value=null;
one_style="";
two_style="";
three_style="";
four_style="";
one_row_divisions=0;
two_row_divisions=0;
three_row_divisions=0;
four_row_divisions=0;
one_row_j=0;
two_row_j=0;
three_row_j=0;
four_row_j=0;
} }

第一个文件路径 是计算向量相加(最后一个[]中  数据想通的做相加)

一个大数据的demo

第二个文件路径 是计算向量点乘

一个大数据的demo

第三个文件路径  是输出结果!

转载请注明出处!谢谢!