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. eclipse下载更新可用的SDK 2018-11-12

    懒人方法: mirrors.neusoft.edu.cn:80 操作步骤: 1. Android SDK Manager----Tools----Options-----Http Proxy Serv ...

  2. day22 定时任务

    检查软件是否安装 cronie [root@oldboyedu ~]# rpm -qa cronie cronie-1.4.11-19.el7.x86_64 [root@oldboyedu ~]# r ...

  3. [HTML] websocket的模拟日志监控界面

    模拟命令行的界面效果,使用swoole作为websocket的服务,重新做了下html的界面效果 <html> <head> <title>SwLog Montio ...

  4. numpy特性

    numpy特性 待办 可获取最小值最大值或者排序等操作的索引,然后通过索引取得对应值或者对应值的序列 按行按列求和.按行按列求积 方差等等统计函数使用 enter description here e ...

  5. idea tomcat启动无效

    今天换了台电脑,用idea 部署tomcat启动死活启动不了,报 Application Server was not connected before run configuration stop, ...

  6. How To Use These LED Garden Lights

    Are you considering the lighting options for the outdoor garden? Depending on how you use it, LED ga ...

  7. python项目虚拟环境搭建

    一. 虚拟环境搭建目的 一个项目一个环境,防止各个项目互相干扰,项目更加简洁,利于打包.... 二.使用 pip install virtualenv 安装 创建虚拟环境 cd my_project_ ...

  8. 如何处理python异常

    1.python异常有那些? window的机器如果安装了python,则直接可以在idle中查看,打开idle,按F1即可打开帮助文档,按如下路径即可查看,也可以去python官网查看这里不说明了百 ...

  9. 虚拟机下的Linux连接校园网上网问题

    安装了CentOS7后,本地Windows系统连接用的是校园网,虚拟机用的是桥接模式,无法上网. 解决的办法是:找到连接的校园网的网卡,给虚拟机的VMnet8共享. 步骤:打开[网络和共享中心]-[更 ...

  10. 【译】PHP 内核 — zval 基础结构

    [译]PHP 内核 - zval 基础结构 原文地址:http://www.phpinternalsbook.com/php7/internal_types/zvals/basic_structure ...