HBaseAPI在eclipse上的建表以及put、get、scan

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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.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;

public class HbaseAPI {

    public static final String TABLE = "users20202005149";
    public static final String FAMILY = "info";
    public static final String COL1 = "name";
    public static final String COL2 = "telephone";
    public static final String COL3 = "email";
    public static final String ROWKEY1 = "003";
    
    
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        System.setProperty("hadoop.home.dir", "D://Download/bigData/hadoop-2.7.3win");
        Configuration conf = HBaseConfiguration.create(); //创建一个环境,没有对环境设置的话,就会自动从hbase-site.xml读取
        Connection connection = ConnectionFactory.createConnection(conf);// 获取连接
        put(connection);
        get(connection);
        scan(connection);
        connection.close();
        
    }
    public static void get(Connection connection) throws IOException {
        // TODO Auto-generated method stub
        System.out.println("get.......");
        Table table = connection.getTable(TableName.valueOf(TABLE));
        Get get = new Get(ROWKEY1.getBytes());  //提供行键
        Result result = table.get(get);  //使用get方法取出来放到result中
        System.out.println(new String(result.getValue(Bytes.toBytes(FAMILY),Bytes.toBytes(COL1)) ) );
        System.out.println(new String(result.getValue(Bytes.toBytes(FAMILY),Bytes.toBytes(COL2)) ) );
        table.close();
    }
    public static void createTable(Connection connection) throws IOException {
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE)); //对表的描述
        tableDescriptor.addFamily(new HColumnDescriptor("ID"));//列族名
        tableDescriptor.addFamily(new HColumnDescriptor(FAMILY));
        Admin admin = connection.getAdmin();
        admin.createTable(tableDescriptor);
        
    }
    
    public static void put(Connection connection) throws IOException {
        Table table = connection.getTable(TableName.valueOf(TABLE)); //和表进行连接
        List<Put> list = new ArrayList<Put>();
        Put putt = new Put(Bytes.toBytes("003"));
        putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL1), Bytes.toBytes("zhangsan"));
        putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL2), Bytes.toBytes("139001"));
        putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL3), Bytes.toBytes("123@qq.com"));
        list.add(putt);

        putt = new Put(Bytes.toBytes("004"));
        putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL1), Bytes.toBytes("lisi"));
        putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL2), Bytes.toBytes("136002"));
        putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL3), Bytes.toBytes("lisi@qq.com"+"lisi@scnu.edu.cn"));
        putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL3), Bytes.toBytes("lisi@scnu.edu.cn"));
        list.add(putt);
        
        table.put(list);
        System.out.println("data putted!");
        table.close();
    }

    
    public static void scan(Connection connection) throws IOException {
        System.out.println("scan...........");
        Table table = connection.getTable(TableName.valueOf(TABLE));
        
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> list = scanner.iterator();
        Result result = null;
        while(list.hasNext()) {
            result = list.next();
            System.out.println(new String(result.getRow())+":"+
            new String(result.getValue(Bytes.toBytes(FAMILY), Bytes.toBytes(COL1))));
            System.out.println(new String(result.getRow())+":"+
            new String(result.getValue(Bytes.toBytes(FAMILY), Bytes.toBytes(COL2))));
        }
        table.close();
    }
}

文件目录

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值