hadoop学习(5)------java连接hbase

第一步:启动hadoop,启动hbase
第二步,新建Java Project——>新建Class
第三步:在工程中导入Linux中路径/usr/local/hbase/lib下所有的jar包:
链接:https://pan.baidu.com/s/12GjSyFuvTG_lA2ni9xc1DQ
提取码:4zt6
注意:必须新建立项目,不能够在HDFS Java项目的基础上导入hbase的包,会引起包冲突。
1、在hbase中建表

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;

public class MyCreate {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

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

      	//设置虚拟机ip和端口,端口号默认2181
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
        configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");

        //创建连接
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
        System.out.println("start create table");
        
        // 新建表(不能直接将字符串“Score”传入HTableDescriptor,必须将"Score"转化为对象,其实这个我觉得很奇怪,没必要·新建一个对象,有空研究一下)
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("Score"));
        
        // 新建列族
        HColumnDescriptor hColumnDescriptorSname = new HColumnDescriptor("sname");
        hTableDescriptor.addFamily(hColumnDescriptorSname);
        
        // 新建列族
        HColumnDescriptor hColumnDescriptorCourse = new HColumnDescriptor("course");
        hTableDescriptor.addFamily(hColumnDescriptorCourse);
        
        // 建表
        admin.createTable(hTableDescriptor);

        // 关闭连接
        admin.close();
        connection.close();
        System.out.println("ok");
    }
}

2、删除表格

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;

public class MyDeleteTable {
	public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    
	public static void main(String[] args) throws IOException {
		//输入要删除的表格名字·
		String tableName = "Score";
		
		Configuration configuration = HBaseConfiguration.create();
        configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
        configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
        System.out.println("start delete table " + tableName);
        
        //创建tableName这个对象,对ta禁用,然后删除
        TableName tablename = TableName.valueOf(tableName);
        admin.disableTable(tablename);
        admin.deleteTable(tablename);
        
        admin.close();
        connection.close();
        System.out.println("ok");
	}

}

删除某一行:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;

public class MyDeleteData {
	public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    
	public static void main(String[] args) throws IOException {
		//设置表和行数
		String tableName = "Score";
	    String rowkey = "95001";
	    
		Configuration configuration = HBaseConfiguration.create();
        configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
        configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
        System.out.println("start delete "+tableName+"'s "+rowkey+" line");
       
        Table table = connection.getTable(TableName.valueOf(tableName));
        //设置要删除的行键
        Delete delete = new Delete(rowkey.getBytes());
        table.delete(delete);
        table.close();
        admin.close();
        connection.close();
        System.out.println("ok");
        

	}

}

3、查询

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;

public class MyGetdata {
	public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		
		String tableName = "Score";
	    String rowkey = "95001";
	    String colFamily ="sname";
	    String col = "";
	    

	    
	    Configuration configuration = HBaseConfiguration.create();
        configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
        configuration.set("hbase.zookeeper.quorum", "192.168.19.128");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();

	    
	    Table table = connection.getTable(TableName.valueOf(tableName));
	    
        System.out.println("start ");
        Get get = new Get(rowkey.getBytes());
        get.addColumn(colFamily.getBytes(), col.getBytes());
        Result result = table.get(get);
        System.out.println(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)) + " ");
        }
        
        admin.close();
        connection.close();
        System.out.println("-------------------------------------------");
        
	}

}

hbase的特性,我们对ta修改,但ta过去的版本依然有备份,因此ta并没有实际意义上的修改,即是说ta不能被修改,而是在基础上增加。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值