首页 / JAVA / Hbase的Java操作
Hbase的Java操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Hbase的Java操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4561字,纯文字阅读大概需要7分钟。
内容图文
![Hbase的Java操作](/upload/InfoBanner/zyjiaocheng/845/5b0a9ef3dbe54657a209788ce663927e.jpg)
HBase Java API 1 导入pom依赖 <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> 2 添加配置文件 在resource目录下创建hbase-site.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hbase.zookeeper.quorum</name> <value>zk01,zk02,zk03</value> <description>The directory shared by region servers. </description> </property> </configuration> 3 连接Hbase // hbase的两种连接方式:1)读取配置文件 只需要配置zookeeper Configuration config = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(config); //2)通过代码配置 Configuration configuration = new Configuration(); configuration.set("hbase.zookeeper.quorum", "zk01:2181,zk02:2181,zk03:2181"); connection = ConnectionFactory.createConnection(); 4 创建表 public static void main(String[] args) throws IOException { // 1.连接HBase // 1.1 HBaseConfiguration.create(); Configuration config = HBaseConfiguration.create(); // 1.2 创建一个连接 Connection connection = ConnectionFactory.createConnection(config); // 1.3 从连接中获得一个Admin对象 Admin admin = connection.getAdmin(); // 2.创建表 // 2.1 判断表是否存在 TableName tableName = TableName.valueOf("user"); if (!admin.tableExists(tableName)) { //2.2 如果表不存在就创建一个表 HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); hTableDescriptor.addFamily(new HColumnDescriptor("base_info")); admin.createTable(hTableDescriptor); System.out.println("创建表"); } } 5 打印表的信息 @Before public void initConnection() { try { connection = ConnectionFactory.createConnection(config); } catch (IOException e) { System.out.println("连接数据库失败"); } } ? @Test public void tableInfo() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.获取表的描述信息 HTableDescriptor tableDescriptor = table.getTableDescriptor(); // 4.获取表的列簇信息 HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies(); for (HColumnDescriptor columnFamily : columnFamilies) { // 5.获取表的columFamily的字节数组 byte[] name = columnFamily.getName(); // 6.使用hbase自带的bytes工具类转成string String value = Bytes.toString(name); // 7.打印 System.out.println(value); } } 6 添加数据(PUT) @Before public void initConnection() { try { connection = ConnectionFactory.createConnection(config); } catch (IOException e) { System.out.println("连接数据库失败"); } } @Test public void put() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.准备数据 String rowKey = "rowkey_10"; Put zhangsan = new Put(Bytes.toBytes(rowKey)); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("张三")); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("sex"), Bytes.toBytes("1")); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("address"), Bytes.toBytes("北京市")); zhangsan.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("birthday"), Bytes.toBytes("2014-07-10")); // 4. 添加数据 table.put(zhangsan); table.close(); } 7 获取数据(Get) @Test public void get() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.准备数据 String rowKey = "rowkey_10"; // 4.拼装查询条件 Get get = new Get(Bytes.toBytes(rowKey)); // 5.查询数据 Result result = table.get(get); // 6.打印数据 获取所有的单元格 List<Cell> cells = result.listCells(); for (Cell cell : cells) { // 打印rowkey,family,qualifier,value System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "==> " + Bytes.toString(CellUtil.cloneFamily(cell)) + "{" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ":" + Bytes.toString(CellUtil.cloneValue(cell)) + "}"); } } 8 全表扫描(scan 慎用) @Test public void scan() throws IOException { // 1.定义表的名称 TableName tableName = TableName.valueOf("user"); // 2.获取表 Table table = connection.getTable(tableName); // 3.全表扫描 Scan scan = new Scan(); // 4.获取扫描结果 ResultScanner scanner = table.getScanner(scan); Result result = null; // 5. 迭代数据 while ((result = scanner.next()) != null) { // 6.打印数据 获取所有的单元格 List<Cell> cells = result.listCells(); for (Cell cell : cells) { // 打印rowkey,family,qualifier,value System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "==> " + Bytes.toString(CellUtil.cloneFamily(cell)) + "{" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ":" + Bytes.toString(CellUtil.cloneValue(cell)) + "}"); } } }
内容总结
以上是互联网集市为您收集整理的Hbase的Java操作全部内容,希望文章能够帮你解决Hbase的Java操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】