您好,登錄后才能下訂單哦!
自動導包(需要從cloudera倉庫下載,耗時較長,耐心等待)
如下內容作為maven工程中pom.xml的repositories的內容
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-mr1-cdh6.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0-cdh6.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0-cdh6.14.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*/RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
/**
* 操作數據庫 第一步:獲取連接 第二步:獲取客戶端對象 第三步:操作數據庫 第四步:關閉
* 創建myuser表,有兩個列族 f1 f2
* @throws IOException
*/
@Test
public void createTable() throws IOException {
//獲得連接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
//創建連接對象
Connection connection = ConnectionFactory.createConnection(configuration);
//操作:建表、刪除表、修改表 -> 管理員;創建管理員對象
Admin admin = connection.getAdmin();
//添加了表名信息
HTableDescriptor myuser = new HTableDescriptor(TableName.valueOf("myuser"));
//給表添加列族
myuser.addFamily(new HColumnDescriptor("f1"));
myuser.addFamily(new HColumnDescriptor("f2"));
//創建表
admin.createTable(myuser);
//關閉連接
admin.close();
connection.close();
}
private Connection connection;
private Table table;
//建立連接
@BeforeTest
public void init() throws IOException {
//獲得連接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
//configuration.set("zookeeper.znode.parent", "/HBase");
connection = ConnectionFactory.createConnection(configuration);
//獲得表
table = connection.getTable(TableName.valueOf("myuser"));
}
@AfterTest
public void close() throws IOException {
table.close();
connection.close();
}
//向表中添加數據
@Test
public void putData() throws IOException {
Put put = new Put("0001".getBytes());//創建Put對象,并指定rowkey值
//添加cell值:列族名稱+列名+值
put.addColumn("f1".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(18));
put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(25));
put.addColumn("f1".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));
table.put(put);
}
@Test
public void batchInsert() throws IOException {
//創建put對象,并指定rowkey
Put put = new Put("0002".getBytes());
//向f1列族添加數據
put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
//向f2列族添加數據
put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛國譙縣"));
put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("劉備"));
put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿縣"));
put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孫權"));
put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));
Put put4 = new Put("0005".getBytes());
put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("諸葛亮"));
put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出師表你背了嘛"));
Put put5 = new Put("0006".getBytes());
put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司馬懿"));
put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟諸葛亮死掐"));
Put put6 = new Put("0007".getBytes());
put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—呂布"));
put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("內蒙人"));
put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蟬去哪了"));
List<Put> listPut = new ArrayList<Put>();
listPut.add(put);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);
table.put(listPut);
}
//查詢rowkey為0003的數據
@Test
public void getDataByRowkey() throws IOException {
//通過get對象,指定rowkey
Get get = new Get("0003".getBytes());
//獲取某列族
get.addFamily("f1".getBytes());//限定查詢f1列族下面所有列的值
get.addColumn("f2".getBytes(), "say".getBytes());//查詢f2列族say列的值
//通過get查詢,返回0003行,f1列族、f2:say列的所有cell的值,封裝到一個Result對象
Result result = table.get(get);
//獲得Result中的所有Cell
List<Cell> cells = result.listCells();
//遍歷Cell
for(Cell cell: cells) {
//cell單元格
//獲得rowkey
byte[] rowkey_bytes = CellUtil.cloneRow(cell);
//獲得列族
byte[] family_bytes = CellUtil.cloneFamily(cell);
//獲得列
byte[] qualifier_bytes = CellUtil.cloneQualifier(cell);
//獲得值
byte[] cell_bytes = CellUtil.cloneValue(cell);
if("age".equals(Bytes.toString(qualifier_bytes)) || "id".equals(Bytes.toString(qualifier_bytes))) {
System.out.println(Bytes.toString(rowkey_bytes));
System.out.println(Bytes.toString(family_bytes));
System.out.println(Bytes.toString(qualifier_bytes));
System.out.println(Bytes.toInt(cell_bytes));//age或id的值是int類型,得用Bytes.toInt
} else {
System.out.println(Bytes.toString(rowkey_bytes));
System.out.println(Bytes.toString(family_bytes));
System.out.println(Bytes.toString(qualifier_bytes));
System.out.println(Bytes.toString(cell_bytes));
}
}
}
/**
* 不知道rowkey的具體值,我想查詢rowkey范圍值是0003 到0006
* select * from myuser where age > 30 and id < 8 and name like 'zhangsan'
*/
@Test
public void scanData() throws IOException {
Scan scan = new Scan();//若沒有指定startRow以及stopRow,則全表掃描
//掃描f1列族
scan.addFamily("f1".getBytes());
//掃描 f2列族 phone列
scan.addColumn("f2".getBytes(),"phone".getBytes());
//設置起始結束rowkey,前閉后開
scan.setStartRow("0003".getBytes());
scan.setStopRow("0007".getBytes());
//設置每批次返回客戶端的數據條數
scan.setBatch(20);
//從cacheBlock中讀取數據
scan.setCacheBlocks(true);
scan.setMaxResultSize(4);
scan.setMaxVersions(2);//獲取歷史2個版本
//通過getScanner查詢獲取到了表里面所有的數據,是多條數據
ResultScanner scanner = table.getScanner(scan);
//遍歷ResultScanner 得到每一條數據,每一條數據都是封裝在result對象里面了
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family_name = CellUtil.cloneFamily(cell);
byte[] qualifier_name = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] value = CellUtil.cloneValue(cell);
//判斷id和age字段,這兩個字段是整形值
if("age".equals(Bytes.toString(qualifier_name)) || "id".equals(Bytes.toString(qualifier_name))){
System.out.println("數據的rowkey為" + Bytes.toString(rowkey) +"======數據的列族為" + Bytes.toString(family_name)+"======數據的列名為" + Bytes.toString(qualifier_name) + "==========數據的值為" +Bytes.toInt(value));
}else{
System.out.println("數據的rowkey為" + Bytes.toString(rowkey) +"======數據的列族為" + Bytes.toString(family_name)+"======數據的列名為" + Bytes.toString(qualifier_name) + "==========數據的值為" +Bytes.toString(value));
}
}
}
}
/**
* 刪除數據
*/
@Test
public void deleteData() throws IOException {
Delete delete = new Delete("0003".getBytes());
delete.addFamily("f1".getBytes());
delete.addColumn("f2".getBytes(), "phone".getBytes());
table.delete(delete);
}
/**
* 刪除表
*/
@Test
public void deleteTable() throws IOException {
//獲取管理員對象,用于表的刪除
Admin admin = connection.getAdmin();
//刪除一張表之前,需要先禁用表
admin.disableTable(TableName.valueOf("myuser"));
admin.deleteTable(TableName.valueOf("myuser"));
}
e(delete);
}
## 5.2、刪除表操作
```java
/**
* 刪除表
*/
@Test
public void deleteTable() throws IOException {
//獲取管理員對象,用于表的刪除
Admin admin = connection.getAdmin();
//刪除一張表之前,需要先禁用表
admin.disableTable(TableName.valueOf("myuser"));
admin.deleteTable(TableName.valueOf("myuser"));
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。