Hbase之protobuf的使用
生活随笔
收集整理的這篇文章主要介紹了
Hbase之protobuf的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
- 開發(fā)環(huán)境
- 步驟
- 下載并安裝protobuf
- 編寫 `user.proto`
- 使用Protobuf編譯器將該文件編譯成目標語言
- 測試
- 測試項目地址
開發(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
進入protobuf-2.5.0
./configur
make
make install
安裝編譯時間較長,耐心等待??
編寫 user.proto
package cn.com.codingce.hbase;message UserDetail {required string username = 1;required string password = 2; }//這是一個類,該類對象中包含很多UserDetail對象,每個UserDetail 對象是一條記錄,dayUserDetail類的對象其實就相當(dāng)于Java的集合。 message dayUserDetail {required UserDetail dayUserDetail=1; //屬性名稱就是類名,相當(dāng)于該類是一個集合存放的泛型是UserDetail (寫法和Java不同,注意理解即可) }需要注意的是文件名不能和類名一樣
使用Protobuf編譯器將該文件編譯成目標語言
執(zhí)行編譯
./protoc --java_out=/root/ ./user.proto生成成功
測試
業(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 對象public static Configuration conf;static {//使用 HBaseConfiguration 的單例方法實例化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ù)//獲取指定表對象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)建表屬性對象,表名需要轉(zhuǎn)字節(jié)HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));//創(chuàng)建多個列族for (String cf : columnFamily) {descriptor.addFamily(new HColumnDescriptor(cf));}//根據(jù)對表的配置,創(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 對象//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;} }測試項目地址
[Github]:https://github.com/xzMhehe/codingce-java
[Gitee]:https://gitee.com/codingce/codingce-java
如有問題歡迎評論區(qū)討論
總結(jié)
以上是生活随笔為你收集整理的Hbase之protobuf的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac下通过Anaconda安装Tens
- 下一篇: javax.jdo.JDODataSto