Hbase java API 的方法

使用java   API方法   对hbase的操作

创建hbase表  添加数据  批量添加  查询  扫描数据

注:必须导入依赖的jar

 

 

1.使用API创建表

package com.bw.hbase;

import java.io.IOException;

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.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HbaseOp {
//创建表 
    public static void main(String[] args) throws Exception, ZooKeeperConnectionException, IOException {
        Configuration conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux04:2181,linux05:2181,linux06:2181");
        HBaseAdmin admin = new HBaseAdmin(conf);//有一个表的管理对象 DDL
        HTableDescriptor hd = new HTableDescriptor("Nurse");//对hbase表的描述
        HColumnDescriptor hcd = new HColumnDescriptor("info");//对hbase列族的描述
        hcd.setMaxVersions(3);
        HColumnDescriptor hcd1 = new HColumnDescriptor("address");
        hcd1.setMaxVersions(3);
        hd.addFamily(hcd1); 
        hd.addFamily(hcd);
        //判断
        if(!admin.tableExists("Nurse")) {
            admin.createTable(hd);
        }else {
            admin.deleteTable("Nurse");
            admin.createTable(hd);
        }
        System.out.println("create table finished......");
    }
}

 

 2.put   添加数据  

package com.bw.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;

public class AddData {
    public static void main(String[] args) throws Exception {
        //创建连接
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux04,linux05,linux06");
        HTable table = new HTable(conf, "Nurse");//配置添加的数据的表
        
        Put put = new Put("nurse001".getBytes());//行键
        put.add("info".getBytes(),"age".getBytes(),"30".getBytes());//列簇= 列= 数据
        put.add("info".getBytes(),"salary".getBytes(),"15000".getBytes());
        put.add("address".getBytes(),"city".getBytes(),"bj".getBytes());
        
        table.put(put);//将数据放入
        System.out.println("add data");
    }
}

注:去集群中查看  是否添加成功  

添加之前将集群都启动

扫描表 scan 'Nurse'

 

3.Batch批量添加  循环添加 

package com.bw.hbase;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;

public class BatchAdd {
    public static void main(String[] args) throws Exception {
        //创建和zk的连接
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux04,linux05,linux06");
        
        HTable table = new HTable(conf,"Nurse");
        List<Put> list = new ArrayList<Put>();
        for(int i=1;i<100000;i++) {
            Put put = new Put(("Nurse0000"+i).getBytes());//创建行键
            put.add("info".getBytes(),"name".getBytes(),("No"+i).getBytes());//将列族 列和数据放入
            list.add(put);//将数据行键放入集合
            if(i%1000==0) {//如果...
                table.put(list);//就将表放入集合
                list.clear();
            }
        }
        table.put(list);
        System.out.println("batch 批量");
    }
}

去集群查看   scan 'Nurse'

 

4.getData 根据行键得到结果 

package com.bw.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;

public class GetData {
    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux04,linux05,linux06");
        HTable table = new HTable(conf,"Nurse");//将查询的表名放入
        Get get = new Get("Nurse000099982".getBytes());//根据行键查询
        Result result = table.get(get);//根据行键得到结果
        byte[] value = result.getValue("info".getBytes(), "name".getBytes());
        System.out.println("查询结果 列族 和列信息:"+new String(value));//打印查询结果
        String row = new String(result.getRow());//得到行键信息
        System.out.println("行键:"+row);
    }
}

 

 5.Scan扫描数据 注: scan不加参数扫描的是所有数据  加参数是 扫描的中间的

package com.bw.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;

public class ScanData {
    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux04,linux05,linux06");
        
        HTable table = new HTable(conf,"Nurse");
        //Scan sc = new Scan();//如果参数是空的 扫描所有的数据  
        Scan sc = new Scan("Nurse00000".getBytes(),"Nurse0000100".getBytes());
        ResultScanner scanner = table.getScanner(sc);
        for (Result r : scanner) {
            System.out.println("行键row:"+new String(r.getRow()));//行键
            System.out.println("查询数据的结果:"+new String(r.getValue("info".getBytes(), "name".getBytes())));
        }
    }
}
注:是按照字典排序:1 11 111 2 22 223

 

6.delete

 

package com.bw.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;


public class DeleteTest {
    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux04,linux05,linux06");
        HTable table = new HTable(conf,"Nurse");
        Delete delete = new Delete("Nurse00001".getBytes());//根据行键删除   删除行键的东西
        table.delete(delete);
        System.out.println("删除成功");
        
    }
}

 

 

 

posted @ 2019-05-18 09:20  lilixia  阅读(1028)  评论(0编辑  收藏  举报