2021-07-29 10:02:31 +08:00
|
|
|
|
package com.zdjizhi.utils;
|
|
|
|
|
|
|
2021-08-16 18:24:13 +08:00
|
|
|
|
import com.zdjizhi.common.CommonConfig;
|
2021-08-17 18:56:53 +08:00
|
|
|
|
import org.apache.flink.api.java.tuple.Tuple2;
|
2021-08-16 18:24:13 +08:00
|
|
|
|
import org.apache.hadoop.hbase.HBaseConfiguration;
|
|
|
|
|
|
import org.apache.hadoop.hbase.HConstants;
|
|
|
|
|
|
import org.apache.hadoop.hbase.TableName;
|
|
|
|
|
|
import org.apache.hadoop.hbase.client.*;
|
|
|
|
|
|
import org.apache.hadoop.hbase.util.Bytes;
|
|
|
|
|
|
import org.apache.hadoop.io.ArrayWritable;
|
|
|
|
|
|
import org.apache.hadoop.io.IntWritable;
|
|
|
|
|
|
import org.apache.hadoop.io.Writable;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author wlh
|
|
|
|
|
|
*/
|
2021-07-29 10:02:31 +08:00
|
|
|
|
public class HbaseUtils {
|
2021-08-16 18:24:13 +08:00
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(HbaseUtils.class);
|
2021-08-17 18:56:53 +08:00
|
|
|
|
private static Table table = null;
|
2021-08-16 18:24:13 +08:00
|
|
|
|
private static Scan scan = null;
|
2021-08-17 18:56:53 +08:00
|
|
|
|
public static Map<String, Map<String, Tuple2<ArrayList<Integer>, Integer>>> baselineMap = new HashMap<>();
|
|
|
|
|
|
private static ArrayList<String> floodTypeList = new ArrayList<>();
|
2021-08-16 18:24:13 +08:00
|
|
|
|
|
|
|
|
|
|
static {
|
2021-08-17 18:56:53 +08:00
|
|
|
|
floodTypeList.add("TCP SYN Flood");
|
|
|
|
|
|
floodTypeList.add("UDP Flood");
|
|
|
|
|
|
floodTypeList.add("ICMP Flood");
|
|
|
|
|
|
floodTypeList.add("DNS Amplification");
|
2021-08-16 18:24:13 +08:00
|
|
|
|
readFromHbase();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void prepareHbaseEnv() throws IOException {
|
|
|
|
|
|
org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
|
|
|
|
|
|
|
|
|
|
|
|
config.set("hbase.zookeeper.quorum", CommonConfig.HBASE_ZOOKEEPER_QUORUM);
|
|
|
|
|
|
config.set("hbase.client.retries.number", "3");
|
|
|
|
|
|
config.set("hbase.bulkload.retries.number", "3");
|
|
|
|
|
|
config.set("zookeeper.recovery.retry", "3");
|
|
|
|
|
|
config.setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, CommonConfig.HBASE_CLIENT_OPERATION_TIMEOUT);
|
|
|
|
|
|
config.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, CommonConfig.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD);
|
|
|
|
|
|
|
2021-08-17 18:56:53 +08:00
|
|
|
|
TableName tableName = TableName.valueOf(CommonConfig.HBASE_BASELINE_TABLE_NAME);
|
2021-08-16 18:24:13 +08:00
|
|
|
|
Connection conn = ConnectionFactory.createConnection(config);
|
|
|
|
|
|
table = conn.getTable(tableName);
|
|
|
|
|
|
scan = new Scan().setAllowPartialResults(true).setLimit(CommonConfig.HBASE_BASELINE_TOTAL_NUM);
|
|
|
|
|
|
logger.info("连接hbase成功,正在读取baseline数据");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
Set<String> keySet = baselineMap.keySet();
|
2021-08-17 18:56:53 +08:00
|
|
|
|
for (String key : keySet) {
|
|
|
|
|
|
|
2021-08-16 18:24:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
System.out.println(baselineMap.size());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 18:56:53 +08:00
|
|
|
|
private static void readFromHbase() {
|
2021-08-16 18:24:13 +08:00
|
|
|
|
try {
|
|
|
|
|
|
prepareHbaseEnv();
|
|
|
|
|
|
logger.info("开始读取baseline数据");
|
|
|
|
|
|
ResultScanner rs = table.getScanner(scan);
|
|
|
|
|
|
for (Result result : rs) {
|
2021-08-17 18:56:53 +08:00
|
|
|
|
Map<String, Tuple2<ArrayList<Integer>, Integer>> floodTypeMap = new HashMap<>();
|
2021-08-16 18:24:13 +08:00
|
|
|
|
String rowkey = Bytes.toString(result.getRow());
|
2021-08-17 18:56:53 +08:00
|
|
|
|
for (String type:floodTypeList){
|
|
|
|
|
|
ArrayList<Integer> sessionRate = getArraylist(result, type, "session_rate");
|
|
|
|
|
|
Integer defaultValue = getDefaultValue(result, type, "session_rate_default_value");
|
|
|
|
|
|
floodTypeMap.put(type,Tuple2.of(sessionRate, defaultValue));
|
|
|
|
|
|
}
|
|
|
|
|
|
baselineMap.put(rowkey, floodTypeMap);
|
2021-08-16 18:24:13 +08:00
|
|
|
|
}
|
2021-08-17 18:56:53 +08:00
|
|
|
|
logger.info("格式化baseline数据成功,读取IP共:{}", baselineMap.size());
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
logger.error("读取hbase数据失败", e);
|
2021-08-16 18:24:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 18:56:53 +08:00
|
|
|
|
private static Integer getDefaultValue(Result result, String family, String qualifier) {
|
|
|
|
|
|
byte[] value = result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));
|
|
|
|
|
|
if (value != null){
|
|
|
|
|
|
return Bytes.toInt(value);
|
2021-08-16 18:24:13 +08:00
|
|
|
|
}
|
2021-08-17 18:56:53 +08:00
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static ArrayList<Integer> getArraylist(Result result, String family, String qualifier) throws IOException {
|
|
|
|
|
|
if (containsColumn(result, family, qualifier)) {
|
|
|
|
|
|
ArrayWritable w = new ArrayWritable(IntWritable.class);
|
|
|
|
|
|
w.readFields(new DataInputStream(new ByteArrayInputStream(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)))));
|
|
|
|
|
|
return fromWritable(w);
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
2021-08-16 18:24:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static ArrayList<Integer> fromWritable(ArrayWritable writable) {
|
|
|
|
|
|
Writable[] writables = writable.get();
|
|
|
|
|
|
ArrayList<Integer> list = new ArrayList<>(writables.length);
|
|
|
|
|
|
for (Writable wrt : writables) {
|
2021-08-17 18:56:53 +08:00
|
|
|
|
list.add(((IntWritable) wrt).get());
|
2021-08-16 18:24:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 18:56:53 +08:00
|
|
|
|
private static boolean containsColumn(Result result, String family, String qualifier) {
|
|
|
|
|
|
return result.containsColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
|
|
|
|
|
|
}
|
2021-07-29 10:02:31 +08:00
|
|
|
|
|
|
|
|
|
|
}
|