如何为 java 中的 HBase 插入数据生成行名称
how to generate row name for HBase insert data in java
使用 java API 我想在我的 HBase 数据库中插入数据。我需要在插入之前创建一个 put class,像这样:
Put p = new Put(Bytes.toBytes("row1"));
我的程序从用户那里获取数据并放入数据库,但问题是插入需要行名(这里是行 1),但我不知道如何唯一地自动生成它,因为对于每一行它都应该是不同,如果不同,它会覆盖数据。
这是我的代码:
String fName, lName, number ;
Scanner in = new Scanner (System.in) ;
System.out.println("Wainting for taking new phone number info ...\n");
System.out.println("Please Enter first name : ");
fName = in.nextLine() ;
System.out.println("Please Enter last name : ");
lName = in.nextLine() ;
System.out.println("Please Enter number : ");
number = in.nextLine() ;
Configuration config = HBaseConfiguration.create();
HTable hTable = new HTable(config, "phoneList");
Put p = new Put(Bytes.toBytes("row1"));
p.add(Bytes.toBytes("personal"), Bytes.toBytes("firstName")
,Bytes.toBytes(fName));
p.add(Bytes.toBytes("personal"), Bytes.toBytes("lastName")
,Bytes.toBytes(lName));
p.add(Bytes.toBytes("phone"), Bytes.toBytes("number")
,Bytes.toBytes(number));
hTable.put(p);
hTable.close();
in.close();
rowkey
不是行名。实际上它是每一列的行键。当你想在 HBase 中插入或改变一行时,需要提供一个行键。
基于HBase Documentation,increamenting/generating 行键在类似 BigTable 的数据存储中存在问题,不推荐使用。
您可以在下面检查 rowkey 是否存在:
Get get = new Get(rowkey);
Result result = htable.get(get);
if (!result.isEmpty()) {
throw new Exception("Row with this key already exists.");
}
这取决于您希望行键如何。在 HBase 中,由于行键是唯一的索引,当您尝试使用相同的行键并尝试将数据插入到已经存在的列中时,它会尝试覆盖数据。您可以使用与之关联的时间戳来验证它。如果您指定一个新列,数据将不会被覆盖。
一种方法是获取 table 的最后一行并递增该值并将该行插入 table:
Scan bigtableScan = new Scan();
List<Result> getResults = null;
try(ResultScanner bigTableResults = bigtableTable.getScanner(bigtableScan)){
getResults = new ArrayList<>();
for(Result bigTableResult = bigTableResults.next(); (bigTableResult != null);
bigTableResult = bigTableResults.next()) {
getResults.add(bigTableResult);
}
}
导航到结果列表的最后一行。要从结果中获取行,您只需执行以下操作:
for(Result hbaseResult : getResult){
String rowKey = Bytes.toString(hbaseResult.getRow());
}
获得最后一行后,您可以应用自己的逻辑来获取新行并将该行插入 table。
使用 java API 我想在我的 HBase 数据库中插入数据。我需要在插入之前创建一个 put class,像这样:
Put p = new Put(Bytes.toBytes("row1"));
我的程序从用户那里获取数据并放入数据库,但问题是插入需要行名(这里是行 1),但我不知道如何唯一地自动生成它,因为对于每一行它都应该是不同,如果不同,它会覆盖数据。
这是我的代码:
String fName, lName, number ;
Scanner in = new Scanner (System.in) ;
System.out.println("Wainting for taking new phone number info ...\n");
System.out.println("Please Enter first name : ");
fName = in.nextLine() ;
System.out.println("Please Enter last name : ");
lName = in.nextLine() ;
System.out.println("Please Enter number : ");
number = in.nextLine() ;
Configuration config = HBaseConfiguration.create();
HTable hTable = new HTable(config, "phoneList");
Put p = new Put(Bytes.toBytes("row1"));
p.add(Bytes.toBytes("personal"), Bytes.toBytes("firstName")
,Bytes.toBytes(fName));
p.add(Bytes.toBytes("personal"), Bytes.toBytes("lastName")
,Bytes.toBytes(lName));
p.add(Bytes.toBytes("phone"), Bytes.toBytes("number")
,Bytes.toBytes(number));
hTable.put(p);
hTable.close();
in.close();
rowkey
不是行名。实际上它是每一列的行键。当你想在 HBase 中插入或改变一行时,需要提供一个行键。
基于HBase Documentation,increamenting/generating 行键在类似 BigTable 的数据存储中存在问题,不推荐使用。
您可以在下面检查 rowkey 是否存在:
Get get = new Get(rowkey);
Result result = htable.get(get);
if (!result.isEmpty()) {
throw new Exception("Row with this key already exists.");
}
这取决于您希望行键如何。在 HBase 中,由于行键是唯一的索引,当您尝试使用相同的行键并尝试将数据插入到已经存在的列中时,它会尝试覆盖数据。您可以使用与之关联的时间戳来验证它。如果您指定一个新列,数据将不会被覆盖。
一种方法是获取 table 的最后一行并递增该值并将该行插入 table:
Scan bigtableScan = new Scan();
List<Result> getResults = null;
try(ResultScanner bigTableResults = bigtableTable.getScanner(bigtableScan)){
getResults = new ArrayList<>();
for(Result bigTableResult = bigTableResults.next(); (bigTableResult != null);
bigTableResult = bigTableResults.next()) {
getResults.add(bigTableResult);
}
}
导航到结果列表的最后一行。要从结果中获取行,您只需执行以下操作:
for(Result hbaseResult : getResult){
String rowKey = Bytes.toString(hbaseResult.getRow());
}
获得最后一行后,您可以应用自己的逻辑来获取新行并将该行插入 table。