首页 > 技术文章 > java对Hbase的基本操作

wwzyy 2018-03-18 13:35 原文

 

 1.新建一个普通java项目,把${hbase}/lib/目录下的jar包全部导入

 

2.导出jar文件如下

3.运行

注意:需要先把jar文件导入到hbase路径里去,然后运行相应的类

 

4.查看数据库

 

 附加:

package com.wzy.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseApp {
    public static void main(String[] args) {
        switch (args[0]) {
        case "create":
            new HbaseApp().createTable();
            break;
        case "put":
            new HbaseApp().put();
            break;
        case "get":
            new HbaseApp().get();

        default:
            System.out.println("enter true args");
        }
        
    }
    public void createTable(){
        try{
            Configuration conf = HBaseConfiguration.create();
            HBaseAdmin admin = new HBaseAdmin(conf);
            //表的名字
            TableName tablename = TableName.valueOf("test3");
            HTableDescriptor tdesc = new HTableDescriptor(tablename);
            //列族的名字
            HColumnDescriptor hcd = new HColumnDescriptor("data");
            tdesc.addFamily(hcd);
            admin.createTable(tdesc);
            System.out.println("create table over ! !");
        }catch(Exception e){
            
        }
        
    }
    @SuppressWarnings("deprecation")
    public void put(){
        try{
            HTable table = new HTable(HBaseConfiguration.create(),"test3");
            Put put = new Put(Bytes.toBytes("row1"));
            put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("1"), Bytes.toBytes("value01"));
            put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("2"), Bytes.toBytes("value02"));
            put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("3"), Bytes.toBytes("value03"));
            table.put(put);
            table.close();
            System.out.println("put data over ! !");
        }catch(Exception e){
            
        }
        
    }
    @SuppressWarnings("deprecation")
    public void get(){
        try{
            HTable table = new HTable(HBaseConfiguration.create(), "test3");
            Get get = new Get(Bytes.toBytes("row1"));
            get.addColumn(Bytes.toBytes("data"), Bytes.toBytes("1"));
            get.addColumn(Bytes.toBytes("data"), Bytes.toBytes("2"));
            
            Result r = table.get(get);
            KeyValue kv = r.getColumnLatest(Bytes.toBytes("data"), Bytes.toBytes("1"));
            System.out.println("data:1.key "+ Bytes.toString(kv.getKey()));
            System.out.println("data:1.value "+ Bytes.toString(kv.getValue()));
            table.close();
            System.out.println("get over !");
        }catch(Exception e){
            
        }
        
    }
    
    public void scan(){
        try{
            HTable table = new HTable(HBaseConfiguration.create(), "test3");
            Scan scan = new Scan();
            ResultScanner rs = table.getScanner(scan);
            for(Result r : rs){
                System.out.println(Bytes.toString(r.getRow())+" : "+Bytes.toString(r.getColumnLatestCell(Bytes.toBytes("data"), Bytes.toBytes("1")).getValue()));
            }
            rs.close();
            table.close();
        }catch(Exception e){
            
        }
    }
    public void delete(){
        try{
            HTable table = new HTable(HBaseConfiguration.create(), "test3");
            Delete d = new Delete(Bytes.toBytes("row1"));
            table.delete(d);
            table.close();
        }catch(Exception e){
            
        }
    }
    
    public void drop(){
        try{
            Configuration conf = HBaseConfiguration.create();
            HBaseAdmin admin = new HBaseAdmin(conf);
            admin.disableTable("test3");
            admin.deleteTable("test3");
            admin.close();
        }catch(Exception e){
            
        }
    }
    
    
}
CRUD操作

 

 

注:java运行普通的jar文件

1.新建一个普通java项目

2.打包成jar文件

3.运行

 

推荐阅读