Hbase Java API
来源:互联网 发布:手机八格切图软件 编辑:程序博客网 时间:2024/05/01 04:58
JAR包
解压 hbase 安装包,lib文件夹下的所有jar文件,安装文件 点此下载
常用方法
package hbase;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellScanner;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Admin;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.exceptions.DeserializationException;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.util.Bytes;/** * Douguo Hbase Java Client * @author JainfeiZhang * @version 1.0.0 */public class DouguoHbaseJavaClient { // Hbase connection object public static Connection conn; // database metadate connection object public static Admin admin; /** * get hbase connection object * @author JainfeiZhang * @since 1.0.0 */ public static void getConn (){ // get configuration params object Configuration conf = HBaseConfiguration.create(); //set connection params, ip conf.set("hbase.zookeeper.quorum", "192.168.238.200"); //set connection params, port conf.set("hbase.zookeeper.property.clientPort", "2181"); try { //get connetion conn = ConnectionFactory.createConnection(conf); //get admin admin = conn.getAdmin(); } catch (IOException e) { e.printStackTrace(); } if (admin == null){ System.out.println(" Connection failed. Check it please ... "); System.out.println(); } else { System.out.println(" The connection is successful ! ready to perform the operation ... "); System.out.println(); } } /** * 关闭连接 * @throws IOException */ public static void closeConnection() throws IOException{ admin.close(); System.out.println(); System.out.println(" Close connection ... "); } /** * 创建表 * @author JainfeiZhang * @param tableName * @param familyColumnsName * @throws IOException */ public static void createHbaseTable (String tableName, String[] familyColumnsName) throws IOException{ //create table name object TableName tableNameObject = TableName.valueOf(tableName); //judge if the table is existed if (admin.tableExists(tableNameObject)) { System.out.println("Warning : the table is existed !"); } else { // table is not existed, create it // create table description object HTableDescriptor hTableDescriptor = new HTableDescriptor(tableNameObject); // create familyColumns description object for (String familyColumnName : familyColumnsName){ // create object HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(familyColumnName); // add familyColumn to tableDescriptor hTableDescriptor.addFamily(hColumnDescriptor); } // create table admin.createTable(hTableDescriptor); } } /** * 对表增加列族 * @param tableName * @param familyColumnName * @throws IOException */ public static void addFamilyColumn (String tableName, String familyColumnName) throws IOException{ // get table object TableName tableObject = TableName.valueOf(tableName); // create familyColumn object HColumnDescriptor columnDescriptor = new HColumnDescriptor(familyColumnName); // add familyColumn to table admin.addColumn(tableObject, columnDescriptor); } /** * 删除列族 * @param tableName * @param familyColumnName * @throws IOException * @author JainfeiZhang */ public static void deleteFamilyColumn (String tableName, String familyColumnName) throws IOException{ // get table object TableName tableObject = TableName.valueOf(tableName); // delete familyColumn admin.deleteColumn(tableObject, familyColumnName.getBytes()); } /** * 增加数据 * @param tableName * @param familyColumnName * @param qualifierName * @param dataArray * @throws IOException * @author JainfeiZhang */ public static void insertDate (String tableName, String rowKeyName ,String familyColumnName, String qualifierName, String dataArray) throws IOException{ // get table object Table table = conn.getTable(TableName.valueOf(tableName)); // get put data object Put put; // create data put list List<Put> putList = new ArrayList<Put>();// for ( String data : dataArray ){ put = new Put(Bytes.toBytes(rowKeyName)); put.addColumn(familyColumnName.getBytes(), qualifierName.getBytes(), dataArray.getBytes()); putList.add(put);// } // put data in hbase table table.put(putList); } /** * 获取所有的列族名 * @param tableName * @throws IOException */ public static void getfamilyColumnsNames (String tableName) throws IOException{ // get table object Table table = conn.getTable(TableName.valueOf(tableName)); // get all familyColumns HColumnDescriptor[] hColumnDescriptor = table.getTableDescriptor().getColumnFamilies(); for ( HColumnDescriptor hc : hColumnDescriptor ){ String familyColumnName = hc.getNameAsString(); System.out.println(familyColumnName); } } /** * 遍历获取指定表、行键、列族、列的数据 * 结果示例 : mall/infoa:pv/1494196479710/Put/vlen=7/seqid=0 ,rowKey/family:qualifier/timestamp/... * @author JainfeiZhang * @param tableName * @param rowKeyName * @param familyName * @param qualifierName * @throws IOException */ public static void getAllDates (String tableName, String rowKeyName, String familyName, String qualifierName) throws IOException{ // get table object Table table = conn.getTable(TableName.valueOf(tableName)); // 获取rowkey Get get = new Get(Bytes.toBytes(rowKeyName)); // 添加需要查询的列族和列名 get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(qualifierName)); // 获取结果集 Result result = table.get(get); // 遍历结果集 if (result.isEmpty()){ System.out.println(" The result is Empty ! "); } else { // 获取游标 CellScanner cellScanner = result.cellScanner(); // 遍历 while (cellScanner.advance()){ System.out.println("Get cell : " + cellScanner.current() ); } } } /** * 根据行键值按行获取数据 * @param tableName * @param rowKeyName * @throws IOException */ public static void getAllDateByRowKey (String tableName, String rowKeyName, String columnFamilyName) throws IOException { // get hbase table object Table table = conn.getTable(TableName.valueOf(tableName)); // create query object Get get = new Get(Bytes.toBytes(rowKeyName)); // query by rowKey Result result = table.get(get); byte[] row = result.getRow(); System.out.println("row key is:" + new String(row)); for (Cell cell : result.rawCells()){ String rowKey = Bytes.toString (result.getRow()); String family = Bytes.toString(CellUtil.cloneFamily(cell)); String qualifier = Bytes. toString (CellUtil. cloneQualifier (cell)); String value = Bytes. toString (CellUtil. cloneValue (cell)); if (columnFamilyName.equals(columnFamilyName)){ System.out.println( "{\"rowkey\":\"" + rowKey + "\",\"family\":\"" + family + "\",\"qualifier\":\"" + qualifier + "\",\"value\":\"" + value + "\"}"); } } }}
阅读全文
1 0
- HBase之Java API
- HBase之Java API
- HBase 的Java API
- HBase Java API详解
- hbase java api详解
- HBase Java API详解
- Hbase Java API详解
- HBase之Java API
- Hbase Java API详解
- Hbase for Java API
- HBase Java API详解
- HBase Java API详解
- hbase java api操作
- HBase的Java API
- Hbase Java API详解
- hbase java api操作
- HBase Java API详解
- hbase 与 java API
- 【常识】户型图要怎么看?买房时销售员不会说的注意事项
- 使用localstorage.setItem()存储对象
- 基于Echart.js一点总结
- 12槽位PCIe扩展箱
- android视频播放器缓存
- Hbase Java API
- Java核心技術卷_2_注釋與數據類型
- JSON.parse()方法将数据转换为javascript对象
- React-Native 安卓出现图片资源路径正确,却无法加载显示
- JAVA 模拟瞬间高并发
- 外观模式(Facade)
- Android 调用echart图表,实现牛逼的效果
- 数据库第一课
- oc 同步、异步图片下载