個人HBase版本是0.98
首先說明一下,若是用eclipse操做hbase時,若是報Unknown host錯誤,找不到主機,是由於你沒有配IP地址的映射
方法是 找到你的系統盤裏面的C:\Windows\System32\drivers\etc下的hosts文件,打開,增長一個映射
加一個映射java
192.168.52.140 master
話很少說,直接看代碼,註釋很詳細數據庫
- 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
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
import java
.io.IOException;
import java
.util.Arrays;
import java
.util.List;
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.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.util.Bytes;
public class MyHbaseApi {
public static void main(String[] args) {
Admin admin=null
;
Connection con=null
;
try {
//
1.得到配置文件對象
Configuration conf=HBaseConfiguration
.create()
;
//設置配置參數
conf
.set(
"hbase.zookeeper.quorum",
"192.168.52.140")
;
//
2.創建鏈接
con=ConnectionFactory
.createConnection(conf)
;
//
3.得到會話
admin=con
.getAdmin()
;
//System
.out.println(con)
;
//System
.out.println(admin)
;
//
4.操做
//創建數據庫
//建立表名對象
TableName tn=TableName
.valueOf(
"stu")
;
//a.判斷數據庫是否存在
if(admin
.tableExists(tn)){
System
.out.println(
"====> 表存在,刪除表....")
;
//先使表設置爲不可編輯
admin
.disableTable(tn)
;
//刪除表
admin
.deleteTable(tn)
;
System
.out.println(
"表刪除成功.....")
;
}
System
.out.println(
"===>表不存在,建立表......")
;
//建立表結構對象
HTableDescriptor htd=new HTableDescriptor(tn)
;
//建立列族結構對象
HColumnDescriptor hcd1=new HColumnDescriptor(
"fm1")
;
HColumnDescriptor hcd2=new HColumnDescriptor(
"fm2")
;
htd
.addFamily(hcd1)
;
htd
.addFamily(hcd2)
;
//建立表
admin
.createTable(htd)
;
System
.out.println(
"建立表成功...")
;
//向表中插入數據
//a.單個插入
Put put =new Put(Bytes
.toBytes(
"row01"))
;//參數是行健row01
put
.addColumn(Bytes
.toBytes(
"fm1"), Bytes
.toBytes(
"col1"), Bytes
.toBytes(
"value01"))
;
//得到表對象
Table table=con
.getTable(tn)
;
table
.put(put)
;
//批量插入
Put put01 =new Put(Bytes
.toBytes(
"row02"))
;//參數是行健row02
put01
.addColumn(Bytes
.toBytes(
"fm2"), Bytes
.toBytes(
"col2"), Bytes
.toBytes(
"value02")).
addColumn(Bytes
.toBytes(
"fm2"), Bytes
.toBytes(
"col3"), Bytes
.toBytes(
"value03"))
;
Put put02 =new Put(Bytes
.toBytes(
"row03"))
;//參數是行健row01
put02
.addColumn(Bytes
.toBytes(
"fm1"), Bytes
.toBytes(
"col4"), Bytes
.toBytes(
"value04"))
;
List<Put> puts=Arrays
.asList(put01,put02)
;
//得到表對象
Table table02=con
.getTable(tn)
;
table02
.put(puts)
;
//讀取操做
//scan
Scan scan=new Scan()
;
//得到表對象
Table table03=con
.getTable(tn)
;
//獲得掃描的結果集
ResultScanner rs=table03
.getScanner(scan)
;
for(Result result:rs){
//獲得單元格集合
List<Cell> cs=result
.listCells()
;
for(Cell cell:cs){
//取行健
String rowKey=Bytes
.toString(CellUtil
.cloneRow(cell))
;
//取到時間戳
long timestamp = cell
.getTimestamp()
;
//取到族列
String family = Bytes
.toString(CellUtil
.cloneFamily(cell))
;
//取到修飾名
String qualifier = Bytes
.toString(CellUtil
.cloneQualifier(cell))
;
//取到值
String value = Bytes
.toString(CellUtil
.cloneValue(cell))
;
System
.out.println(
" ===> rowKey : " + rowKey +
", timestamp : " +
timestamp +
", family : " + family +
", qualifier : " + qualifier +
", value : " + value)
;
}
}
System
.out.println(
" ===================get取數據==================")
;
//get
Get get = new Get(Bytes
.toBytes(
"row02"))
;
get
.addColumn(Bytes
.toBytes(
"fm2"), Bytes
.toBytes(
"col2"))
;
Table t04 = con
.getTable(tn)
;
Result r = t04
.get(get)
;
List<Cell> cs = r
.listCells()
;
for (Cell cell : cs) {
String rowKey = Bytes
.toString(CellUtil
.cloneRow(cell))
; //取行鍵
long timestamp = cell
.getTimestamp()
; //取到時間戳
String family = Bytes
.toString(CellUtil
.cloneFamily(cell))
; //取到族列
String qualifier = Bytes
.toString(CellUtil
.cloneQualifier(cell))
; //取到修飾名
String value = Bytes
.toString(CellUtil
.cloneValue(cell))
; //取到值
System
.out.println(
" ===> rowKey : " + rowKey +
", timestamp : " +
timestamp +
", family : " + family +
", qualifier : " + qualifier +
", value : " + value)
;
}
//刪除數據
System
.out.println(
" ===================delete刪除數據==================")
;
Delete delete = new Delete(Bytes
.toBytes(
"row02"))
;
delete
.addColumn(Bytes
.toBytes(
"fm2"), Bytes
.toBytes(
"col2"))
;
Table t05 = con
.getTable(tn)
;
t05
.delete(delete)
;
System
.out.println(
" ===================delete刪除數據後==================")
;
//scan
scan = new Scan()
;
table03 = con
.getTable(tn)
; //得到表對象
rs = table03
.getScanner(scan)
;
for (Result result : rs) {
cs = result
.listCells()
;
for (Cell cell : cs) {
String rowKey = Bytes
.toString(CellUtil
.cloneRow(cell))
; //取行鍵
long timestamp = cell
.getTimestamp()
; //取到時間戳
String family = Bytes
.toString(CellUtil
.cloneFamily(cell))
; //取到族列
String qualifier = Bytes
.toString(CellUtil
.cloneQualifier(cell))
; //取到修飾名
String value = Bytes
.toString(CellUtil
.cloneValue(cell))
; //取到值
System
.out.println(
" ===> rowKey : " + rowKey +
", timestamp : " +
timestamp +
", family : " + family +
", qualifier : " + qualifier +
", value : " + value)
;
}
}
} catch (IOException e) {
e
.printStackTrace()
;
}
//
5.關閉
try {
if (admin != null){
admin
.close()
;
}
if(con != null){
con
.close()
;
}
} catch (IOException e) {
e
.printStackTrace()
;
}
}
}
下面看對於上面的基本操做的封裝,封裝好了,之後就能夠直接用apache
- 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
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
import java
.io
.IOException;
import java
.util
.HashMap;
import java
.util
.List;
import java
.util
.Map;
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
.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
.Table;
import org
.apache
.hadoop
.hbase
.util
.Bytes;
public class HBaseUtil {
private static Configuration conf;
private static Connection con;
// 初始化鏈接
static {
conf
= HBaseConfiguration
.create();
// 得到配製文件對象
conf
.set(
"hbase.zookeeper.quorum",
"192.168.52.140");
try {
con
= ConnectionFactory
.createConnection(conf);
// 得到鏈接對象
} catch (IOException e) {
e
.printStackTrace();
}
}
// 得到鏈接
public static Connection getCon() {
if (con
== null || con
.isClosed()) {
try {
con
= ConnectionFactory
.createConnection(conf);
} catch (IOException e) {
e
.printStackTrace();
}
}
return con;
}
// 關閉鏈接
public static
void close() {
if (con
!= null) {
try {
con
.close();
} catch (IOException e) {
e
.printStackTrace();
}
}
}
// 建立表
public static
void createTable(
String tableName,
String... FamilyColumn) {
TableName tn
= TableName
.valueOf(tableName);
try {
Admin admin
= getCon()
.getAdmin();
HTableDescriptor htd
= new HTableDescriptor(tn);
for (
String fc : FamilyColumn) {
HColumnDescriptor hcd
= new HColumnDescriptor(fc);
htd
.addFamily(hcd);
}
admin
.createTable(htd);
admin
.close();
} catch (IOException e) {
e
.printStackTrace();
}
}
// 刪除表
public static
void dropTable(
String tableName) {
TableName tn
= TableName
.valueOf(tableName);
try {
Admin admin
= con
.getAdmin();
admin
.disableTable(tn);
admin
.deleteTable(tn);
admin
.close();
} catch (IOException e) {
e
.printStackTrace();
}
}
// 插入或者更新數據
public static boolean insert(
String tableName,
String rowKey,
String family,
String qualifier,
String value) {
try {
Table t
= getCon()
.getTable(TableName
.valueOf(tableName));
Put put
= new Put(
Bytes.toBytes(rowKey));
put
.addColumn(
Bytes.toBytes(family),
Bytes.toBytes(qualifier),
Bytes.toBytes(value));
t
.put(put);
return true;
} catch (IOException e) {
e
.printStackTrace();
} finally {
HBaseUtil
.close();
}
return false;
}
// 刪除
public static boolean del(
String tableName,
String rowKey,
String family,
String qualifier) {
try {
Table t
= getCon()
.getTable(TableName
.valueOf(tableName));
Delete del
= new Delete(
Bytes.toBytes(rowKey));
if (qualifier
!= null) {
del
.addColumn(
Bytes.toBytes(family),
Bytes.toBytes(qualifier));
}
else if (family
!= null) {
del
.addFamily(
Bytes.toBytes(family));
}
t
.delete(del);
return true;
} catch (IOException e) {
e
.printStackTrace();
} finally {
HBaseUtil
.close();
}
return false;
}
//刪除一行
public static boolean del(
String tableName,
String rowKey) {
return del(tableName, rowKey,
null,
null);
}
//刪除一行下的一個列族
public static boolean del(
String tableName,
String rowKey,
String family) {
return del(tableName, rowKey, family,
null);
}
// 數據讀取
//取到一個值
public static
String byGet(
String tableName,
String rowKey,
String family,
String qualifier) {
try {
Table t
= getCon()
.getTable(TableName
.valueOf(tableName));
Get get
= new Get(
Bytes.toBytes(rowKey));
get
.addColumn(
Bytes.toBytes(family),
Bytes.toBytes(qualifier));
Result r
= t
.get(get);
return Bytes.toString(CellUtil
.cloneValue(r
.listCells()
.get(
0)));
} catch (IOException e) {
e
.printStackTrace();
}
return null;
}
//取到一個族列的值
public static
Map<String,
String> byGet(
String tableName,
String rowKey,
String family) {
Map<String,
String> result
= null ;
try {
Table t
= getCon()
.getTable(TableName
.valueOf(tableName));
Get get
= new Get(
Bytes.toBytes(rowKey));
get
.addFamily(
Bytes.toBytes(family));
Result r
= t
.get(get);
List<Cell
> cs
= r
.listCells();
result
= cs
.size()
> 0 ? new HashMap
<String,
String>() : result;
for (Cell cell : cs) {
result
.put(
Bytes.toString(CellUtil
.cloneQualifier(cell)),
Bytes.toString(CellUtil
.cloneValue(cell)));
}
} catch (IOException e) {
e
.printStackTrace();
}
return result;
}
//取到多個族列的值
public static
Map<String,
Map<String,
String>> byGet(
String tableName,
String rowKey) {
Map<String,
Map<String,
String>> results
= null ;
try {
Table t
= getCon()
.getTable(TableName
.valueOf(tableName));
Get get
= new Get(
Bytes.toBytes(rowKey));
Result r
= t
.get(get);
List<Cell
> cs
= r
.listCells();
results
= cs
.size()
> 0 ? new HashMap
<String,
Map<String,
String>> () : results;
for (Cell cell : cs) {
String familyName
= Bytes.toString(CellUtil
.cloneFamily(cell));
if (results
.get(familyName)
== null)
{
results
.put(familyName,
new HashMap
<String,
String> ());
}
results
.get(familyName)
.put(
Bytes.toString(CellUtil
.cloneQualifier(cell)),
Bytes.toString(CellUtil
.cloneValue(cell)));
}
} catch (IOException e) {
e
.printStackTrace();
}
return results;
}
}
看看測試類markdown
- 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
package com.yc.hbase.util;
import static org.junit.Assert.*;
import java.util.Map;
import org.junit.Test;
public class HBaseUtilTest {
@Test
public void testCreateTable() {
//建立表
HBaseUtil.createTable(
"myTest",
"myfc1",
"myfc2",
"myfc3");
HBaseUtil.close();
HBaseUtil.createTable(
"myTest02",
"myfc1",
"myfc2",
"myfc3");
HBaseUtil.close();
}
@Test
public void testDropTable() {
//刪除表
HBaseUtil.dropTable(
"myTest");
HBaseUtil.dropTable(
"myTest02");
}
@Test
public void testInsert(){
//插入數據
HBaseUtil.insert(
"myTest",
"1",
"myfc1",
"sex",
"men");
HBaseUtil.insert(
"myTest",
"1",
"myfc1",
"name",
"xiaoming");
HBaseUtil.insert(
"myTest",
"1",
"myfc1",
"age",
"32");
HBaseUtil.insert(
"myTest",
"1",
"myfc2",
"name",
"xiaohong");
HBaseUtil.insert(
"myTest",
"1",
"myfc2",
"sex",
"woman");
HBaseUtil.insert(
"myTest",
"1",
"myfc2",
"age",
"23");
}
@Test
public void testByGet(){
//獲得一行下一個列族下的某列的數據
String result = HBaseUtil.byGet(
"myTest",
"1",
"myfc1",
"name");
System.out.println(
"結果是的: " + result);
assertEquals(
"xiaosan", result);
}
@Test
public void testByGet02(){
//獲得一行下一個列族下的全部列的數據
Map<String, String> result = HBaseUtil.byGet(
"myTest",
"1",
"myfc1");
System.out.println(
"結果是的: " + result);
assertNotNull(result);
}
@Test
public void testByGet03(){
//獲得一行的全部列族的數據
Map<String, Map<String, String>> result = HBaseUtil.byGet(
"myTest",
"1");
System.out.println(
"全部列族的數據是: "+result);
System.out.println(
"結果是的: " + result.get(
"myfc1"));
assertNotNull(result);
}
}
轉載請指明出處http://blog.csdn.net/tanggao1314/article/details/51408166eclipse