码迷,mamicode.com
首页 > 编程语言 > 详细

HBase_JavaAPI

时间:2019-08-22 19:14:50      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:取出   rss   ble   com   exception   hba   scanner   als   admin   

package com.zy.test;

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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

public class HBaseTest {
    
    @Test
    public void createTable() throws IOException{
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "exercise1:2181,exercise2:2181,exercise3:2181");
        //建立连接
        Connection conn=ConnectionFactory.createConnection(conf);
        //获取表管理类
        Admin admin=conn.getAdmin();
        //定义表
        HTableDescriptor hTableDescriptor=new HTableDescriptor(TableName.valueOf("person"));
        //定义列族
        HColumnDescriptor hColumnDescriptor=new HColumnDescriptor("info");
        //将列族添加到表中
        hTableDescriptor.addFamily(hColumnDescriptor);
        //执行建表操作
        admin.createTable(hTableDescriptor);
        admin.close();
        conn.close();
        }
    @Test
    public void put() throws IOException{
    Configuration conf=HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "exercise1:2181,exercise2:2181,exercise3:2181");
    //建立连接
    Connection conn=ConnectionFactory.createConnection(conf);
    //获取表
    Table table=conn.getTable(TableName.valueOf("person"));
    //用行键实例化Put
    Put put=new Put("rk0001".getBytes());
    //指定列族名、列名和值
    put.addColumn("info".getBytes(), "name".getBytes(),"zhangsan".getBytes());
    //执行put操作
    table.put(put);
    //关闭连接
    table.close();
    conn.close();
    }
    @Test
    public void get() throws IOException {
    Configuration conf=HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum","exercise1:2181,exercise2:2181,exercise3:2181");
    //建立连接
    Connection conn=ConnectionFactory.createConnection(conf);
    //获取表
    Table table=conn.getTable(TableName.valueOf("person"));
    //用行键实例化Get
    Get get=new Get("rk0001".getBytes());
    //增加列族名和列名条件
    get.addColumn("info".getBytes(),"name".getBytes());
    //执行,返回结果
    Result result=table.get(get);
    //取出结果
    String valStr=Bytes.toString(result.getValue("info".getBytes(),"name".getBytes()));
    System.out.println(valStr);
    //关闭连接
    table.close();
    conn.close();
    }
    @Test
    public void scan() throws IOException{
    Configuration conf=HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "exercise1:2181,exercise2:2181,exercise3:2181");
    //建立连接
    Connection conn=ConnectionFactory.createConnection(conf);
    //获取表
    Table table=conn.getTable(TableName.valueOf("person"));
    //初始化Scan实例
    Scan scan=new Scan();
    //增加过滤条件
    scan.addColumn("info".getBytes(),"name".getBytes());
    //返回结果
    ResultScanner rss=table.getScanner(scan);
    //迭代并取出结果
    for(Result rs:rss){
    String valStr=Bytes.toString(rs.getValue("info".getBytes(),"name".getBytes()));
    System.out.println(valStr);
    }
    //关闭连接
    table.close();
    conn.close();
    }
    @Test
    public void delete() throws IOException{
    Configuration conf=HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "exercise1:2181,exercise2:2181,exercise3:2181");
    //建立连接
    Connection conn=ConnectionFactory.createConnection(conf);
    //获取表
    Table table=conn.getTable(TableName.valueOf("person"));
    // 用行键来实例化Delete实例
    Delete del = new Delete("rk0001".getBytes());
    // 执行删除
    table.delete(del);
    //关闭连接
    table.close();
    conn.close();
    }
    }

 

HBase_JavaAPI

标签:取出   rss   ble   com   exception   hba   scanner   als   admin   

原文地址:https://www.cnblogs.com/qfdy123/p/11395884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!