溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java代碼訪問Hbase測試

發(fā)布時(shí)間:2020-06-27 17:11:06 來源:網(wǎng)絡(luò) 閱讀:591 作者:刀刀_高揚(yáng) 欄目:關(guān)系型數(shù)據(jù)庫

import java.io.IOException;
import java.util.Iterator;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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;
import org.junit.Test;

public class Run {

static Configuration conf = null;

public static Configuration configuration;
static {
conf = HBaseConfiguration.create();
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "hd1,hd2,hd3,hd4");
configuration.set("hbase.master", "hd1:60000");
}


public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
try {
System.out.println("start");

go();
System.out.println("over");
}
catch (Exception e)
{
System.out.println(e.toString());
}
}

public Run() throws IOException {
super();

}

public static void go() throws IOException
{
for (int i1 = 0; i1 <= 3; i1++) {
for (int i2 = 0; i2 <= 3; i2++) {
for (int i3 = 0; i3 <= 3; i3++) {
for (int i4 = 0; i4 <= 3; i4++) {
System.out.println(Integer.toString(i1*1000+i2*100+i3*10+i4));
addData(i1, i2, i3, i4);
}
}
}
}
}

public static void addData(int i1, int i2, int i3, int i4) throws IOException {
String strTBName = "ttable0";
String strColFamily = "tuser";
String strColumn = "b";//列名
String strRowKey = "";//行號
String strValue = "1";//值

HTable table1 = new HTable(conf, strTBName);
strRowKey = Integer.toString(i1)+Integer.toString(i2)+Integer.toString(i3)+Integer.toString(i4);
Put put = new Put(Bytes.toBytes(strRowKey));// 設(shè)置行號,RowKey

put.add(Bytes.toBytes(strColFamily), Bytes.toBytes(strColumn),
Bytes.toBytes(strValue));

table1.put(put);

table1.close();
}


}

單純這樣寫,別的問題沒有,就是速度超級慢。損失的環(huán)節(jié)主要是建立連接和分配HTable的時(shí)候。 




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI