标签:基本 hbase String org API import new HBase getBytes
创建表
package com.zyd.api; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.junit.Before; import org.junit.Test; public class HBaseApi { //创建连接的时候比较慢,可以设置一个连接池 private HBaseAdmin admin = null; private HTable table = null; private String TABLE_NAME = "hbaseApi"; //初始化 @Before public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ Configuration conf = new Configuration(); //连接zookeeper,因为zookeeper知道HMaster地址 第一个是在hbase-site.xml文件中 conf.set("hbase.zookeeper.quorum","note01,note02,note03"); admin = new HBaseAdmin(conf); table = new HTable(conf, TableName.valueOf(TABLE_NAME)); } @Test public void createTable() throws IOException{ HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME)); //列族 HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("cf1"); HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("cf2"); //设置最大版本号,默认是1 hColumnDescriptor1.setMaxVersions(3); // 设置生命周期 默认单位为秒 // hColumnDescriptor1.setTimeToLive(0); //add 表示可以创建多个 hTableDescriptor.addFamily(hColumnDescriptor1); hTableDescriptor.addFamily(hColumnDescriptor2); admin.createTable(hTableDescriptor); } }
结果通过主页http://note03:60010/master-status 访问tables
添加rowkey column value
//添加数据 @Test public void put() throws RetriesExhaustedWithDetailsException, InterruptedIOException{ //rowkey Put put = new Put("1000".getBytes()); //列族 列名 value put.add("cf1".getBytes(),"name".getBytes(), "zs".getBytes()); put.add("cf1".getBytes(),"age".getBytes(), "18".getBytes()); // table.put(put); Put put2 = new Put("100".getBytes()); //列族 列名 value put2.add("cf1".getBytes(),"name".getBytes(), "zs".getBytes()); put2.add("cf1".getBytes(),"age".getBytes(), "18".getBytes()); Listlist = new ArrayList(); list.add(put); list.add(put2); //也可以是批量的插入 table.put(list); }
查询
//查询数据 @Test public void get() throws IOException{ //获取行 Get get = new Get("100".getBytes()); Result result = table.get(get); byte[] value = result.getValue("cf1".getBytes(), "name".getBytes()); Cell cell = result.getColumnLatestCell("cf1".getBytes(),"name".getBytes()); //可以得到列族名以及其他的信息等 byte [] cloneFamily = CellUtil.cloneFamily(cell); //hbase里面全是字节 自己要知道类型 String str = new String(value); System.out.println(str); System.out.println("第二种方式:"+new String(cloneFamily)); //动态添加列时,获取类名方法和value ListlistCells = result.listCells(); for (Cell cell2 : listCells) { System.out.println("列名(column)"+new String(CellUtil.cloneQualifier(cell2))+" value" + ":"+new String(CellUtil.cloneValue(cell2))); } }
扫描
//扫描 @Test public void scan() throws IOException{ Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { ListlisCells = result.listCells(); for (Cell cell : lisCells) { System.out.println("column:"+new String(CellUtil.cloneQualifier(cell))+" value" + new String(CellUtil.cloneValue(cell))); } } }
出现小bug column 打印的是地址值,
解决:new String()括号位置不正确
标签:基本,hbase,String,org,API,import,new,HBase,getBytes 来源: https://blog.51cto.com/bigdata/2855170
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。