Java_Habse_shell
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException;
public class HBaseOperation{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static long ts;
public static void main(String[] args)throws IOException{
//createTable("Student",new String[]{"Score"});
//createTable("SC",new String[]{"Score"});
//createTable("Course",new String[]{"Score"});
/*
String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"};
String[] values = {"99", "80", "100"};
try {
addRecord("Student", "Score", fields, values);
}
catch (IOException e) {
e.printStackTrace();
}
*/ //scanColumn("Student", "Score"); // try {
// modifyData("Student", "Score", "Math", "100");
// } catch (IOException e) {
// e.printStackTrace();
// } try {
deleteRow("Student", "Score");
} catch (IOException e) {
e.printStackTrace();
}
}
//建立连接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
//建表
public static void createTable(String myTableName,String[] colFamily) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println(myTableName+"表建立成功--李运辰");
}
close();
}
//删表
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
//添加
public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
for (int i = 0; i != fields.length; i++) {
Put put = new Put(row.getBytes());
String[] cols = fields[i].split(":");
put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes()); table.put(put);
}
table.close(); close();
System.out.println("添加成功--李运辰");
} //删除数据
public static void deleteRow(String tableName, String row) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete=new Delete(row.getBytes());
table.delete(delete);
table.close(); close();
System.out.println("删除成功--李运辰"); } public static void modifyData(String tableName, String row, String column, String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(row.getBytes());
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result r : resultScanner) {
for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {
ts = cell.getTimestamp();
}
}
put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
table.put(put);
table.close();
close();
System.out.println("更新成功--李运辰");
} public static void scanColumn(String tableName, String column) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.addFamily(Bytes.toBytes(column));
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next();result != null;result = scanner.next()) {
showCell(result);
}
table.close();
close();
}
//格式化输出
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
}
Java_Habse_shell的更多相关文章
随机推荐
- rest_framework:解析器
一.解析器的作用 根据请求头content-type选择对应的解析器对请求体内容进行处理. 有application/json,x-www-form-urlencoded,form-data等格式 二 ...
- 虚拟机中CentOS 6.5 添加扩展分区
此扩展方法要求支持LVM 1.更改虚拟机配置 虚拟机->设置->硬盘->扩展磁盘容量 fdisk -l 查看,发现硬盘空间变大了 [root@thj Desktop]# fdisk ...
- 题解【洛谷P5658】[CSP-S 2019]括号树
题面 一道简单的栈与\(\text{DP}\)的结合. 首先介绍一下序列上的括号匹配问题,也就是此题在序列上的做法: 设 \(dp_i\) 表示以 \(i\) 结尾的合法的括号序列个数, \(ss_i ...
- pytest学习3-断言
断言: 一个标准的用例都包含了断言,编写pytest自动化脚本的时候,也需要设置断言 pytest常用断言比较大小与是否相等.是否包含.验证boolean 例子一: 验证是否相等: import py ...
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- CentOS7识别不到win10启动项的解决方法
Windows的文件系统是NTFS格式的,而CentOS是不支持NTFS格式的.因此,我们要安装另外的工具使CentOS能识别NTFS格式的文件系统. 这里我们选择ntfs-3g这个工具,安装过程如下 ...
- 1015 Reversible Primes
1. 题目 2. 抽象建模 无 3. 方法 无 4. 注意点 素数判断(1不是素数) 数值的倒转 5. 代码 #include<stdio.h> #include<math.h> ...
- 051_switch语句的使用 052_while循环详解 053_for循环详解_dowhile简介 054_嵌套循环_循环相关练习
051_switch语句的使用 package testmode2;/** * 测试switch语句 * 遇到多值判断的时候,使用switch.当然,switch完全可以使用ifelseifelse代 ...
- 页面中<link>和<script>标签
在html中,经常肯定会有js,css的引入 <head> <title>MyHtml</title> <link rel="stylesheet& ...
- ThinkPHP中的时间自动填充 无法获取时间
protected $_auto = array( array('addTime','time','1','function'), ); addTime在数据库里的的类型必须为int ...