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. PHP 把秒数转为时分秒格式

    PHP函数 1.gmdate $seconds = 174940;$hours = intval($seconds/); $time1 = $hours."小时".gmdate(' ...

  2. 错误:pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

    pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', ...

  3. C#泛型应用及原理

    https://blog.csdn.net/ananlele_/article/details/97623254 https://blog.csdn.net/kebi007/article/detai ...

  4. C++-POJ2503-Babelfish[hash]

    哈个希加挂个链表 一个要背的字符串hash函数ELFhash() mod数取数据最大容量的1.5倍最佳?! #include <set> #include <map> #inc ...

  5. 【译】写个好的 CLI 程序

    写个好的 CLI 程序 Write a Good CLI Program 译文 原文链接:https://qiita.com/tigercosmos/items/678f39b1209e60843cc ...

  6. 在Linux系统上安装Git

    Git是目前流行的非常好用的版本控制工具,这里介绍两种安装方式,1.yum安装,2.从github上下载最新的源码编译后安装 一.yum安装 1.在Linux上是有yum安装Git,非常简单,只需要一 ...

  7. Pacemaker+ISCSI实现Apache高可用-配置

    一.配置文件系统 任意节点用ISCSI的共享磁盘创建LVM node1 pvcreate /dev/sdb vgcreate my_vg /dev/sdb lvcreate -L 1G -n web_ ...

  8. js函数防抖和函数节流

    参考链接:https://juejin.im/post/5b651dc15188251aa30c8669 参考链接:https://www.jb51.net/article/158818.htm 在我 ...

  9. Eqaulize Prices

    There are n products in the shop. The price of the ii-th product is aiai. The owner of the shop want ...

  10. 传奇服务端添加双倍经验卷的方法 双倍经验卷轴DB示例展示

    第一步我们在DBC数据库中添加好双倍经验卷轴DB,以下是现成的双倍经验卷DB,导入到DB里面就可以了. 222;双倍经验卷;31;0;1;20;0;0;265;0;0;0;0;0;0;0;0;0;0; ...