HBase_JavaAPI
Posted 勤奋的园
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HBase_JavaAPI相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章