欢迎投稿

今日深度:

HBase使用Put插入数据,hbaseput插入

HBase使用Put插入数据,hbaseput插入


HBase使用Put插入数据

package com.hbase;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

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

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

import org.apache.hadoop.hbase.util.Bytes;

public class HBasePutTest {

// 加载hbase的配置

private static Configuration config = null;

static {

config = HBaseConfiguration.create();

}

插入单条数据到表

// 插入单条数据到表

public static void putOne(String tableName, String rowKey, String cloumn, String qualifier, String value)

throws IOException {

HTable table = new HTable(config, tableName);

Put put = new Put(Bytes.toBytes(rowKey));

put.add(Bytes.toBytes(cloumn), Bytes.toBytes(qualifier), Bytes.toBytes(value));

table.put(put);

}

使用列表向HBase中添加数据

// 使用列表向HBase中添加数据

public static void mulPut() throws IOException {

HTable table = new HTable(config, "testtable");

// 创建一个列表用于存放Put实例

List<Put> puts = new ArrayList<Put>();

// 将第一个Put实例添加到列表

Put put1 = new Put(Bytes.toBytes("row1"));

put1.add(Bytes.toBytes("cf1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));

puts.add(put1);

// 将第2个Put实例添加到列表

Put put2 = new Put(Bytes.toBytes("row2"));

put1.add(Bytes.toBytes("cf1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));

puts.add(put2);

// 将第3个Put实例添加到列表

Put put3 = new Put(Bytes.toBytes("row3"));

put1.add(Bytes.toBytes("cf1"), Bytes.toBytes("qual3"), Bytes.toBytes("val3"));

// 向HBase中存入多行多列数据

puts.add(put3);

}

原子性操作 compare-and-set

// 原子性操作 compare-and-set

public static void match() throws IOException {

HTable table = new HTable(config, "testtable");

// 创建一个新的Put实例

Put put1 = new Put(Bytes.toBytes("row1"));

put1.add(Bytes.toBytes("cf1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));

// 检查一个新的列是否存在,按检查的结果决定是否执行put操作

boolean res1 = table.checkAndPut(Bytes.toBytes("row1"), Bytes.toBytes("cf1"), Bytes.toBytes("qual1"), null,

put1);

System.out.println("Put applied:" + res1);

// 再次想同一单元格写数据

boolean res2 = table.checkAndPut(Bytes.toBytes("row1"), Bytes.toBytes("cf1"), Bytes.toBytes("qual1"), null,

put1);

System.out.println("Put applied:" + res2);

Put put2 = new Put(Bytes.toBytes("row1"));

put1.add(Bytes.toBytes("cf1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));

boolean res3 = table.checkAndPut(Bytes.toBytes("row1"), Bytes.toBytes("cf1"), Bytes.toBytes("qual2"),

Bytes.toBytes("val2"), put2);

System.out.println("Put applied:" + res3);

Put put3 = new Put(Bytes.toBytes("row2"));

put1.add(Bytes.toBytes("cf1"), Bytes.toBytes("qual1"), Bytes.toBytes("val3"));

// 在此处抛异常

boolean res4 = table.checkAndPut(Bytes.toBytes("row1"), Bytes.toBytes("cf1"), Bytes.toBytes("qual1"),

Bytes.toBytes("val1"), put3);

System.out.println("Put applied:" + res4);

}

public static void main(String[] args) throws IOException {

// HBasePutTest.putOne("testtable", "row1", "cf1", "qual2", "val2");

// HBasePutTest.putOne("testtable", "row1", "cf1", "qual3", "val3");

// HBasePutTest.mulPut();

// System.out.println("ok!");

HBasePutTest.match();

}

}

执行的结果应该是:

INFO [main-SendThread(slave2:2181)] zookeeper.ClientCnxn: Session establishment complete on server slave2/192.168.3.109:2181, sessionid = 0x3564f568174000a, negotiated timeout = 40000

Put applied:false

Put applied:false

Put applied:true

Exception in thread "main" org.apache.hadoop.hbase.DoNotRetryIOException: org.apache.hadoop.hbase.DoNotRetryIOException: Action's getRow must match the passed row

在res4处抛异常:

Exception in thread "main" org.apache.hadoop.hbase.DoNotRetryIOException: org.apache.hadoop.hbase.DoNotRetryIOException: Action's getRow must match the passed row

特别说明:原子性操作 compare-and-set

该Put方法比较特殊,能保住自身的原子性:检查写(check and put)。主要保证服务端put操作的原子性。检查通过就put,否则就放弃操作。这种方法用于需要检查现有相关值,并决定是否修改数据的操作。经常用于账户结余、状态转换或数据处理等场景。这些场景的共同点是,在读取数据的同时需要处理数据。一旦你想把一个处理好的结果写回HBase中,并保证没有其他的客户端做了同样的操作,你就可以使用这个有原子性保障的操作,先对原值进行比较,再做修改。当检查与修改针对不同行的数据时就会抛出异常。在分布式系统用得很多。

www.htsjk.Com true http://www.htsjk.com/hbase/29195.html NewsArticle HBase使用Put插入数据,hbaseput插入 HBase使用Put插入数据 package com.hbase; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.h...
相关文章
    暂无相关文章
评论暂时关闭