大佬教程收集整理的这篇文章主要介绍了IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.3.5</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.3.5</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.8</version> <scope>system</scope> <systemPath>C:/Program Files/Java/jdk1.8.0_261/lib/tools.jar</systemPath> </dependency>
package com.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.bytes; import org.Stringtemplate.v4.ST; import scala.util.control.Exception; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ConfigulationHBase { private static Configuration configuration; private static Connection connection; private static Admin admin; static { configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "192.168.161.141"); configuration.set("hbase.zookeeper.property.clientPort", "2181"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException E) { e.printStackTrace(); } } /** * 判斷表是否存在 * <br>存在則返回true * @param tablename * @return * @throws IOException */ public static Boolean isTableExist(String tableName) throws IOException { Boolean b = admin.tableExists(Tablename.valueOf(tableName)); return b; } /** * 創建表 * * 参数tablename为表的名称,字符串数组fields为存储记录各个域名称的数组。<br> * 要求当HBase已经存在名为tablename的表时,先删除原有的表,然后再<br> * 创建新的表 field:列族<br> * @param tablename 表名 * @param fields 列族名 * @throws IOException */ public static void createTable(String tableName,String[] fields) throws IOException { if(isTableExist(tableName)){ System.out.println(tablename + " table is alreadly exist..."); admin.disableTable(Tablename.valueOf(tableName)); admin.deleteTable(Tablename.valueOf(tableName)); System.out.println(tablename + " table is deleted..."); } HTableDescriptor hTableDescriptor = new HTableDescriptor(Tablename.valueOf(tableName)); for(String str:fields){ HcolumnDescriptor hcolumnDescriptor = new HcolumnDescriptor(str); hTableDescriptor.addFamily(hcolumnDescriptor); } admin.createTable(hTableDescriptor); System.out.println(tablename + " table is created!"); admin.close(); } /** * 添加数据 * * 向表tablename,行键rowKey和fields字段指定的单元格中添加对应的值values<br> * 例如:表名:student,行键:1001,添加的字段:info:name,添加的值:JAnna<br> * put 'student','1001','info:name','JAnna'<br> * * @param tablename 表名 * @param rowKey 行键 * @param family 列族 * @param qualifier 列族值 * @param value 值 * @throws IOException */ public static void addRow(String tableName, String rowKey, String family, String qualifier, String value) throws IOException { Table table = connection.getTable(Tablename.valueOf(tableName)); byte[] rowKeyAsBytes = rowKey.getBytes(); Put put = new Put(rowKeyAsBytes); put.addcolumn(family.getBytes(), qualifier.getBytes(), value.getBytes()); table.put(put); table.close(); admin.close(); } /** * 删除一行或者多行数据 * @param tablename * @param rows * @throws IOException */ public static void deleteMultiRow(String tableName, String... rows) throws IOException { Table table = connection.getTable(Tablename.valueOf(tableName)); List<delete> deleteList = new ArrayList<delete>(); for (int i = 0; i < rows.length; i++) { delete delete = new delete(rows[i].getBytes()); deleteList.add(Delete); } table.delete(deleteList); table.close(); admin.close(); } /** * * 查询某tablename所有的数据 * @param tablename 表名 * @throws IOException */ public static void getAllRows(String tableName) throws IOException { Table table = connection.getTable(Tablename.valueOf(tableName)); Scan scan = new Scan(); ResultScAnner scAnner = table.getScAnner(scan); for (Result result:scAnner){ Cell[] cells = result.rawCells(); for (Cell cell:cells){ System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("--------------------------------------"); } System.out.println("======================================="); } table.close(); } /** * 查询tablename表的rowKey行键的数据 * get 'student','1001' * @param tablename 表名 * @param rowKey 行键 */ public static void getRow(String tableName, String rowKey) throws IOException { Table table = connection.getTable(Tablename.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); Result result = table.get(get); for (Cell cell:result.rawCells()){ System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("--------------------------------------"); } } /** * 获取某表某行键某列族的值 * * @param tablename 表名 * @param rowKey 行键 * @param famliy 列族 * @param qualifier 列族值 * @throws IOException */ public static void getRowQualifier(String tableName, String rowKey, String famliy, String qualifier) throws IOException { Table table = connection.getTable(Tablename.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); get.addcolumn(famliy.getBytes(), qualifier.getBytes()); Result result = table.get(get); for (Cell cell:result.rawCells()){ System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("timestamp:" + cell.gettimestamp()); System.out.println("--------------------------------------"); } } /** * main 程序入口 * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // 創建表 // String[] fileds = {"base_info","other_info"}; // createTable("football", fileds); // 添加数据 // addRow("football", "11002", "base_info", "name", "lin"); // addRow("football", "11002", "base_info", "sex", "female"); // addRow("football", "11003", "base_info", "sex", "male"); // 删除数据 // deleteMultiRow("football", new String[]{"11002"}); // 得到所有的数据 // getAllRows("student"); // 查询某表某行键数据 // getRow("student","1001"); // 查询某表某行键某列族数据 getRowQualifier("student", "1001", "info", "name"); } }
有什么想法,评论区留言,互相指教指教。
以上是大佬教程为你收集整理的IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI全部内容,希望文章能够帮你解决IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。