hbase的java编程实例 hbase项目实战
首先 根据 hadoop 搭建 + hbase 搭建把 环境弄好

这里列举下 hbase shell 的常用操作
#最有用命令 help help 'status' #建表 create 'FileTable','fileInfo','saveInfo' #列出有哪些表 list #描述表信息 desc 'FileTable' #统计 表 count 'FileTable' #添加列簇 alter 'FileTable','cf' # 删除列簇 ,一定要注意大小写 alter 'FileTable',{NAME=>'cf',METHOD=>'delete'}插入数据
put 'FileTable','rowkey1','fileInfo:name','file1.txt'
0 row(s) in 3.9840 seconds
put 'FileTable','rowkey1','fileInfo:type','txt'
0 row(s) in 0.0410 seconds
put 'FileTable','rowkey1','fileInfo:size','1024'
0 row(s) in 0.1100 seconds
put 'FileTable','rowkey1','saveInfo:path','/home/pics'
0 row(s) in 0.1320 seconds
put 'FileTable' , 'rowkey1','saveInfo:creator','tom'
0 row(s) in 0.1430 seconds查询表总行数
hbase(main):016:0> count 'FileTable'
1 row(s) in 0.8500 secondsget 查询
get 'FileTable','rowkey1'
get 'FileTable','rowkey1','fileInfo'delete
deleteall
scan
删除表之前必须先禁用表Drop the named table. Table must first be disabled:
disable 'FileTable'
is_enabled
is_disabled
drop 'FileTable'
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
查询所有列簇

查询指定列簇

HBase 连接类
package com.ghc.hbase.api;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;import java.io.IOException;
public class HBaseConn {
private static final HBaseConn INSTANCE = new HBaseConn();
private static Configuration configuration;
private static Connection connection;private HBaseConn(){ try{ if(configuration == null){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.32.129:2181"); } }catch(Exception e){ e.printStackTrace(); } } private Connection getConnection(){ if(connection == null || connection.isClosed()){ try{ connection = ConnectionFactory.createConnection(configuration); }catch(IOException e){ e.printStackTrace(); } } return connection; } public static Connection getHBaseConnection(){ return INSTANCE.getConnection(); } public static Table getTable(String tableName) throws IOException{ return INSTANCE.getConnection().getTable(TableName.valueOf(tableName)); } public static void closeConn(){ if(connection != null){ try{ connection.close(); }catch (IOException e){ e.printStackTrace(); } } }
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
junit 测试一波连接类
import com.ghc.hbase.api.HBaseConn;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.junit.Test;import java.io.IOException;
public class HBaseConnTest {
@Test
public void getConnTest(){
Connection conn = HBaseConn.getHBaseConnection();
System.out.println(conn.isClosed());
HBaseConn.closeConn();
System.out.println(conn.isClosed());
}
@Test
public void getTableTest(){
Table table = null;
try{table = HBaseConn.getTable("FileTable");
System.out.println(table.getName());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
hbase 增删操作类
package com.ghc.hbase.api;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.*;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;public class HBaseUtil {
public static Boolean createTable(String tableName,String[] cfs){ try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){ if(admin.tableExists(tableName)) { return false; } // 不存在 则创建 HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); Arrays.stream(cfs).forEach(cf->{ HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf); hColumnDescriptor.setMaxVersions(1); hTableDescriptor.addFamily(hColumnDescriptor); }); admin.createTable(hTableDescriptor); }catch(Exception e){ e.printStackTrace(); } return true; } public static Boolean putRow(String tableName, String rowKey, String cfName,String qualifier,String data){ try(Table table = HBaseConn.getTable(tableName)){ Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data)); table.put(put); }catch (IOException ioe){ ioe.printStackTrace(); } return true; } public static Boolean putRows(String tableName,List<Put> puts){ try(Table table = HBaseConn.getTable(tableName)){ table.put(puts); }catch(IOException ioe){ ioe.printStackTrace(); } return true; } public static Result getRow(String tableName, String rowKey){ try(Table table = HBaseConn.getTable(tableName)){ Get get = new Get(Bytes.toBytes(rowKey)); return table.get(get); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } public static Result getRow(String tableName, String rowKey, FilterList filterList){ try(Table table = HBaseConn.getTable(tableName)){ Get get = new Get(Bytes.toBytes(rowKey)); get.setFilter(filterList); return table.get(get); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } public static ResultScanner getScanner(String tableName){ try(Table table = HBaseConn.getTable(tableName)){ Scan scan = new Scan(); scan.setCaching(1000); return table.getScanner(scan); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){ try(Table table = HBaseConn.getTable(tableName)){ Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(startRowKey)); scan.setStopRow(Bytes.toBytes(endRowKey)); scan.setCaching(1000); return table.getScanner(scan); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } // 使用过滤器 public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){ try(Table table = HBaseConn.getTable(tableName)){ Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(startRowKey)); scan.setStopRow(Bytes.toBytes(endRowKey)); scan.setCaching(1000); scan.setFilter(filterList); return table.getScanner(scan); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } // 删除 public static Boolean deleteRow(String tableName,String rowKey){ try(Table table = HBaseConn.getTable(tableName)){ Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); return true; }catch(IOException ioe){ioe.printStackTrace();} return null; } // 删除 列簇 用 admin public static Boolean deleteColumnFamily(String tableName,String cfName){ try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){ admin.deleteColumn(tableName,cfName); }catch(IOException ioe){ ioe.printStackTrace(); } return true; } //删除 某列 用 table public static Boolean deleteQualifier(String tableName, String rowKey, String cfName,String qualifier){ try(Table table = HBaseConn.getTable(tableName)){ Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier)); table.delete(delete); return true; }catch(IOException ioe){ ioe.printStackTrace(); } return null; }
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
原文链接:https://blog.51cto.com/u_16099325/6353666

浙公网安备 33010602011771号