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("刪除成功");
        
    }
}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM