}
public class HbaseDemo {
Connection conn = null;
@Before
public void init() throws Exception{
Configuration conf = ate(); // 会自动加载classpath中的l文件中的参数
conn = ateConnection(conf);
}
/**
* 插入数据
* @throws IOException
*/
@Test
public void testPut() throws IOException{
Table table = Table(TableName.valueOf("t_article"));
// 准备好要插入的数据,封装为Put对象
Put put1 = new Put("zhangsan-20170809-001".getBytes());
put1.addColumn("primary_content".getBytes(), "title".getBytes(), "震惊世界,印度阿三入侵伟大中华帝国达50天之久,中国居然毫无反应".getBytes());
put1.addColumn("primary_content".getBytes(), "author".getBytes(), "zhangsan".getBytes());
put1.addColumn("primary_content".getBytes(), "content".getBytes(), "阿三作死,迟早会把中国人民逼急的,直接一路打到新德里,活捉墓地,扒皮抽筋".getBytes());
Put put2 = new Put("zhongsheng-20170809-002".getBytes());
put2.addColumn("primary_content".getBytes(), "title".getBytes(), "忍无可忍,无需再忍".getBytes());
put2.addColumn("primary_content".getBytes(), "author".getBytes(), "zhongsheng".getBytes());
put2.addColumn("primary_content".getBytes(), "content".getBytes(), "话已说尽,仁至义尽,忍无可忍,无需再忍,勿谓言之不预也".getBytes());
ArrayList<Put> puts = new ArrayList<>();
puts.add(put1);
puts.add(put2);
table.put(puts);
table.close();
conn.close();
}
@Test
public void testPut2() throws IOException{
Table table = Table(TableName.valueOf("t_user_info"));
// 准备好要插入的数据,封装为Put对象
Put put1 = new Put("zhang-bj-male-001".getBytes());
put1.addColumn("f1".getBytes(), "name".getBytes(), "张三".getBytes());
put1.addColumn("f1".getBytes(), "age".getBytes(), Bytes(30));
Put put2 = new Put("zhang-bj-female-002".getBytes());
put2.addColumn("f1".getBytes(), "name".getBytes(), "张丽".getBytes());
put2.addColumn("f1".getBytes(), "age".getBytes(), Bytes(20));
Put put3 = new Put("liu-bj-female-003".getBytes());
put3.addColumn("f1".getBytes(), "name".getBytes(), "刘芳".getBytes());
put3.addColumn("f1".getBytes(), "age".getBytes(), Bytes(23));
Put put4 = new Put("liu-bj-female-004".getBytes());
put4.addColumn("f1".getBytes(), "name".getBytes(), "刘红".getBytes());
put4.addColumn("f1".getBytes(), "age".getBytes(), Bytes(23));
Put put5 = new Put("liu-sh-male-005".getBytes());
put5.addColumn("f1".getBytes(), "name".getBytes(), "刘德华".getBytes());
put5.addColumn("f1".getBytes(), "age".getBytes(), Bytes(43));
ArrayList<Put> puts = new ArrayList<>();
puts.add(put1);
puts.add(put2);
puts.add(put3);
puts.add(put4);
puts.add(put5);
table.put(puts);
table.close();
conn.close();
}
/**
* 删除数据
* @throws Exception
*/
@Test
public void testDelete() throws Exception{
Table table = Table(TableName.valueOf("t_article"));
Delete delete = new Delete("zhangsan-20170809-001".getBytes());
// 如果delete对象中不指定具体的key,则会删除这一整行
// table.delete(delete);
// delete对象中指定要删的key
delete.addColumn("primary_content".getBytes(), "author".getBytes());
table.delete(delete);
table.close();
conn.close();
}
/**
* 修改数据 : 其实就是put,put会覆盖相同rowkey相同key的数据
* @throws Exception
*/
@Test
public void testUpdate() throws Exception{
Table table = Table(TableName.valueOf("t_article"));
Put put = new Put("zhongsheng-20170809-002".getBytes());
put.addColumn("primary_content".getBytes(), "author".getBytes(), "人民日报".getBytes());
// 覆盖同行同key的数据value
table.put(put);
table.close();
conn.close();
}
/**
* 单行查询数据:
* @throws Exception
*/
@Test
public void testGet() throws Exception{
Table table = Table(TableName.valueOf("t_article"));
// 构造查询参数
Get get = new Get("zhongsheng-20170809-002".getBytes());
// 指定要获取的key
get.addColumn("primary_content".getBytes(), "author".getBytes());
Result result = (get);
byte[] value = Value("primary_content".getBytes(), "author".getBytes());
String author = new String(value,"utf-8");
System.out.println(author);
table.close();
conn.close();
}
/**
* 范围查询
* @throws IOException
*/
@Test
public void testScan() throws IOException{
Table table = Table(TableName.valueOf("t_article"));
// 含首不含尾
Scan scan = new Scan("zhangsan-20170809-001".getBytes(), "zhongsheng-20170809-002 00".getBytes());
ResultScanner scanner = Scanner(scan);
Iterator<Result> it = scanner.iterator();
while(it.hasNext()){
// 取到一行
Result result = it.next();
//Value(family, qualifier)
// 取该行的cell迭代器
CellScanner cellScanner = llScanner();
// 迭代这一行的cell
while(cellScanner.advance()){
Cell cell = cellScanner.current();
byte[] rowArray = RowArray();
byte[] familyArray = FamilyArray();
byte[] qualifierArray = QualifierArray();
byte[] valueArray = ValueArray();
System.out.println("行键:" +new String(RowOffset(),RowLength()));
System.out.println("列族名:" +new String(FamilyOffset(),FamilyLength()));
System.out.println("key:" +new String(QualifierOffset(),QualifierLength()));
System.out.println("value:" +new String(ValueOffset(),ValueLength()));
}
/*cellScanner.advance();
Cell cell = cellScanner.current();
byte[] rowArray = RowArray();
byte[] familyArray = FamilyArray();
byte[] qualifierArray = QualifierArray();
byte[] valueArray = ValueArray();
System.out.println("行键:" +new String(rowArray));
System.out.println("列族名:" +new String(familyArray));
System.out.println("key:" +new String(qualifierArray));
System.out.println("value:" +new String(valueArray));*/
System.out.println("**************行分割线***********************");
}
scanner.close();
table.close();
conn.close();
}
/**
* 过滤查询示范
* @throws Exception
*/
@Test
public void testFilterScan() throws Exception{
Table table = Table(TableName.valueOf("t_user_info"));
PrefixFilter filter =new PrefixFilter("liu".getBytes()); // 构造了一个条件过滤器
Scan scan = new Bytes("liu-bj-female-003"), filter);
ResultScanner scanner = Scanner(scan);
Iterator<Result> it = scanner.iterator();
while(it.hasNext()){
// 取到一行
Result result = it.next();
//Value(family, qualifier)
// 取该行的cell迭代器
CellScanner cellScanner = llScanner();
// 迭代这一行的cell
while(cellScanner.advance()){
Cell cell = cellScanner.current();
byte[] rowArray = RowArray();
byte[] familyArray = FamilyArray();
byte[] qualifierArray = QualifierArray();
byte[] valueArray = ValueArray();
System.out.println("行键:" +new String(RowOffset(),RowLength()));
System.out.println("列族名:" +new String(FamilyOffset(),FamilyLength()));
System.out.println("key:" +new String(QualifierOffset(),QualifierLength()));
System.out.println("value:" +new String(ValueOffset(),ValueLength()));
}
System.out.println("**************行分割线***********************");
}
scanner.close();
table.close();
conn.close();
}
}
本文发布于:2024-02-05 06:38:58,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170726463163946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |