1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
import com.google.common.base.Splitter; import com.google.common.base.Supplier; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.util.ObjectUtils;
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.NumberFormat; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executors; import java.util.concurrent.atomic.LongAdder; import java.util.function.*; import java.util.stream.IntStream; import java.util.stream.Stream;
import static com.sun.xml.internal.xsom.impl.UName.comparator; import static java.util.stream.Collectors.*;
@Slf4j public class CheckTableRows { private static BiFunction<String,Integer,CheckTable> checkTable = CheckTable::new; private final static List<CheckTable> markDoneTables = Arrays.asList( checkTable.apply("xxx_FD_MESSAGE_T",5951) ,checkTable.apply("xxx_LOOKUP_CLASSIFY_T",2631) ,checkTable.apply("xxx_PERSONALIZED_SETTING_T",623) ); private final static List<String> tempTables =Arrays.asList("temp_test","temp_test1");
@org.junit.Test public void Test1() throws Exception { Path pathOracle = Paths.get("C:\\Users\\git\\SaveTheWorld\\soup\\src\\test\\java\\com\\cbg\\tools\\demo\\sql\\move", "all_tables_oracle_new.txt"); Path pathMysql = Paths.get("C:\\Users\\git\\SaveTheWorld\\soup\\src\\test\\java\\com\\cbg\\tools\\demo\\sql\\move", "all_tables_mysql.txt"); Stream<String> linesOracle = Files.lines(pathOracle); Stream<String> linesMysql = Files.lines(pathMysql); List<CheckTable> collectOracle = getCheckTables(linesOracle); List<CheckTable> collectMysql = getCheckTables(linesMysql);
collectOracle.removeIf(e->{ String tableName = e.getTableName(); return markDoneTables.stream().filter(m->{ return (m.getTableName()).equalsIgnoreCase(tableName); }).findAny().isPresent()||tempTables.contains(tableName); }); collectOracle.addAll(markDoneTables); Map<Boolean, List<CheckTable>> collect1 = collectOracle.parallelStream().collect(partitioningBy(collectMysql::contains)); List<CheckTable> collectDone = collect1.get(true); List<CheckTable> collectNotDone = collect1.get(false); System.out.println("已完成:"+collectDone); System.out.println("总共:"+collectOracle.size()+",已完成:"+collectDone.size()+"条"+",未完成:"+collectNotDone.size()+"条"); NumberFormat instance = NumberFormat.getInstance(); instance.setMaximumFractionDigits(2); System.out.println("完成进度:"+instance.format((float)collectDone.size()/(float)collectOracle.size()*100)+"%"); System.out.println("未完成:"+collectNotDone); List<String> collectNotDone_noTable = collectNotDone.parallelStream().map(CheckTable::getTableName).filter(e -> { return !collectMysql.stream().map(CheckTable::getTableName).collect(toList()).contains(e); }).collect(toList()); System.out.println("未完成(无表):"+collectNotDone_noTable); System.out.println("未完成(无表):"+collectNotDone_noTable.size()+"条"); List<CheckTable> collectNotDone_hasTable_WrongRows = collectNotDone.parallelStream().filter(e -> { return !collectNotDone_noTable.contains(e.getTableName()); }).collect(toList()); List<List<CheckTable>> collect = collectNotDone_hasTable_WrongRows.parallelStream().map(e -> { List<CheckTable> objects = Lists.newArrayList(); CheckTable checkTable = collectMysql.stream().filter(m -> m.getTableName().equalsIgnoreCase(e.getTableName())).findFirst().get(); objects.add(e); objects.add(checkTable); return objects; }).collect(toList()); System.out.println("未完成(oracle)(表相同,数据量不一致):"+collectNotDone_hasTable_WrongRows); System.out.println("未完成(oracle)(表相同,数据量不一致):"+collectNotDone_hasTable_WrongRows.size()+"条"); System.out.println("未完成(oracle,mysql)(表相同,数据量不一致):"+collect); System.out.println("未完成(oracle,mysql)(表相同,数据量不一致):"+collect.size()+"条");
}
public List<CheckTable> getCheckTables(Stream<String> linesOracle) { return linesOracle.parallel().map(org.apache.logging.log4j.util.Strings::trimToNull).filter( ((Predicate<String>)ObjectUtils::isEmpty).negate() ).map(e -> { CheckTable checkTable = new CheckTable(); Splitter.on("&").trimResults() .withKeyValueSeparator(";").split(e) .forEach((k, v) -> { checkTable.setTableName(k.toLowerCase()); checkTable.setRows(Integer.parseInt(v)); }); return checkTable; }).collect(toList()); } @org.junit.Test public void getOracleInsertData() throws Exception { Path pathOracle = Paths.get("D:\\sqlExport\\"); Stream<Path> list = Files.list(pathOracle); CopyOnWriteArrayList co = new CopyOnWriteArrayList(); List<CompletableFuture> objects = Lists.newArrayList(); list.forEach(e->{ CompletableFuture<String> voidCompletableFuture = CompletableFuture.supplyAsync(() -> { try { Stream<String> linesOracle = Files.lines(e); String name = e.getFileName().toString(); long count = linesOracle.filter(lo -> lo.contains("INSERT INTO")).count(); return name.replace(".sql","") + ";" + count; } catch (Exception e1) { String name = e.getFileName().toString();
return name.replace(".sql","") + ";" + "Exception"; } }, Executors.newFixedThreadPool(100)); voidCompletableFuture.thenAccept(co::add); objects.add(voidCompletableFuture); }); CompletableFuture.allOf((CompletableFuture[])objects.stream().toArray(size->new CompletableFuture[size])).join(); co.removeIf(ObjectUtils::isEmpty); co.stream().sorted().forEach(System.out::println);
}
@org.junit.Test public void Test2() throws Exception { CheckTable checkTable1 = new CheckTable(); CheckTable checkTable2 = new CheckTable(); checkTable1.setTableName("gg"); checkTable2.setTableName("gg"); checkTable1.setRows(0); checkTable2.setRows(0); System.out.println(checkTable1 == checkTable2); String s = org.apache.logging.log4j.util.Strings.trimToNull(" "); System.out.println(ObjectUtils.isEmpty(s)); HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap(); } @org.junit.Test public void TestRepeat() throws Exception { } }
@Data @AllArgsConstructor @NoArgsConstructor class CheckTable { private String tableName;
private Integer rows;
public void setTableName(String tableName) { this.tableName = org.apache.logging.log4j.util.Strings.trimToNull(tableName); }
@Override public String toString() { return "{" + "tableName:'" + tableName + '\'' + ", rows:" + rows + '}'; }
}
|