日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Hbase之protobuf的使用

發(fā)布時(shí)間:2024/9/30 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hbase之protobuf的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • 開發(fā)環(huán)境
  • 步驟
    • 下載并安裝protobuf
    • 編寫 `user.proto`
    • 使用Protobuf編譯器將該文件編譯成目標(biāo)語言
    • 測(cè)試
  • 測(cè)試項(xiàng)目地址

開發(fā)環(huán)境

  • Hbase-2.2.7集群
  • Hadoop-3.1.1集群
  • Zookeeper-3.5.6集群
  • protobuf-2.5.0

開發(fā)環(huán)境的配置見前幾篇文章, 軟件下載可自行百度,或者關(guān)注后端碼匠回復(fù)電腦環(huán)境下載

步驟

下載并安裝protobuf

解壓 protobuf-2.5.0

進(jìn)入protobuf-2.5.0

./configur

make

make install

安裝編譯時(shí)間較長(zhǎng),耐心等待??

編寫 user.proto

package cn.com.codingce.hbase;message UserDetail {required string username = 1;required string password = 2; }//這是一個(gè)類,該類對(duì)象中包含很多UserDetail對(duì)象,每個(gè)UserDetail 對(duì)象是一條記錄,dayUserDetail類的對(duì)象其實(shí)就相當(dāng)于Java的集合。 message dayUserDetail {required UserDetail dayUserDetail=1; //屬性名稱就是類名,相當(dāng)于該類是一個(gè)集合存放的泛型是UserDetail (寫法和Java不同,注意理解即可) }

需要注意的是文件名不能和類名一樣

使用Protobuf編譯器將該文件編譯成目標(biāo)語言

執(zhí)行編譯

./protoc --java_out=/root/ ./user.proto

生成成功

測(cè)試

業(yè)務(wù)代碼

package cn.com.codingce.hbase;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ZooKeeperConnectionException; 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.Put; import org.apache.hadoop.hbase.client.Table;import java.io.IOException;/*** @author williamma*/ public class ProtoTest {public static void main(String[] args) throws IOException {// createTable("usertest", "info");addRowData("usertest", "1006");}//獲取 Configuration 對(duì)象public static Configuration conf;static {//使用 HBaseConfiguration 的單例方法實(shí)例化conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "你的ip");conf.set("hbase.zookeeper.property.clientPort", "2181");}/*** 獲取連接** @return* @throws IOException*/public static Connection getConnection() throws IOException {Connection connection = ConnectionFactory.createConnection(conf);System.out.println("創(chuàng)建連接。。。" + connection);return connection;}/*** 向表中插入數(shù)據(jù)** @param tableName* @param rowKey* @throws IOException*/public static void addRowData(String tableName,String rowKey) throws IOException {Connection connection = getConnection();//查詢數(shù)據(jù)//獲取指定表對(duì)象Table table = connection.getTable(TableName.valueOf(tableName));User.UserDetail.Builder userDetail = User.UserDetail.newBuilder(); // userDetail.setUsername("Mike"); // userDetail.setPassword("Mike");userDetail.setUsername("HEHE");userDetail.setPassword("HEHE");Put put = new Put(rowKey.getBytes());put.addColumn("info".getBytes(), "userDetail".getBytes(), userDetail.build().toByteArray());table.put(put);System.out.println("插入成功。。。");}/*** 創(chuàng)建表** @param tableName* @param columnFamily* @throws MasterNotRunningException* @throws ZooKeeperConnectionException* @throws IOException*/public static void createTable(String tableName, String... columnFamily) throwsMasterNotRunningException, ZooKeeperConnectionException, IOException {Connection connection = getConnection();Admin admin = connection.getAdmin();//判斷表是否存在if (isTableExist(tableName)) {System.out.println("表 " + tableName + "已存在");//System.exit(0);} else {//創(chuàng)建表//創(chuàng)建表屬性對(duì)象,表名需要轉(zhuǎn)字節(jié)HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));//創(chuàng)建多個(gè)列族for (String cf : columnFamily) {descriptor.addFamily(new HColumnDescriptor(cf));}//根據(jù)對(duì)表的配置,創(chuàng)建表admin.createTable(descriptor);System.out.println("表 " + tableName + "創(chuàng)建成功!");}}/*** 判斷表是否存在** @param tableName* @return* @throws MasterNotRunningException* @throws ZooKeeperConnectionException* @throws IOException*/public static boolean isTableExist(String tableName) throws MasterNotRunningException,ZooKeeperConnectionException, IOException {//在 HBase 中管理、訪問表需要先創(chuàng)建 HBaseAdmin 對(duì)象//Connection connection = ConnectionFactory.createConnection(conf);// HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();Connection connection = getConnection();Admin admin = connection.getAdmin();boolean result = admin.tableExists(TableName.valueOf(tableName));System.out.println("表是否存在:" + result);return result;} }

測(cè)試項(xiàng)目地址

[Github]:https://github.com/xzMhehe/codingce-java

[Gitee]:https://gitee.com/codingce/codingce-java

如有問題歡迎評(píng)論區(qū)討論

總結(jié)

以上是生活随笔為你收集整理的Hbase之protobuf的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。