hbase搭建,java hbase 例子,java操作hbase例子

 2023-11-11 阅读 23 评论 0

摘要:java操作hbase例子java操作hbase,在eclipse中創建一個java項目,將hbase安裝文件根目錄的jar包和lib目錄下jar包導入項目,然后就可以編寫java代碼操作hbase了。下面代碼給出來一個簡單的示例/*** @date 2015-07-23 21:28:10hbase搭建?* @author

java操作hbase例子

java操作hbase,在eclipse中創建一個java項目,將hbase安裝文件根目錄的jar包和lib目錄下jar包導入項目,然后就可以編寫java代碼操作hbase了。下面代碼給出來一個簡單的示例

/**

* @date 2015-07-23 21:28:10

hbase搭建?* @author sgl

*/

package com.songguoliang.hbase;

import java.io.IOException;

import java.util.ArrayList;

java項目實例、import java.util.List;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

java小程序例子、import org.apache.hadoop.hbase.KeyValue;

import org.apache.hadoop.hbase.client.Delete;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HBaseAdmin;

import org.apache.hadoop.hbase.client.HTable;

java接口的實例。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.util.Bytes;

java代碼例子。public class HBaseTest {

//聲明靜態配置,HBaseConfiguration

private static Configuration conf=null;

static{

conf=HBaseConfiguration.create();

java案例,conf.set(hbase.zookeeper.quorum, sdw1,sdw2);

}

/**

* 創建一個表

* @date 2015-07-23 21:44:32

hbase使用、* @author sgl

* @param tableName 表名

* @param columnFamilys 列族

* @throws IOException

*/

java查詢hbase、public static void createTable(String tableName,String[]columnFamilys) throws IOException{

HBaseAdmin admin =new HBaseAdmin(conf);

if (admin.tableExists(tableName)) {

System.out.println(table already exists!);

}else{

hbase不用java、HTableDescriptor tableDesc=new HTableDescriptor(tableName);

for(int i=0;ilist=new ArrayList();

Delete delete=new Delete(rowKey.getBytes());

list.add(delete);

table.delete(list);

java繼承簡單例子,System.out.println(delete record + rowKey+ success!);

}

/**

* 獲取一行記錄

* @date 2015-07-23 22:21:33

javaapi批量寫入hbase。* @author sgl

* @param tableName 表名

* @param rowKey 行健

* @throws IOException

*/

java連接hbase數據庫、public static void getOneRecord(String tableName,String rowKey) throws IOException{

HTable table=new HTable(conf, tableName);

Get get=new Get(rowKey.getBytes());

Result rs=table.get(get);

for(KeyValue kv:rs.raw()){

java hadoop,System.out.print(new String(kv.getRow()) + );

System.out.print(new String(kv.getFamily()) + : );

System.out.print(new String(kv.getQualifier()) + );

System.out.print(kv.getTimestamp() + );

System.out.println(new String(kv.getValue()));

}

}

/**

* 獲取所有數據

* @date 2015-07-23 22:26:19

* @author sgl

* @param tableName 表名

* @throws IOException

*/

public static void getAllRecord(String tableName) throws IOException{

HTable table=new HTable(conf, tableName);

Scan scan=new Scan();

ResultScanner scanner=table.getScanner(scan);

for(Result result:scanner){

for(KeyValue kv:result.raw()){

System.out.print(new String(kv.getRow()) + );

System.out.print(new String(kv.getFamily()) + :);

System.out.print(new String(kv.getQualifier()) + );

System.out.print(kv.getTimestamp() + );

System.out.println(new String(kv.getValue()));

}

}

}

/**

* 刪除一個表

* @date 2015-07-23 22:29:35

* @author sgl

* @param tableName 表名

* @throws IOException

*/

public static void deleteTable(String tableName) throws IOException{

HBaseAdmin admin=new HBaseAdmin(conf);

admin.disableTable(tableName);

admin.deleteTable(tableName);

System.out.println(delete table +tableName+ success!);

}

public static void main(String[] args) {

try {

String tableName=scores;

String[]columnFamilys={grade,course};

HBaseTest.createTable(tableName, columnFamilys);

HBaseTest.addRecord(tableName, sgl, grade, , 5);

HBaseTest.addRecord(tableName, sgl, course, , 90);

HBaseTest.addRecord(tableName, sgl, course, math, 97);

HBaseTest.addRecord(tableName, sgl, course, art, 87);

HBaseTest.addRecord(tableName, guoguo, grade, , 4);

HBaseTest.addRecord(tableName, guoguo, course, math, 89);

System.out.println(********get one record*********);

HBaseTest.getOneRecord(tableName, sgl);

System.out.println(********get all record*********);

HBaseTest.getAllRecord(tableName);

System.out.println(********delete one record*********);

HBaseTest.deleteRecord(tableName, guoguo);

HBaseTest.getAllRecord(tableName);

System.out.println(********delete table*********);

HBaseTest.deleteTable(tableName);

} catch (IOException e) {

e.printStackTrace();

}

}

}

http://www.dengb.com/Javabc/1037705.htmlwww.dengb.comtruehttp://www.dengb.com/Javabc/1037705.htmlTechArticlejava操作hbase例子 java操作hbase,在eclipse中創建一個java項目,將hbase安裝文件根目錄的jar包和lib目錄下jar包導入項目,然后就可以編寫java代碼...

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/170468.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息