《大数据和人工智能交流》头条号向广大初学者新增C 、Java 、Python 、Scala、javascript 等目前流行的计算机、大数据编程语言,希望大家以后关注本头条号更多的内容。
注意HBase必须引用HBase1.2版本以上的新的API,HBase版本以下API不合适,况且1.0版本以下bug非常多。
package com.it18zhang.hbasedemo.test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import javax.naming.Name;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
/**
*
*/
public class TestCRUD {
@Test
public void put() throws Exception {
//创建conf对象
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接对象
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接查询tableName对象
TableName tname = TableName.valueOf("ns1:t1");
//获得table
Table table = conn.getTable(tname);
//通过bytes工具类创建字节数组(将字符串)
byte[] rowid = Bytes.toBytes("row3");
//创建put对象
Put put = new Put(rowid);
byte[] f1 = Bytes.toBytes("f1");
byte[] id = Bytes.toBytes("id") ;
byte[] value = Bytes.toBytes(102);
put.addColumn(f1,id,value);
//执行插入
table.put(put);
}
@Test
public void bigInsert() throws Exception {
DecimalFormat format = new DecimalFormat();
format.applyPattern("0000");
long start = System.currentTimeMillis() ;
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
TableName tname = TableName.valueOf("ns1:t1");
HTable table = (HTable)conn.getTable(tname);
//不要自动清理缓冲区
table.setAutoFlush(false);
for(int i = 1 ; i < 10000 ; i ++){
Put put = new Put(Bytes.toBytes("row" + format.format(i))) ;
//关闭写前日志
put.setWriteToWAL(false);
put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("id"),Bytes.toBytes(i));
put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"),Bytes.toBytes("tom" + i));
put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("age"),Bytes.toBytes(i % 100));
table.put(put);
if(i % 2000 == 0){
table.flushCommits();
}
}
//
table.flushCommits();
System.out.println(System.currentTimeMillis() - start );
}
@Test
public void get() throws Exception {
//创建conf对象
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接对象
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接查询tableName对象
TableName tname = TableName.valueOf("ns1:t1");
//获得table
Table table = conn.getTable(tname);
//通过bytes工具类创建字节数组(将字符串)
byte[] rowid = Bytes.toBytes("row3");
Get get = new Get(Bytes.toBytes("row3"));
Result r = table.get(get);
byte[] idvalue = r.getValue(Bytes.toBytes("f1"),Bytes.toBytes("id"));
System.out.println(Bytes.toInt(idvalue));
}
@Test
public void formatNum(){
DecimalFormat format = new DecimalFormat();
format.applyPattern("0000000");
System.out.println(format.format(8));
}
@Test
public void createNameSpace() throws Exception {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
//创建名字空间描述符
NamespaceDescriptor nsd = NamespaceDescriptor.create("ns1").build();
admin.createNamespace(nsd);
NamespaceDescriptor[] ns = admin.listNamespaceDescriptors();
for(NamespaceDescriptor n : ns){
System.out.println(n.getName());
}
}
@Test
public void listNameSpaces() throws Exception {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
NamespaceDescriptor[] ns = admin.listNamespaceDescriptors();
for(NamespaceDescriptor n : ns){
System.out.println(n.getName());
}
}
@Test
public void createTable() throws Exception {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
//创建表名对象
TableName tableName = TableName.valueOf("ns1:t1");
//创建表描述符对象
HTableDescriptor tbl = new HTableDescriptor(tableName);
//创建列族描述符
HColumnDescriptor col = new HColumnDescriptor("f1");
tbl.addFamily(col);
admin.createTable(tbl);
}
@Test
public void disableTable() throws Exception {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
//禁用表 enable(...) disableTable(...)
admin.deleteTable(TableName.valueOf("ns2:t2"));
}
@Test
public void deleteData() throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
TableName tname = TableName.valueOf("ns1:t1");
Table table = conn.getTable(tname);
Delete del = new Delete(Bytes.toBytes("row0001"));
del.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("id"));
del.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"));
table.delete(del);
System.out.println("over");
}
/**
* 删除数据
*/
@Test
public void scan() throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
TableName tname = TableName.valueOf("ns1:t1");
Table table = conn.getTable(tname);
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("row5000"));
scan.setStopRow(Bytes.toBytes("row8000"));
ResultScanner rs = table.getScanner(scan);
Iterator it = rs.iterator();
while (it.hasNext()) {
Result r = it.next();
byte[] name = r.getValue(Bytes.toBytes("f1"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(name));
}
}
/**
* 动态遍历
*/
@Test
public void scan2() throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
TableName tname = TableName.valueOf("ns1:t1");
Table table = conn.getTable(tname);
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("row5000"));
scan.setStopRow(Bytes.toBytes("row8000"));
ResultScanner rs = table.getScanner(scan);
Iterator it = rs.iterator();
while (it.hasNext()) {
Result r = it.next();
Map map = r.getFamilyMap(Bytes.toBytes("f1"));
for(Map.Entry entrySet : map.entrySet()){
String col = Bytes.toString(entrySet.getKey());
String val = Bytes.toString(entrySet.getValue());
System.out.print(col + ":" + val + ",");
}
System.out.println();
}
}
/**
* 动态遍历
*/
@Test
public void scan3() throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
TableName tname = TableName.valueOf("ns1:t1");
Table table = conn.getTable(tname);
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("row5000"));
scan.setStopRow(Bytes.toBytes("row8000"));
ResultScanner rs = table.getScanner(scan);
Iterator it = rs.iterator();
while (it.hasNext()) {
Result r = it.next();
//得到一行的所有map,key=f1,value=Map
>NavigableMap>> map = r.getMap();
//
for(Map.Entry>> entry : map.entrySet()){
//得到列族
String f = Bytes.toString(entry.getKey());
Map> colDataMap = entry.getValue();
for(Map.Entry> ets : colDataMap.entrySet() ){
String c = Bytes.toString(ets.getKey());
Map tsValueMap = ets.getValue();
for(Map.Entry e : tsValueMap.entrySet()){
Long ts = e.getKey() ;
String value = Bytes.toString(e.getValue());
System.out.print(f+":"+c+":"+ts+"=" +value + ",");
}
}
}
System.out.println();
}
}
}
![0cc863f153df68b043cb3682bd9046e1.png](https://i-blog.csdnimg.cn/blog_migrate/ce98800a156f0a96dfa9c5893dbbc344.jpeg)
《大数据和人工智能交流》的宗旨
1、将大数据和人工智能的专业数学:概率数理统计、线性代数、决策论、优化论、博弈论等数学模型变得通俗易懂。
2、将大数据和人工智能的专业涉及到的数据结构和算法:分类、聚类 、回归算法、概率等算法变得通俗易懂。
3、最新的高科技动态:数据采集方面的智能传感器技术;医疗大数据智能决策分析;物联网智慧城市等等。
根据初学者需要会有C语言、Java语言、Python语言、Scala函数式等目前主流计算机语言。
根据读者的需要有和人工智能相关的计算机科学与技术、电子技术、芯片技术等基础学科通俗易懂的文章。