简介:
本篇博客是hbase api java代码的相关代码,主要包括ddl部分的命名空间创建,表的创建,表的修改,表的删除,dml部分包含数据的插入,数据的修改,数据的查询,数据的删除 *** 作。
1、对pom.xml进行如下配置:
4.0.0 org.example hbaseDemo1.0-SNAPSHOT 8 8 org.apache.hbase hbase-server2.0.5 org.glassfish javax.elorg.apache.hbase hbase-client2.0.5 org.glassfish javax.el3.0.1-b06 org.slf4j slf4j-log4j121.7.30
2、在画框的目录下添加画框的文件
然后输入以下内容:
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%nHbase ddl API代码
package com.lqs.hbase.ddl; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HbaseConfiguration; import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.NamespaceExistException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class OfficialCreateHbaseConnectDemo { public static void main(String[] args) { System.out.println(connection); } //声明静态属性 public static Connection connection; //初始化单列链接 static { try { //获取配置类 Configuration configuration = HbaseConfiguration.create(); //给配置类添加配置 configuration.set("hbase.zookeeper.quorum", "bdc112,bdc113,bdc114"); //创建链接 connection = ConnectionFactory.createConnection(configuration); } catch (IOException e) { e.printStackTrace(); } } public static void close() { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } public static boolean createNameSpace(String nameSpace) { //获取admin //返回数据标志 boolean flag = false; try { Admin admin = connection.getAdmin(); //创建descriptor(描述符) NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build(); //创建命名空间 admin.createNamespace(namespaceDescriptor); //创建成功 flag = true; admin.close(); } catch (NamespaceExistException e) { //捕获名称存在错误 System.out.println("命名空间已经存在!"); } catch (IOException e) { e.printStackTrace(); } return flag; } public static boolean isTableExist(String nameSpace, String tableName) { boolean b = false; try { //获取admin Admin admin = connection.getAdmin(); //判断表是否存在 b = admin.tableExists(TableName.valueOf(nameSpace, tableName)); admin.close(); } catch (IOException e) { e.printStackTrace(); } return b; } public static boolean createTable(String nameSpace, String tableName, String... familyNames) { //判断是否存在列族信息 if (familyNames.length <= 0) { System.out.println("列族信息不存在,请设置列族信息..."); return false; } //判断表格是否存在 if (isTableExist(nameSpace, tableName)) { System.out.println("该表格已经存在"); return false; } try { //获取admin Admin admin = connection.getAdmin(); //获取descriptor的builder TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(nameSpace, tableName)); //添加列族 for (String familyName : familyNames) { //获取单个列族descriptor ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(familyName)); //添加版本 columnFamilyDescriptorBuilder.setMaxVersions(2); //将单个列族的descriptor添加到builder中 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build()); } //创建表 admin.createTable(tableDescriptorBuilder.build()); admin.close(); } catch (IOException e) { e.printStackTrace(); } return true; } public static boolean modifyTable(String nameSpace, String tableName, String familyName, int version) { try { //获取admin Admin admin = connection.getAdmin(); //获取原来的描述 TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(nameSpace, tableName)); //获取原来描述的builder TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor); //在原先的builder中修改对应的列族 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(familyName)).setMaxVersions(version); //传递列族描述 tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build()); //传递表格描述 admin.modifyTable(tableDescriptorBuilder.build()); admin.close(); } catch (IOException e) { e.printStackTrace(); } return true; } public static boolean deleteTable(String nameSpace, String tableName) { //判断表格是否存在 if (!isTableExist(nameSpace, tableName)) { System.out.println("删除失败!表格不存在..."); return false; } try { //获取admin Admin admin = connection.getAdmin(); //标记表格为disable(禁用表) admin.disableTable(TableName.valueOf(nameSpace, tableName)); //删除表格 admin.deleteTable(TableName.valueOf(nameSpace, tableName)); admin.close(); } catch (IOException e) { e.printStackTrace(); } return true; } }Hbase dml API代码
package com.lqs.hbase.dml; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HbaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class HbaseDMLUtil { private static Connection connection; //获取连接 static { try { //获取配置类 Configuration configuration = HbaseConfiguration.create(); //给配置类添加参数 configuration.set("hbase.zookeeper.quorum", "bdc112,bdc113,bdc114"); //创建连接 connection = ConnectionFactory.createConnection(configuration); } catch (IOException e) { e.printStackTrace(); } } public static void putCell(String nameSpace, String tableName, String rowKey, String family, String column, String value) { try { //获取table Table table = connection.getTable(TableName.valueOf(nameSpace, tableName)); //创建put对象 Put put = new Put(Bytes.toBytes(rowKey)); //添加put属性 put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value)); //插入(put)数据 table.put(put); table.close(); } catch (IOException e) { e.printStackTrace(); } } public static String getCell(String nameSpace, String tableName, String rowKey, String family, String column) { String value = ""; //获取table try { //获取table Table table = connection.getTable(TableName.valueOf(nameSpace, tableName)); //获取get对象 Get get = new Get(Bytes.toBytes(rowKey)); //添加get属性 get.addColumn(Bytes.toBytes(family), Bytes.toBytes(column)); //获取(get)数据 //简单获取 // byte[] value = table.get(get).value(); // String s = new String(value); //复杂获取 //获取结果(result Result result = table.get(get); //h获取cells Cell[] cells = result.rawCells(); //遍历cells结果集 for (Cell cell : cells) { //输出每个cell value += Bytes.toString(CellUtil.clonevalue(cell)) + "-"; } table.close(); } catch (IOException e) { e.printStackTrace(); } return value; } public static ListscanRows(String nameSpace, String tableName, String startRow, String stopRow) { ArrayList arrayList = new ArrayList<>(); try { //获取table Table table = connection.getTable(TableName.valueOf(nameSpace, tableName)); //创建扫描对象 Scan scan = new Scan().withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow)); //扫描数据 ResultScanner scanner = table.getScanner(scan); //获取扫描得到的结果 for (Result result : scanner) { arrayList.add(Bytes.toString(result.value())); } scanner.close(); table.close(); } catch (IOException e) { e.printStackTrace(); } return arrayList; } public static void deleteColumn(String nameSpace, String tableName, String rowKey, String family, String column) { try { Table table = connection.getTable(TableName.valueOf(nameSpace, tableName)); //创建delete对象 Delete delete = new Delete(Bytes.toBytes(rowKey)); //添加删除信息 //删除单个版本 // delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column)); //删除所有版本 delete.addColumn(Bytes.toBytes(family), Bytes.toBytes(column)); //删除列族 // delete.addFamily(Bytes.toBytes(family)); //删除数据 table.delete(delete); table.close(); } catch (IOException e) { e.printStackTrace(); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)