package com.bawei.test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class TestApi {
// 声明链接
private static Connection connection = null;
// 声明管理员对象
private static Admin admin = null;
// 初始化
static {
try {
Configuration configuration = HBaseConfiguration.create();
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 释放资源
*/
private static void close() throws IOException {
if (admin != null) {
admin.close();
}
if (connection != null) {
connection.close();
}
}
/**
* 判断表是否存在
*/
public static boolean isTableExsit(String tableName) throws IOException {
boolean exists = admin.tableExists(TableName.valueOf(tableName));
return exists;
}
/**
* 创建表
*/
public static void createTable(String tableName, String... cf) throws IOException {
if (isTableExsit(tableName)) {
System.out.println("表已经存在,在点我就报警了!");
return;
}
if (cf.length <= 0) {
System.out.println("参数异常!");
return;
}
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String s : cf) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(s);
descriptor.addFamily(hColumnDescriptor);
}
admin.createTable(descriptor);
}
/**
* 删除表
*/
public static void deleteTable(String tableName) throws IOException {
if (!isTableExsit(tableName)) {
System.out.println("表不存在!");
return;
}
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
}
/**
* 创建命名空间
*/
public static void createNameSpace(String nameSpace) throws IOException {
try {
NamespaceDescriptor build = NamespaceDescriptor.create(nameSpace).build();
admin.createNamespace(build);
} catch (NamespaceExistException e) {
System.out.println(nameSpace + "此命名空间已经存在!");
} catch (IOException e) {
e.printStackTrace();
}
}
// =====================================>我是华丽分割线===================================>
/**
* 添加数据
*/
public static void put(String tableName, String rk, String cf, String cn, String value) throws IOException {
// 创建表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 声明put对象
Put putObject = new Put(Bytes.toBytes(rk));
// 向put对象中添加值
putObject.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
// 向表中添加数据
table.put(putObject);
// 关闭
table.close();
}
/**
* 查询数据 ---》 get
*/
public static void get(String tableName, String rk) throws IOException {
// 1. 获取对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 2. 获得get对象
Get getObject = new Get(Bytes.toBytes(rk));
// 3. 获取查询结果
Result result = table.get(getObject);
// 4. 遍历获取单个cell并打印
for (Cell cell : result.rawCells()) {
System.out.println("RK : " + Bytes.toString(CellUtil.cloneRow(cell)) +
", CF : " + Bytes.toString(CellUtil.cloneFamily(cell)) +
", CN : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
", Value : " + Bytes.toString(CellUtil.cloneValue(cell)));
}
// 5. 释放资源
table.close();
}
/**
* 查询数据 ---> scan
*/
public static void scan(String tableName) throws IOException {
// 1. 获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 2. 创建Scan对象
Scan scanObject = new Scan(Bytes.toBytes("1001"), Bytes.toBytes("1003"));
// 3. 查询数据并获取结果
ResultScanner scanner = table.getScanner(scanObject);
// 4. 遍历
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println("RK : " + Bytes.toString(CellUtil.cloneRow(cell)) +
", CF : " + Bytes.toString(CellUtil.cloneFamily(cell)) +
", CN : " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
", Value : " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
// 5. 释放资源
table.close();
}
/**
* 删除数据 ---> delete
*/
public static void delete(String tableName, String rk) throws IOException {
// 1. 获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 2. 执行删除逻辑
Delete deleteObject = new Delete(Bytes.toBytes(rk));
table.delete(deleteObject);
// 2. 释放资源
table.close();
}
/**
* 测试Api
*
* @param args
*/
public static void main(String[] args) throws IOException {
// 1. 测试表是否存在
// System.out.println(isTableExsit(“employee”));
// 2. 创建表
// createTable(“user”, “info”, “name”);
// 3. 删除表
// deleteTable(“user”);
// 4. 创建命名空间
// createNameSpace(“1703”);
// 5. 插入数据
// put(“user”, “1003”, “info”, “name”, “qiaoyuan2”);
// 6. 查询数据 get
// get(“user”, “1001”);
// 7. 查询数据 scan
// scan(“user”);
// 8. 删除数据 delete
delete(“user”, “1003”);
// 释放资源
close();
}
}
import java.io.{FileOutputStream, OutputStreamWriter}
import java.text.{DecimalFormat, SimpleDateFormat}
import java.util
import java.util.Date
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks
/**
- 初始化电话信息
*/
object PeoductLog {
//创建ArrayBuffer存放联系人天花号码
var phoneList:ArrayBuffer[String]=ArrayBuffer()
//创建hashMap存放联系人姓名电话映射
var phoneNameMap:mutable.HashMap[String,String] = mutable.HashMap()
//存放联系人电话姓名映射
def initPhone() = {
phoneList.append(“17078388295”)
phoneList.append(“17078388294”)
phoneNameMap.put("17078388295","李四")
phoneNameMap.put("17078388294","王五")
}
def randomBuildTime(startTime: String,endTime:String): String = {
val sdf = new SimpleDateFormat(“yyyy-MM-dd”)
val startDate = sdf.parse(startTime)
val endDate = sdf.parse(endTime)
if (endDate.getTime <= startDate.getTime) return null
var randomTS = startDate.getTime+((endDate.getTime-startDate.getTime)*Math.random()).toLong
val resultDate = new Date(randomTS)
val sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val str = sdf2.format(resultDate)
str
}
def product() = {
var caller = “”
var callee = “”
var callerName = “”
var calleeName = “”
//获得主叫号码
val calleeIndex = (Math.random() * phoneList.size).toInt
caller = phoneList(calleeIndex)
calleeName=phoneNameMap(caller)
import scala.util.control.Breaks.{break,breakable}
breakable{
while(true){
//获得被叫电话
val calleeIndex = (Math.random()*phoneList.size).toInt
callee=phoneList(calleeIndex)
calleeName=phoneNameMap(callee)
if (!caller.equals(callee)) break();
}
}
val buildTime = randomBuildTime(“2020-1-2”,“2020-3-4”)
val df = new DecimalFormat(“0000”)
val duration = df.format((3060Math.random()).toInt)
val sb = new StringBuilder
sb.append(caller+",").append(callee+",").append(buildTime+",").append(duration)
sb.toString()
}
def writeLog(filePath: String): Unit = {
//将数据写入到文件
val osw = new OutputStreamWriter(new FileOutputStream(filePath),“UTF-8”)
val breaks = new Breaks
while (true){
Thread.sleep(100)
val str = product()
osw.write(str+"\n")
osw.flush()
println(str)
}
}
def main(args: Array[String]): Unit = {
val logPath=“D:\calllog.csv”
PeoductLog.initPhone()
PeoductLog.writeLog(logPath)
}
}