windows环境下java开发连接linux环境的hbase数据获取CURD

单节点集群测试


初始化连接

/**
 *
 * 操作hbase数据库简单查询例子
 * Created by wan on 17-11-7.
 */
public class HbaseTest {

public static Configuration conf; 


    static { 
    conf = HBaseConfiguration.create(); 
    conf.set("hbase.zookeeper.quorum", "172.26.40.122");//必须有
    conf.set("hbase.zookeeper.property.clientPort", "2181");//可有可无
    conf.set("hbase.rootdir", "172.26.40.122:9001");//可有可无
    //conf更多参数配置需要看详细看百度
    System.out.println("连接创建完成!" + conf);
    } 
    


public static void main(String[] args)throws Exception {
   
   
        String tableName = "luffy_1107table01";  
   
        HTable table = new HTable(conf,tableName);


        /**
         * 调用创建表方法 第一个参数:表明;第二个参数:列族的名字(可以设置更多,此处只设置一个参数)
         */
        //HbaseTest.createTable(tableName,"name");


        //HbaseTest.deleteTable(tableName);


        /*HbaseTest.putCell(table,"001","name","luffy","chao");
        HbaseTest.putCell(table,"002","name","luffy","chao");
        HbaseTest.putCell(table,"003","name","luffy","chao");*/


        //HbaseTest.getRow(table,"001");


        //HbaseTest.deleteRow(table,"002");


        //ResultScanner rs = HbaseTest.scanAll(table);


        
        ResultScanner rs = HbaseTest.scanRange(table,"001","003");
        for(Result r:rs) {
            System.out.println("Scan: "+r);
        }


        table.close();
    }




    /**
     * 返回指定row行数的数据,包头不包尾.
     * @param table
     * @param startrow
     * @param endrow
     * @return
     * @throws Exception
     */
    public static ResultScanner scanRange(HTable table,String startrow,String endrow) throws Exception {
        Scan s =new Scan(Bytes.toBytes(startrow),Bytes.toBytes(endrow));
        ResultScanner rs = table.getScanner(s);
        return rs;
    }


    /**
     * 返回指定table名字的所有数据
     * @param table
     * @return
     * @throws Exception
     */
    public static ResultScanner scanAll(HTable table) throws Exception {
            Scan s =new Scan();
            ResultScanner rs = table.getScanner(s);
            return rs;
    }




    /**
     * 根据标识删除hbase中的数据
     * @param table
     * @param rowKey
     * @throws Exception
     */
    public static void deleteRow(HTable table, String rowKey) throws Exception {
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
        System.out.println("Delete row: "+rowKey);
    }


    /**
     * 根据行标识获取hbase中的数据
     * @param table
     * @param rowKey
     * @return
     * @throws Exception
     */
    public static Result getRow(HTable table, String rowKey) throws Exception {
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        System.out.println("Get: "+result);
        return result;
    }




    /**
     * 增加数据
     * @param table
     * @param rowKey  行数唯一标识
     * @param columnFamily 列族
     * @param identifier 列名
     * @param data 值
     * @throws Exception
     */
    public static void putCell(HTable table, String rowKey, String columnFamily, String identifier, String data) throws Exception{
        Put p1 = new Put(Bytes.toBytes(rowKey));
        p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(identifier), Bytes.toBytes(data));
        table.put(p1);
        System.out.println("put '"+rowKey+"', '"+columnFamily+":"+identifier+"', '"+data+"'");
    }




    /**
     * 删除表
     * @param tablename 表名
     * @return
     * @throws Exception
     */
    public static boolean deleteTable(String tablename) throws Exception {
        HBaseAdmin admin = new HBaseAdmin(conf);
        if(admin.tableExists(tablename)) {
            try {
                admin.disableTable(tablename);
                admin.deleteTable(tablename);
                System.out.print("delete table success.");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                admin.close();
                return false;
            }
        }
        admin.close();
        return true;
    }


    /**
     * 创建table:table name :表名
     * @param tablename
     * @param columnFamily
     * @throws Exception
     */
    public static void createTable(String tablename, String columnFamily) throws Exception {


        //获取hbase管理
        HBaseAdmin admin = new HBaseAdmin(conf);
        //判断表是否存在
        if(admin.tableExists(tablename)) {
            System.out.println("Table exists!");
            System.exit(0);
        }else {
            //创建表
            HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
            tableDesc.addFamily(new HColumnDescriptor(columnFamily));
            admin.createTable(tableDesc);
            System.out.println("create table success!");
        }
        //关闭
        admin.close();
    }

}


//如果连接成功之后,会报异常连接失败

需要做主机映射

修改windows下面的hosts文件

增加

172.26.40.122 luffy

修改linux环境下面的/etc/hosts

注释127.0.0.1 luffy

增加

172.26.40.122 luffy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shaw_Bigdata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值