深圳住房与建设局网站,天河区门户信息网,wordpress 付款,代做机械毕业设计网站简介有关HBase的安装可以参考hbase安装我们可以通过hbase shell和hbase数据库进行交互#xff0c;也可以通过Java-api和hbase数据库交互#xff0c;这里我们使用的是hbase-client。主要是介绍2.0重大重构之后的api的基本使用。命名空间#列出所有命名空间list_namespace#新建命…简介有关HBase的安装可以参考hbase安装我们可以通过hbase shell和hbase数据库进行交互也可以通过Java-api和hbase数据库交互这里我们使用的是hbase-client。主要是介绍2.0重大重构之后的api的基本使用。命名空间#列出所有命名空间list_namespace#新建命名空间create_namespace namespaceName#删除命名空间drop_namespace namespaceName#修改命名空间表操作create:创建表exists:检查表是否存在list:查看所有表alter修改表delete删除列disable:禁用表is_enabled查看表是否禁用desc\describe:查看表结构drop:删除表put:插入数据count:统计表有多少行get:获取数据scan:扫描表或者列#列出所有表list#列出指定命名空间下的所有表list_namespace_tables namespaceName#新建一个以命名空间namespaceName的表tableName列族为cf1。create namespaceName:tableName, cf1create tablename,columnFamily1,columnFamily2create tablename,{NAME columnFamily1,VERSIONS 1, TTL 214783647, BLOCKCACHE false,IN_MEMORYfalse},{NAMEcolumnFamily2,VERSIONS1,TTL259200,BLOCKCACHEfalse,IN_MEMORYfalse}disable tableName#删除表drop tableName#查看表内容查看前5行数据scan namespaceName:tableName, {LIMIT5}#插入 put tableName rowkey,columnFamily:column,valueput tableName, rowkey, columnFamilyName:columnName, value#查看 get rowKeyget tableName, rowkey删除表中的列簇先disable表修改后enable表严格区分大小写scan查询user表中的所有信息scan user查询user表中列族为info的信息scan user, {COLUMNS info}scan user, {COLUMNS info, RAW true, VERSIONS 5}查询user表中列族为info和data的信息scan user, {COLUMNS [info, data]}scan user, {COLUMNS [info:name, data:pic]}查询user表中列族为info、列标示符为name的信息scan user, {COLUMNS info:name}查询user表中列族为info、列标示符为name的信息,并且版本最新的5个scan user, {COLUMNS info:name, VERSIONS 5}查询user表中列族为info和data且列标示符中含有a字符的信息scan user, {COLUMNS [info, data], FILTER (QualifierFilter(,substring:a))}查询user表中row key为rk0001cell的值为中国scan user, rk0001, {FILTER ValueFilter(,binary:中国)}查询user表中row key为rk0001列标示符中含有a的信息scan user, rk0001, {FILTER (QualifierFilter(,substring:a))}查询user表中row key以rk字符开头的scan user,{FILTERPrefixFilter(rk)}查询user表中列族为infork范围是[rk0001, rk0003)的数据scan user, {COLUMNS info, STARTROW rk0001, ENDROW rk0003}查询user表中指定范围的数据scan user, {TIMERANGE [1392368783980, 1392380169184]}get命令也可以使用如上所示的scan命令方式基本命令help:查看命令帮助status:查看hbase状态version:查看hbase版本快照修改表名HBase没有rename命令可以通过快照功能修改表名//为表创建快照这时还没有复制数据clone_snapshot tableSnapshot, newTableName//根据某个快照而创建新表此时复制数据snapshot tableName, tableSnapshot//删除快照delete_snapshot tableSnapshotJava APIorg.apache.hbasehbase-client2.2.0import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.NamespaceDescriptor;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.util.LinkedList;import java.util.List;import java.util.Random;public class HBaseTest {private static final String ZOOKEEPER_QUORUM 127.0.0.1:2181;public static final String TABLE_NAME test_tb;private Connection connection;private Configuration configuration;Beforepublic void setUp() throws IOException {configuration HBaseConfiguration.create();configuration.set(hbase.zookeeper.quorum, ZOOKEEPER_QUORUM);connection ConnectionFactory.createConnection(configuration);}Testpublic void createTable() throws Exception {Admin admin connection.getAdmin();TableName tableName TableName.valueOf(TABLE_NAME);TableDescriptorBuilder tableDescriptorBuilder TableDescriptorBuilder.newBuilder(tableName);ColumnFamilyDescriptor baseInfo ColumnFamilyDescriptorBuilder.of(base_info);ColumnFamilyDescriptor extInfo ColumnFamilyDescriptorBuilder.of(ext_info);tableDescriptorBuilder.setColumnFamily(baseInfo);tableDescriptorBuilder.setColumnFamily(extInfo);TableDescriptor tableDescriptor tableDescriptorBuilder.build();admin.createTable(tableDescriptor);}Testpublic void dealTable() throws IOException {Admin admin connection.getAdmin();String tableNameString test_user;TableName tableName TableName.valueOf(tableNameString);if(admin.tableExists(tableName)){admin.disableTable(tableName);admin.deleteTable(tableName);}}Testpublic void descTable() throws IOException {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);TableDescriptor tableDescriptor table.getDescriptor();ColumnFamilyDescriptor[] columnFamilies tableDescriptor.getColumnFamilies();for(ColumnFamilyDescriptor cfd : columnFamilies){System.out.println(Bytes.toString(cfd.getName()));}}Testpublic void put() throws Exception {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);Put putnew Put(Bytes.toBytes(user_info_1));// put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));put.addColumn(Bytes.toBytes(base_info), Bytes.toBytes(name), Bytes.toBytes(tim));put.addColumn(Bytes.toBytes(base_info), Bytes.toBytes(tel), Bytes.toBytes(123));table.put(put);}Testpublic void puts() throws Exception {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);LinkedList puts new LinkedList();puts.add(getPut(user_info_2));puts.add(getPut(user_info_3));puts.add(getPut(user_info_4));table.put(puts);}private static Put getPut(String rowKey){Put putnew Put(Bytes.toBytes(rowKey));Random random new Random();String name String.valueOf(random.nextInt(1000000));put.addColumn(Bytes.toBytes(base_info), Bytes.toBytes(name), Bytes.toBytes(name));put.addColumn(Bytes.toBytes(base_info), Bytes.toBytes(tel), Bytes.toBytes(name));return put;}Testpublic void get() throws Exception {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);Get get new Get(Bytes.toBytes(user_info_1));get.addColumn(Bytes.toBytes(base_info), Bytes.toBytes(name));Result resulttable.get(get);List cells result.listCells();for(Cell cell: cells){System.out.println(Bytes.toString(cell.getFamilyArray()));System.out.println(Bytes.toString(cell.getQualifierArray()));System.out.println(Bytes.toString(cell.getValueArray()));System.out.println(cell.getTimestamp());}}Testpublic void gets() throws IOException {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);LinkedList gets new LinkedList();gets.add(getGet(user_info_1));gets.add(getGet(user_info_2));gets.add(getGet(user_info_3));gets.add(getGet(user_info_4));Result[] results table.get(gets);for(Result result : results){System.out.println(new String(result.getRow()));System.out.println(new String(result.getValue(Bytes.toBytes(base_info), Bytes.toBytes(name))));System.out.println(new String(result.getValue(Bytes.toBytes(base_info), Bytes.toBytes(tel))));}}private static Get getGet(String rowKey){Get get new Get(Bytes.toBytes(rowKey));get.addColumn(Bytes.toBytes(base_info), Bytes.toBytes(name));get.addColumn(Bytes.toBytes(base_info), Bytes.toBytes(tel));return get;}Testpublic void scan() throws IOException {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);Scan s new Scan();ResultScanner resultScanner table.getScanner(s);for(Result result : resultScanner){byte[] row result.getRow();System.out.println(new String(row));System.out.println(new String(result.getValue(Bytes.toBytes(base_info), Bytes.toBytes(name))));}}Testpublic void deleteRowKey() throws Exception {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);Delete de new Delete(Bytes.toBytes(rowKey));table.delete(de);}Testpublic void deleteColumn() throws Exception {TableName tableName TableName.valueOf(TABLE_NAME);Table table connection.getTable(tableName);Delete de new Delete(Bytes.toBytes(rowKey));de.addColumn(Bytes.toBytes(), Bytes.toBytes());table.delete(de);}Testpublic void disableTable() throws Exception {TableName tableName TableName.valueOf(TABLE_NAME);Admin admin connection.getAdmin();admin.disableTable(tableName);}Testpublic void listNamespace() throws Exception {Admin admin connection.getAdmin();NamespaceDescriptor[] namespaceDescriptors admin.listNamespaceDescriptors();for(NamespaceDescriptor namespaceDescriptor : namespaceDescriptors){System.out.println(namespaceDescriptor.getName());}}Testpublic void listTables() throws IOException {Admin admin connection.getAdmin();List tableDescriptors admin.listTableDescriptors();for(TableDescriptor tableDescriptor : tableDescriptors){System.out.println(tableDescriptor.getTableName().getNameAsString());}}}重点就是构建连接Configuration configuration HBaseConfiguration.create();configuration.set(hbase.zookeeper.quorum, 127.0.0.1:2181);Connection connection ConnectionFactory.createConnection(configuration);相关的操作基本和Admin有关通过Connection获取AdminAdmin admin connection.getAdmin();不再使用字符串表明而是使用TableName抽象TableName tableName TableName.valueOf(tableName);表描述构建TableDescriptorBuilder tableDescriptorBuilder TableDescriptorBuilder.newBuilder(tableName);列簇构建ColumnFamilyDescriptor baseInfo ColumnFamilyDescriptorBuilder.of(base_info);下面的createTable基本把表相关的抽象都使用到了Testpublic void createTable() throws Exception {Admin admin connection.getAdmin();TableName tableName TableName.valueOf(TABLE_NAME);TableDescriptorBuilder tableDescriptorBuilder TableDescriptorBuilder.newBuilder(tableName);ColumnFamilyDescriptor baseInfo ColumnFamilyDescriptorBuilder.of(base_info);ColumnFamilyDescriptor extInfo ColumnFamilyDescriptorBuilder.of(ext_info);tableDescriptorBuilder.setColumnFamily(baseInfo);tableDescriptorBuilder.setColumnFamily(extInfo);TableDescriptor tableDescriptor tableDescriptorBuilder.build();admin.createTable(tableDescriptor);}