激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 使用Java對Hbase操作總結及示例代碼

使用Java對Hbase操作總結及示例代碼

2020-07-22 14:21天ヾ道℡酬勤 Java教程

這篇文章主要介紹了使用Java對Hbase進行操作總結,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前面已經給大家講解過如何使用Hbase建表,以及基本的操作和一些常用shell命令,今天就給大家介紹下如何使用java對Hbase進行各種操作。
沒印象的話可以再去瀏覽下:
Hbase入門教程,shell命令大全講解

Java操作Hbase主要方法:

1.Configuration
在使用Java API時,Client端需要知道HBase的配置環境,如存儲地址,zookeeper等信息。
這些信息通過Configuration對象來封裝,可通過如下代碼構建該對象:

Configuration config = HBaseConfiguration.create();

在調用HBaseConfiguration.create()方法時,HBase首先會在classpath下查找hbase-site.xml文件,將里面的信息解析出來封裝到Configuration對象中,如果hbase-site.xml文件不存在,則使用默認的hbase-core.xml文件。

2.HBaseAdmin
HBaseAdmin用于創建數據庫表格,并管理表格的元數據信息,通過如下方法構建:
HBaseAdmin admin=new HBaseAdmin(config);

3.HTableDescriptor
在HTableDescriptor中,建立了一個表結構,HTableDescriptor封裝表格對象,對表格的增刪改查操作主要通過它來完成,構造方法如下:
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(“表名”));

4.addFamily
addFamily用于建立表下的列簇,并存放到表結構,方法如下:
HColumnDescriptor base = new HColumnDescriptor(“列簇名”);
table.addFamily(base);

代碼如下:
首先建一個maven工程,導入依賴包導pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0</version>
</dependency

1、創建表操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class HBaseClient {
 public void createTable() throws IOException {
 // 1. 創建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
  //hbase主默認端口是60000
 conf.set("hbase.master","ip1:60000");
 //zookeeper客戶端的端口號2181
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 創建連接
 Connection conn = ConnectionFactory.createConnection(conf);
 //3.獲得一個建表、刪表的對象hbaseAdmin()是繼承admin()
 Admin admin = conn.getAdmin();
 // 4. 創建表的描述信息
 HTableDescriptor student = new HTableDescriptor(TableName.valueOf("表名"));
 // 5. 添加列簇
 student.addFamily(new HColumnDescriptor("列簇名1"));
 student.addFamily(new HColumnDescriptor("列簇名2"));
 // 6. 調用API進行建表操作
 admin.createTable(student);
 }
 
}

2、判斷表是否存在

?
1
2
3
4
5
6
7
8
9
10
11
12
public void isTableExists() throws IOException {
 // 1. 創建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 創建連接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 創建admin
 Admin admin = conn.getAdmin();
 // 4. 調用API進行判斷表是否存在
 System.out.println(admin.tableExists(TableName.valueOf("表名")));
 }

3、向表中插入數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void putData2Table() throws IOException {
 // 1. 創建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 創建連接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 創建Table類
 Table student = conn.getTable(TableName.valueOf("表名"));
 // 4. 創建Put類
 Put put = new Put(Bytes.toBytes("1001"));
 // 5. 向Put中添加 列簇,列名,值 注意:需要轉化成字節數組
 put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列1"),Bytes.toBytes("zhangsan"));
 put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列2"),Bytes.toBytes("female"));
 put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列3"),Bytes.toBytes("math"));
 put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列4"),Bytes.toBytes("89"));
 // 6.調用API進行插入數據
 student.put(put);
 }

4、查看一條數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void getDataFromTable() throws IOException {
 // 1. 創建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 創建連接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 創建Table類
 Table student = conn.getTable(TableName.valueOf("表名"));
 // 4. 創建 Get 類
 Get get = new Get(Bytes.toBytes("1001"));
 // 5.調用API進行獲取數據
 Result result = student.get(get);
 // 6. 將返回的結果進行遍歷輸出
 Cell[] cells = result.rawCells();
 for (Cell cell : cells) {
  System.out.println("rowkey :"+Bytes.toString(CellUtil.cloneRow(cell)));
  System.out.println("列簇 :"+Bytes.toString(CellUtil.cloneFamily(cell)));
  System.out.println("列名 :"+Bytes.toString(CellUtil.cloneQualifier(cell)));
  System.out.println("值 :"+Bytes.toString(CellUtil.cloneValue(cell)));
  System.out.println("----------------");
 }
 }

5、刪除表操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void dropTable() throws IOException {
 // 1. 創建配置
 Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","ip1");
 conf.set("hbase.zookeeper.property.clientPort","2181");
 // 2. 創建連接
 Connection conn = ConnectionFactory.createConnection(conf);
 // 3. 創建admin
 Admin admin = conn.getAdmin();
 // 4. 調用API禁用表
 admin.disableTable(TableName.valueOf("表名"));
 // 5. 調用API刪除表
 admin.deleteTable(TableName.valueOf("表名"));
 }
}

6、刪除hbase中的table里面的rowkey

?
1
2
3
4
5
6
7
public static void deleteRow(String tableName,String rowKey) throws Exception{
 HTable hTable = new HTable(configuration,tableName);
 Delete delete = new Delete(rowKey.getBytes());
 List<Delete> list = new ArrayList<Delete>();
 list.add(delete);
 hTable.delete(list);
 }

7、查詢row = rowKey的數據

?
1
2
3
4
5
6
7
8
public static void getRow(String tableName,String rowKey) throws Exception{
 HTable hTable = new HTable(configuration, tableName);
 Get get = new Get(rowKey.getBytes());
 Result result = hTable.get(get);
 for(KeyValue value:result.raw()){
  System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));
 }
 }

8、查詢rowkey在startRow和endRow之間的數據,及rowkey的范圍查詢
Put、Delete與Get對象都是Row的子類,從該繼承關系中我們就可以了解到Get、Delete與Pu對象本身就只能進行單行的操作,
HBase客戶端還提供了一套能夠進行全表掃描的API,方便用戶能夠快速對整張表進行掃描,以獲取想要的結果—scan:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void getBetweenRow(String tableName,String startRow,String stopRow) throws Exception{
 HTable table = new HTable(configuration, tableName);
 Scan scan = new Scan();
 scan.addColumn("cf1".getBytes(), "colum1".getBytes());
 scan.addColumn("cf1".getBytes(), "colum2".getBytes());
 scan.addColumn("cf1".getBytes(), "colum3".getBytes());
 
    scan.setStartRow(startRow.getBytes());
    scan.setStopRow(stopRow.getBytes());
 
    ResultScanner scanner = table.getScanner(scan);
    
    for(Result result:scanner){
     for(KeyValue value:result.raw()){
      System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));
     }
    }
}      

9、批量寫入

?
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
public <T> void puts(String tableName, Map<String, Object> items) {
 if (items == null || items.isEmpty()) {
  LOG.error("[HBase] Adding null/empty item map!");
  return;
 }
 int maxSize = 10000;
 Table table = null;
 try {
  table = con.getTable(TableName.valueOf(tableName));
  int eachSize = Math.min(maxSize, items.size());
  List<Put> puts = new ArrayList<Put>(eachSize);
  int handled = 0;
  
  for (Entry<String, Object> entry : items.entrySet()) {
  String ultimateRowKey = getHashedID(entry.getKey());
  Object value = entry.getValue();
  
  if (ultimateRowKey == null || ultimateRowKey.isEmpty()) {
   LOG.error("[HBase] Adding null/empty hashed key! Original key is " + entry.getKey());
   handled++;
   continue;
  }
    
        Put put = new Put(Bytes.toBytes(ultimateRowKey));
  put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("ab"), Bytes.toBytes(value .getAb()));
  put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("dt"), Bytes.toBytes(value .getDt()));
  put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("hb"), Bytes.toBytes(value .getHb()));
  
  Gson gson = new Gson();
  String valuestr = gson.toJson(value);
  put.addColumn(Bytes.toBytes(familyName2), Bytes.toBytes("js"), Bytes.toBytes(valuestr));
  puts.add(put);
  handled++;
 
        // 每隔10000,寫一次
  if (handled == eachSize) {
   LOG.info("[HBase] Adding " + eachSize + "rows!");
   table.put(puts);
   puts = new ArrayList<Put>(eachSize);
  }
  }
  if (puts.size() > 0)
  table.put(puts);
 } catch (IOException e) {
    LOG.error("[HBase] Error while putting data " + e.getMessage());
 } finally {
  try {
   if (table != null)
   table.close();
  } catch (IOException e) {
  LOG.error("[HBase] Error while closing table " + e.getMessage());
  }
 
    }
}

到此這篇關于使用Java對Hbase操作總結及示例代碼的文章就介紹到這了,更多相關Java操作hbase總結內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/zp17834994071/article/details/107501922

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91在线视频在线观看 | 欧美在线中文字幕 | 精品国产91久久久 | 深夜毛片免费看 | 免费一级在线视频 | 一区二区久久精品66国产精品 | 成人午夜天堂 | 特级西西444www大精品视频免费看 | 国产一级毛片高清视频 | 色柚视频网站ww色 | 在线观看福利网站 | 激情五月少妇a | 国产精品aⅴ | 国产成年人在线观看 | 精品一区二区三区网站 | 久久精品久久精品国产大片 | 亚洲一区二区免费 | 国产成人精品自拍视频 | 日韩欧美电影在线观看 | 人禽l交免费视频观看 视频 | 毛片在线视频免费观看 | 桥本有菜免费av一区二区三区 | 久草高清视频 | 亚洲一区在线免费视频 | 国产在线观看免费视频软件 | 亚洲综合精品 | 国产日韩欧美一区 | 免费的性生活视频 | 久久精品国产精品亚洲 | 一级做a爱片性色毛片高清 日本一区二区在线看 | 毛片免费观看视频 | 一区二区三区视频在线观看 | 成人三级电影网 | 成人黄色小视频网站 | 成年免费视频黄网站在线观看 | 精品国产第一区二区三区 | 欧美成人性色区 | 最近中文字幕一区二区 | 国产成人高潮免费观看精品 | 一级网站 | 成人三级免费电影 |