HBase put数据丢失的问题
HBase下使用API数据写入,写完之后立刻scan,新写入的数据可用看到。但是当机器重启之后,再进行scan的化,就很不稳定,重启前写入到数据有的时候数据能看到,有的时候就看不到。autoflush和wal选项都设置了。代码如下:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HBaseTestCase;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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 java.util.*;
public class HBaseTest {
//声明静态配置 HBaseConfiguration
static Configuration cfg=HBaseConfiguration.create();
//创建一张表,通过HBaseAdmin HTableDescriptor来创建
public static void creat(String tablename,String columnFamily) throws Exception {
HBaseAdmin admin = new HBaseAdmin(cfg);
if (admin.tableExists(tablename)) {
System.out.println("table Exists!");
//System.exit(0);
}
else{
HTableDescriptor tableDesc = new HTableDescriptor(tablename);
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("create table success!");
}
}
//添加一条数据,通过HTable Put为已经存在的表来添加数据
@SuppressWarnings("deprecation")
public static void put(String tablename,String row, String columnFamily,String column,String data) throws Exception {
HTable table = new HTable(cfg, tablename);
table.setAutoFlush(true);
Put p1=new Put(Bytes.toBytes(row));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
p1.setWriteToWAL(true);
table.put(p1);
System.out.println("put '"+row+"','"+columnFamily+":"+column+"','"+data+"'");
}
public static void get(String tablename,String row) throws IOException{
HTable table=new HTable(cfg,tablename);
Get g=new Get(Bytes.toBytes(row));
Result result=table.get(g);
System.out.println("Get: "+result);
}
//显示所有数据,通过HTable Scan来获取已有表的信息
public static void scan(String tablename) throws Exception{
HTable table = new HTable(cfg, tablename);
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for(Result r:rs){
System.out.println("Scan: "+r);
}
}
public static boolean delete(String tablename) throws IOException{
HBaseAdmin admin=new HBaseAdmin(cfg);
if(admin.tableExists(tablename)){
try
{
admin.disableTable(tablename);
admin.deleteTable(tablename);
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
return true;
}
public static void main (String [] agrs) {
String tablename="table1";
String columnFamily="cf1";
try {
HBaseTest.creat(tablename, columnFamily);
for(int i=700;i<800;i++)
{
String rowID="row"+String.format("%03d", i);
Random r=new Random();
String columnName="";
//col1--name
columnName="Name";
byte[] bytes=new byte[100];
r.nextBytes(bytes);
String name=bytes.toString();
HBaseTest.put(tablename, rowID, columnFamily, columnName, name);
//col2--age
columnName="Age";
int age=r.nextInt(100);
HBaseTest.put(tablename, rowID, columnFamily, columnName, String.valueOf(age));
//col3--height
columnName="Height";
double height=r.nextDouble()*100;
HBaseTest.put(tablename, rowID, columnFamily, columnName, String.format("%.2f", height) );
}
//HBaseTest.put(tablename, "row1", columnFamily, "cl1", "data");
//HBaseTest.get(tablename, "row1");
//HBaseTest.scan(tablename);
/* if(true==HBaseTestCase.delete(tablename))
System.out.println("Delete table:"+tablename+"success!");
*/
}
catch (Exception e) {
e.printStackTrace();
}
}
}
...全文
相关推荐
第7章 HBase组件安装与配置.pptx
* 高可靠性:WAL 机制保证了数据写入时不会因集群异常而导致写入数据丢失,Replication 机制保证了在集群出现严重的问题时,数据不会发生丢失或损坏。
HBase写入优化--write buff
write buff操作 前两天在观察kafka消费数据的时候,发现HBase偶尔会报一个org.apache.hadoop.hbase.RegionTooBusyException: org.apache.hadoop.hbase.RegionTooBusyException这种错误出来,从描述上看,是HBase写入太过频繁导致的。 首先来看我的写入操作代码:/** * 单条更新hb
03侧田HBase迁移方案的设计与实践12.pdf
- **Replication**:使用HBase自带的复制功能来迁移实时增量数据,其优点包括错误重试机制和增量数据的不丢失保证。
HBase性能优化指南
默认情况下,HDFS不会实时同步数据到磁盘,而是写入临时文件后移动到最终位置,导致在断电情况下可能会丢失旧数据。
【No0057】HBase源码解析与开发实战.txt
- 开发者还需要关注如何处理数据丢失等问题。


