Skip to content

Commit

Permalink
to add learn crud api
Browse files Browse the repository at this point in the history
  • Loading branch information
easesstone committed Jun 28, 2017
1 parent bee7ecf commit fd773ba
Show file tree
Hide file tree
Showing 27 changed files with 766 additions and 197 deletions.
54 changes: 27 additions & 27 deletions ch03/src/main/java/client/AppendExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,36 @@
public class AppendExample {

public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();

HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", 100, "colfam1", "colfam2");
helper.put("testtable",
new String[] { "row1" },
new String[] { "colfam1" },
new String[] { "qual1" },
new long[] { 1 },
new String[] { "oldvalue" });
Configuration conf = HBaseConfiguration.create(); //获取Hbase 的集群配置。

HBaseHelper helper = HBaseHelper.getHelper(conf); //通过工具类,获取链接,并且得到Admin 对象。
helper.dropTable("testtable"); // 删除表格
helper.createTable("testtable", 100, "colfam1", "colfam2");// 创建表格,表格有两个列族
helper.put("testtable", // 表明
new String[] { "row1" }, // Row key
new String[] { "colfam1" }, // 列族
new String[] { "qual1" }, // K
new long[] { 1 }, // Time,Version
new String[] { "oldvalue" }); // values
System.out.println("Before append call...");
helper.dump("testtable", new String[]{ "row1" }, null, null);

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));

// vv AppendExample
Append append = new Append(Bytes.toBytes("row1"));
append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
Bytes.toBytes("newvalue"));
append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
Bytes.toBytes("anothervalue"));

table.append(append);
// ^^ AppendExample
System.out.println("After append call...");
helper.dump("testtable", new String[]{"row1"}, null, null);
table.close();
connection.close();
// Connection connection = ConnectionFactory.createConnection(conf);
// Table table = connection.getTable(TableName.valueOf("testtable"));
//
// // vv AppendExample
// Append append = new Append(Bytes.toBytes("row1"));
// append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
// Bytes.toBytes("newvalue"));
// append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
// Bytes.toBytes("anothervalue"));
//
// table.append(append);
// // ^^ AppendExample
// System.out.println("After append call...");
// helper.dump("testtable", new String[]{"row1"}, null, null);
// table.close();
// connection.close();
helper.close();
}
}
5 changes: 5 additions & 0 deletions ch03/src/main/java/client/CRUDExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ public static void main(String[] args) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));
) {
// 添加
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
Bytes.toBytes("val1"));
put.addColumn(Bytes.toBytes("colfam2"), Bytes.toBytes("qual2"),
Bytes.toBytes("val2"));
table.put(put);

// 使用Scan 查询
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result2 : scanner) {
while (result2.advance())
System.out.println("Cell: " + result2.current());
}

// 使用Get 查询
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
Result result = table.get(get);
Expand All @@ -56,10 +59,12 @@ public static void main(String[] args) throws IOException {
Bytes.toBytes("qual1"));
System.out.println("Value only: " + Bytes.toString(val));

// 删除数据
Delete delete = new Delete(Bytes.toBytes("row1"));
delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));
table.delete(delete);

// 使用scanner查询
Scan scan2 = new Scan();
ResultScanner scanner2 = table.getScanner(scan2);
for (Result result2 : scanner2) {
Expand Down
1 change: 1 addition & 0 deletions ch03/src/main/java/client/CRUDExamplePreV1API.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@SuppressWarnings("deprecation") // because of old API usage
public class CRUDExamplePreV1API {

// 旧版本的API
public static void main(String[] args) throws IOException {
// vv CRUDExamplePreV1API
Configuration conf = HBaseConfiguration.create();
Expand Down
31 changes: 16 additions & 15 deletions ch03/src/main/java/client/DeleteExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();

HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", 100, "colfam1", "colfam2");
helper.put("testtable",
new String[] { "row1" },
new String[] { "colfam1", "colfam2" },
new String[] { "qual1", "qual1", "qual2", "qual2", "qual3", "qual3" },
new long[] { 1, 2, 3, 4, 5, 6 },
new String[] { "val1", "val1", "val2", "val2", "val3", "val3" });
System.out.println("Before delete call...");
helper.dump("testtable", new String[]{ "row1" }, null, null);
// helper.dropTable("testtable");
// helper.createTable("testtable", 100, "colfam1", "colfam2");
// helper.put("testtable",
// new String[] { "row1" },
// new String[] { "colfam1", "colfam2" },
// new String[] { "qual1", "qual1", "qual2", "qual2", "qual3", "qual3" },
// new long[] { 1, 2, 3, 4, 5, 6 },
// new String[] { "val1", "val1", "val2", "val2", "val3", "val3" });
// System.out.println("Before delete call...");
// helper.dump("testtable", new String[]{ "row1" }, null, null);

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));
Expand All @@ -41,12 +41,13 @@ public static void main(String[] args) throws IOException {

delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co DeleteExample-3-DelColNoTS Delete the latest version only in one column.
delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 3); // co DeleteExample-4-DelColTS Delete specific version in one column.

//
delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); // co DeleteExample-5-DelColsNoTS Delete all versions in one column.
delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 2); // co DeleteExample-6-DelColsTS Delete the given and all older versions in one column.

delete.addFamily(Bytes.toBytes("colfam1")); // co DeleteExample-7-AddCol Delete entire family, all columns and versions.
delete.addFamily(Bytes.toBytes("colfam1"), 3); // co DeleteExample-8-AddCol Delete the given and all older versions in the entire column family, i.e., from all columns therein.
// // co DeleteExample-6-DelColsTS Delete the given and all older versions in one column.
// delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 2);
//
// delete.addFamily(Bytes.toBytes("colfam1")); // co DeleteExample-7-AddCol Delete entire family, all columns and versions.
// delete.addFamily(Bytes.toBytes("colfam1"), 3); // co DeleteExample-8-AddCol Delete the given and all older versions in the entire column family, i.e., from all columns therein.

table.delete(delete); // co DeleteExample-9-DoDel Delete the data from the HBase table.

Expand Down
1 change: 1 addition & 0 deletions ch03/src/main/java/client/PutExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.IOException;

// Done
public class PutExample {

public static void main(String[] args) throws IOException {
Expand Down
1 change: 1 addition & 0 deletions ch03/src/main/java/client/PutIdenticalExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;

// Done 这个主要是验证存放数据后,是否是真的把数据插入到了数据库里面
public class PutIdenticalExample {

public static void main(String[] args) throws IOException {
Expand Down
1 change: 1 addition & 0 deletions ch03/src/main/java/client/PutListErrorExample1.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import util.HBaseHelper;

// 往列族中插入的数据,如果这一个列族不存在,则会报错,
public class PutListErrorExample1 {

public static void main(String[] args) throws IOException {
Expand Down
2 changes: 2 additions & 0 deletions ch03/src/main/java/client/PutListErrorExample2.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public static void main(String[] args) throws IOException {
put3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
Bytes.toBytes("val3"));
puts.add(put3);
// 不允许插入空的记录
/*[*/Put put4 = new Put(Bytes.toBytes("row2"));
puts.add(put4);/*]*/ // co PutListErrorExample2-1-AddErrorPut Add put with no content at all to list.


/*[*/try {/*]*/
table.put(puts);
/*[*/} catch (Exception e) {
Expand Down
1 change: 1 addition & 0 deletions ch03/src/main/java/client/PutListErrorExample3.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class PutListErrorExample3 {

// 如果出错,后续可以检测到哪里出的错,以及可以打印出错的信息
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
try (
Expand Down
1 change: 1 addition & 0 deletions ch03/src/main/java/client/PutListExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

public class PutListExample {

// 往里面插入整列的值
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();

Expand Down
1 change: 1 addition & 0 deletions ch03/src/main/java/client/ResultExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

public class ResultExample {

// Result 的一些用法
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();

Expand Down
3 changes: 3 additions & 0 deletions ch03/src/main/java/client/RowKeyExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public static void main(String[] args) {
// vv RowKeyExample
byte[] data = new byte[100];
Arrays.fill(data, (byte) '@');
System.out.println(Arrays.toString(data));
String username = "johndoe";
byte[] username_bytes = username.getBytes(Charset.forName("UTF8"));

// 系统的ArrayCopy
// 把username_bytes 中下标从0 开始一直到最后的数据, 拷贝到 data 中下表为第45 开始,增加7位
System.arraycopy(username_bytes, 0, data, 45, username_bytes.length);
System.out.println("data length: " + data.length +
", data: " + Bytes.toString(data));
Expand Down
38 changes: 0 additions & 38 deletions ch03/src/test/java/PutTestSuite.java

This file was deleted.

37 changes: 0 additions & 37 deletions ch03/src/test/resources/hbase-site.xml

This file was deleted.

9 changes: 0 additions & 9 deletions ch03/src/test/resources/log4j.properties

This file was deleted.

1 change: 1 addition & 0 deletions ch04/src/main/java/client/IncrementSingleExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

Expand Down
4 changes: 4 additions & 0 deletions ch05/src/main/java/admin/HColumnDescriptorExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class HColumnDescriptorExample {

public static void main(String[] args) throws IOException, InterruptedException {
// vv HColumnDescriptorExample
// 创建一个HColumnDescriptor 对象,即创建一个列族,列族的名字是colfam1,
// 其中有一个kv 对, K 的名字是test-key V 的值是test-value
// 后面的setBloomFilterType 的作用是,设置一个'布隆'过滤器,在一定的条件下,可以提高其随机读取的效率,
// 但是会比较耗费内存
HColumnDescriptor desc = new HColumnDescriptor("colfam1")
.setValue("test-key", "test-value")
.setBloomFilterType(BloomType.ROWCOL);
Expand Down
Loading

0 comments on commit fd773ba

Please sign in to comment.