汇总代码

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.*;

import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

public class ExampleForHbase{

public static Configuration configuration;

public static Connection connection;

public static Admin admin;

//主函数中的语句请逐句执行,只需删除其前的//即可,如:执行insertRow时请将其他语句注释

public static void main(String[] args)throws IOException{

//  createTable("Student",new String[]{"S_Name","S_Sex","S_Age","C_No","C_Credit","SC_Score"});

//创建一个表,表名为Score,列族为sname,course

//deleteRow("Student", "2015003", "", "");

//在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)

//等价命令:put 'Score','95001','sname','Mary'

/*      insertRow("Student", "2015001", "S_Name", "", "Zhangsan");

insertRow("Student", "2015001", "S_Sex", "", "male");

insertRow("Student", "2015001", "S_Age", "", "23");

insertRow("Student", "2015001", "C_No", "Math", "123001");

insertRow("Student", "2015001", "C_Credit", "Math", "2.0");

insertRow("Student", "2015001", "C_No", "English", "123003");

insertRow("Student", "2015001", "C_Credit", "English", "3.0");

insertRow("Student", "2015001", "SC_Score", "123001", "86");

insertRow("Student", "2015001", "SC_Score", "123003", "69");

insertRow("Student", "2015002", "S_Name", "", "Mary");

insertRow("Student", "2015002", "S_Sex", "", "female");

insertRow("Student", "2015002", "S_Age", "", "22");

insertRow("Student", "2015002", "C_No", "Computer Science", "123002");

insertRow("Student", "2015002", "C_Credit", "Computer Science", "5.0");

insertRow("Student", "2015002", "C_No", "English", "123003");

insertRow("Student", "2015002", "C_Credit", "English", "3.0");

insertRow("Student", "2015002", "SC_Score", "123002", "77");

insertRow("Student", "2015002", "SC_Score", "123003", "99");

insertRow("Student", "2015003", "S_Name", "", "Lisi");

insertRow("Student", "2015003", "S_Sex", "", "male");

insertRow("Student", "2015003", "S_Age", "", "24");

insertRow("Student", "2015003", "C_No", "Computer Science", "123002");

insertRow("Student", "2015003", "C_Credit", "Computer Science", "5.0");

insertRow("Student", "2015003", "C_No", "Math", "123001");

insertRow("Student", "2015003", "C_Credit", "Math", "2.0");

insertRow("Student", "2015003", "SC_Score", "123002", "95");

insertRow("Student", "2015003", "SC_Score", "123001", "98");

*/

//modifyData("Student", "2015001", "S_Name", "Wangwu");

//在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)

//等价命令:put 'Score','95001','score:Math','88'

//insertRow("Score", "95001", "course", "Math", "88");

//在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)

//等价命令:put 'Score','95001','score:English','85'

//insertRow("Score", "95001", "course", "English", "85");

//1、删除Score表中指定列数据,其行键为95001,列族为course,列为Math

//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码取消注释注释,将删除制定列族的代码注释

//等价命令:delete 'Score','95001','score:Math'

//deleteRow("Score", "95001", "course", "Math");

//2、删除Score表中指定列族数据,其行键为95001,列族为course(95001的Math和English的值都会被删除)

//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,将删除制定列族的代码取消注释

//等价命令:delete 'Score','95001','score'

//deleteRow("Score", "95001", "course", "");

//3、删除Score表中指定行数据,其行键为95001

//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,以及将删除制定列族的代码注释

//等价命令:deleteall 'Score','95001'

//  deleteRow("Student", "2015003", "", "");

//查询Score表中,行键为95001,列族为course,列为Math的值

//getData("Score", "95001", "course", "Math");

//查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)

//getData("Student","2015001", "C_Credit", "English");

//删除Score表

// deleteTable("Student");

}

//建立连接

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();

}

}

/**

* 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列

* @param myTableName 表名

* @param colFamily 列族名

* @throws IOException

*/

public static void createTable(String myTableName,String[] colFamily) throws IOException {

init();

TableName tableName = TableName.valueOf(myTableName);

if(admin.tableExists(tableName)){

admin.disableTable(tableName);

admin.deleteTable(tableName);    //删除原来的表

System.out.println("所创建表已存在!");

HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

for(String str:colFamily){

HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);

hTableDescriptor.addFamily(hColumnDescriptor);

}

admin.createTable(hTableDescriptor);

System.out.println("create table success");

}

else {

HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

for(String str:colFamily){

HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);

hTableDescriptor.addFamily(hColumnDescriptor);

}

admin.createTable(hTableDescriptor);

System.out.println("创建成功");

}

close();

}

/**

* 删除指定表

* @param tableName 表名

* @throws IOException

*/

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();

}

/**

* 查看已有表

* @throws IOException

*/

public static void listTables() throws IOException {

init();

HTableDescriptor hTableDescriptors[] = admin.listTables();

for(HTableDescriptor hTableDescriptor :hTableDescriptors){

System.out.println(hTableDescriptor.getNameAsString());

}

close();

}

/**

* 向某一行的某一列插入数据

* @param tableName 表名

* @param rowKey 行键

* @param colFamily 列族名

* @param col 列名(如果其列族下没有子列,此参数可为空)

* @param val 值

* @throws IOException

*/

public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Put put = new Put(rowKey.getBytes());

put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());

table.put(put);

table.close();

System.out.println("插入成功");

close();

}

/**

* 删除数据

* @param tableName 表名

* @param rowKey 行键

* @param colFamily 列族名

* @param col 列名

* @throws IOException

*/

public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Delete delete = new Delete(rowKey.getBytes());

//删除指定列族的所有数据

//delete.addFamily(colFamily.getBytes());

//删除指定列的数据

//delete.addColumn(colFamily.getBytes(), col.getBytes());

table.delete(delete);

table.close();

System.out.println("删除成功");

close();

}

/**

* 根据行键rowkey查找数据

* @param tableName 表名

* @param rowKey 行键

* @param colFamily 列族名

* @param col 列名

* @throws IOException

*/

public static void getData(String tableName,String rowKey,String colFamily,String col)throws  IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Get get = new Get(rowKey.getBytes());

get.addColumn(colFamily.getBytes(),col.getBytes());

Result result = table.get(get);

showCell(result);

table.close();

close();

}

/*

*  修改某一列的数据

*/

public static void modifyData(String tableName,String rowKey,String colFamily,String col,String val)throws IOException{

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Put put = new Put(rowKey.getBytes());

put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());

table.put(put);

table.close();

System.out.println("修改成功");

close();

}

/* public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {

init();

Table table = connection.getTable(TableName.valueOf(tableName));

Put put = new Put(rowKey.getBytes());

put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());

table.put(put);

table.close();

System.out.println("插入成功");

close();

}

*/

/**

* 格式化输出

* @param result

*/

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))+" ");

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容