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的更多相关文章

随机推荐

  1. SVN merge(合并) 时看不到以前的已经合并过的记录的标识

    今天遇到这么一个事情,merge的时候以前merge过的提交记录,咩有已合并过的标识了,就是下面这样的尾巴分叉向下的箭头 通常出现这样的情况,都是工程路径不对,检查了一下,没有问题,这些meng B ...

  2. t-SNE

    Don't look back. Don't hesitate, just do it. t-SNE原理 from here. 1. tsne is strictly used for visuali ...

  3. 在MyEclipse中修改文件名出现问题

    问题描述:An exception has been caught while processing the refactoring 'Rename Compilation Unit'. 问题原因:项 ...

  4. 使用Vue-MUI轮播图失效问题解决案例(在Vue的update中执行)

    我使用的是mui+vue,社区关于轮播图失效的问题也有几个.我这边遇到的一个情况是我把所有的东西都写到plusReady事件中会导致轮播图搞死都不动,按照其他问答解决了vue生命周期等等的问题.提出来 ...

  5. ASP.NET Core Web API中Startup的使用技巧

    Startup类和服务配置   STARTUP CLASS AND THE SERVICE CONFIGURATION 在 Startup 类中,有两个方法:ConfigureServices 是用于 ...

  6. java面试题错题集(牛客网错题)

    一.关于Object类的说法正确 Java中所有的类都直接或间接继承自Object,无论是否明确的指明,无论其是否是抽象类. Object的equals方法,只有一句话,return this==ob ...

  7. cmd命令和快捷键

    命令 含义 盘名: 从C盘到其他盘时输入 dir 查看当前文件夹下的内容 cd 目录名称 选择目录 cd.. 退回当前目录的上一级目录 cd\ 回到根目录 md 目录名称 创建目录 rd 目录名称 删 ...

  8. 2019-08-01 纪中NOIP模拟B组

    T1 [JZOJ2642] 游戏 题目描述 Alice和Bob在玩一个游戏,游戏是在一个N*N的矩阵上进行的,每个格子上都有一个正整数.当轮到Alice/Bob时,他/她可以选择最后一列或最后一行,并 ...

  9. 第五十四篇 Linux相关——远程连接SSH

        No.1. SSH(Secure Shell)安全外壳协议 建立在应用层基础上的安全协议 可靠,专为远程登录会话和其他网络服务提供安全性的协议 有效防止远程管理过程中的信息泄漏问题 SSH客户 ...

  10. 概念理解_L2范数(欧几里得范数)

    L1范数 L1范数是指向量中各个元素绝对值之和 L2范数 L2范数.欧几里得范数一些概念. 首先,明确一点,常用到的几个概念,含义相同. 欧几里得范数(Euclidean norm) ==欧式长度 = ...