Hbase basic operation---idea connects to hbase (shell script springBoot integrates Hbase)

Write automation scripts

vim zhh.sh 
============================= 
#shell script determines parameters 
if [ $1 == "start" ];then 
    echo 'Start zookeeper\hadoop\hbase' 
    /opt/soft/zk345/bin/zkServer.sh start 
    /opt/soft/hadoop260/sbin/start-all.sh 
    /opt/soft/hbase120/bin/start-hbase.sh 
else 
    echo 'Close hbase\hadoop\zookeeper' 
    /opt/soft/hbase120/bin/stop-hbase.sh 
    /opt/soft/hadoop260/sbin/stop-all.sh 
    /opt/soft/zk345/bin/zkServer.sh stop 
fi   
​==============================
 
#Authorize 
chmod 700 zhh.sh 
ls 
#Start 
./zhh.sh start 
#Close 
. /zhh.sh 
stop

 

 

 

 

A table has n column families. 
It is generally recommended to have no more than 
3.

 

Enter database

hbase shell 
#version 
version 
#status 
#View 
whoami 
list 
desc 'mydemo:userinfos' 
#Insert data (name Zhang San is 40 years old) 
put 
'mydemo:userinfos','1','base:name','zhangsan' 
put ' mydemo:userinfos','1','base:age',40 
#Query value 
get 'mydemo:userinfos','1 
'

 

#Batch insert

#First exit the database 
exit 
hdfs dfs -mkdir -p /opt 
hdfs dfs -put /opt/hbase01.csv /opt 
#Here
it is best to paste and insert data hbase sentence by sentence 
org.apache.hadoop.hbase.mapreduce.ImportTsv 
-Dimporttsv. separator=, 
-Dimporttsv.columns="HBASE_ROW_KEY,base:name,base:age" 
'mydemo:userinfos' /opt/hbase01.csv 
​#Enter the 
database 
hbase shell 
#View data 
count 'mydemo:userinfos'

hbase common commands

#Scan 
scan 'mydemo:userinfos' 
#Delete 
get 'mydemo:userinfos','1' 
delete 'mydemo:userinfos','1',"base:age" 
#Delete table 
disable 'mydemo:userinfos' 
drop 'mydemo:userinfos ' 
list 
#Create table 
create "mydemo:userinfos","base" 
#Paging scan 
scan 'mydemo:userinfos',{LIMIT=>3} 
scan 'mydemo:userinfos',{FILTER=>"ValueFilter(=,'substring: a')"} 
scan 'mydemo:userinfos',{FILTER=>"SingleColumnValueFilter('base','name',=,'binary:aahepo')" 
}

java writing code

#1 导包
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.2.0</version>
    </dependency>
#2 Write connection settings 
public class App 
{ 
    public static void main( String[] args ) throws Exception { 
        //Configure zookeeper address 
        Configuration cfg = HBaseConfiguration.create(); 
        cfg.set("hbase.zookeeper.quorum","192.168 .64.210:2181"); 
        //Get the database connection object 
        Connection connection = ConnectionFactory.createConnection(cfg); 
        //Get the data table 
        Table table = connection.getTable(TableName.valueOf("mydemo:userinfos")); 
        //Query Data in the table 
        ResultScanner re = table.getScanner(new Scan()); 
        //Traverse the loop data 
        for (Result result : re) {  
            String name = new String(result.getValue("base".getBytes(),"name".getBytes()));
            String age = new String(result.getValue("base".getBytes(),"age".getBytes()));
            System.out.println(name+"<============>"+age);
        }
        connection.close();
    }
}
​

 

#Insert a piece of data 
public class App 
{ 
    public static void main( String[] args ) throws Exception { 
        //Configure the zookeeper address 
        Configuration cfg = HBaseConfiguration.create(); 
        cfg.set("hbase.zookeeper.quorum","192.168. 64.210:2181"); 
        //Get the database connection object 
        Connection connection = ConnectionFactory.createConnection(cfg); 
        //Get the data table 
        Table table = connection.getTable(TableName.valueOf("mydemo:userinfos")); 
        //Prepare a put command 
        Put put=new Put("10000".getBytes()); 
        put.addColumn("base".getBytes(),"name".getBytes(),"zhangsanfeng".getBytes()); 
        table.put(put); 
        put.addColumn( "base".getBytes(),"age".getBytes(),"200".getBytes());
        //Insert a piece of data 
        connection.close(); 
    }
#Query a message 
public class App 
{ 
    public static void main( String[] args ) throws Exception { 
        //Configure the zookeeper address 
        Configuration cfg = HBaseConfiguration.create(); 
        cfg.set("hbase.zookeeper.quorum","192.168. 64.210:2181"); 
        //Get the database connection object 
        Connection connection = ConnectionFactory.createConnection(cfg); 
        //Get the data table 
        Table table = connection.getTable(TableName.valueOf("mydemo:userinfos")); 
// // Prepare a put command 
// Put put=new Put("10000".getBytes()); 
// put.addColumn("base".getBytes(),"name".getBytes(),"zhangsanfeng". 
// table.put(put); 
// put.addColumn("base".getBytes(),"age".getBytes(),"200".getBytes());
// //Insert a piece of data 
        //Query a 
        Get get = new Get("10000".getBytes()); 
        Result result = table.get(get); 
        System.out.println(new String(result.getValue(" base".getBytes(),"name".getBytes()))); 
        connection.close(); 
    } 
}
#Batch insert 
database view 
scan 'mydemo:userinfos',{FILTER=>"PrefixFilter('101')"} 
====================== 
​public
class App 
{ 
    public static void main( String[] args ) throws Exception { 
        //Configure zookeeper address 
        Configuration cfg = HBaseConfiguration.create(); 
        cfg.set("hbase.zookeeper.quorum","192.168.64.210:2181"); 
        //Get the database connection object 
        Connection connection = ConnectionFactory.createConnection(cfg); 
        //Get the data table 
        Table table = connection.getTable(TableName.valueOf("mydemo:userinfos"));
        List<Put> puts = new ArrayList<>();
        for (int i = 1010; i < 1020; i++) {
            Put put = new Put((i + "").getBytes());
            put.addColumn("base".getBytes(),"name".getBytes(),("test"+i).getBytes());
            puts.add(put);
        }
        table.put(puts);
        connection.close();
    }
}

springboot integrates hbase

#1 导包 pom
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-shaded-client</artifactId>
            <version>1.2.0</version>
        </dependency>
#2 config配置
@Configuration
public class HbaseConfig {
    @Value("${hbase.zookeeper.quorum}")
    private String zookeeper_url;
​
    @Bean
    public org.apache.hadoop.conf.Configuration hbaseConfiguration(){
        org.apache.hadoop.conf.Configuration cfg= HBaseConfiguration.create();
        cfg.set(HConstants.ZOOKEEPER_QUORUM,zookeeper_url);
        return cfg;
​
    }
​
    @Bean
    @Scope("prototype")
    public Connection getConnection() {
        Connection connection=null;
        try {
            connection = ConnectionFactory.createConnection(hbaseConfiguration());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return connection;
    }
​
    @Bean
    public Supplier<Connection> hbaseConSupplier(){
        return ()->{
            return getConnection();
        };
    }
}
​
#3 配置实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Userinfos {
    private String rowkey;
    private String name;
    private String age;
}
​
#4 配置service层
@Service
public class HbaseService {
    @Resource
    private Connection hbasecon;
​
    public List<Userinfos> findAll(){
        ArrayList<Userinfos> users = new ArrayList<>();
        //获取表
        try {
            Table tab = hbasecon.getTable(TableName.valueOf("mydemo:userinfos".getBytes()));
            Scan scan = new Scan();
            ResultScanner results = tab.getScanner(scan);
            for (Result result : results) {
                String name="";
                String age="";
                if(result.containsColumn("base".getBytes(), "name".getBytes())){
                     name = new String(result.getValue("base".getBytes(), "name".getBytes()));
                }
                if(result.containsColumn("base".getBytes(), "age".getBytes())){
                     age = new String(result.getValue("base".getBytes(), "age".getBytes()));
                }
​
​
                Userinfos ui = Userinfos.builder().rowkey(new String(result.getRow()))
                        .name(name)
                        .age(age).build();
​
                users.add(ui);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return users;
    }
    
}
#5 配置 yml
server:
  port: 8081
spring:
  application:
    name: sbhbase
hbase:
  zookeeper:
    quorum: 192.168.64.210:2181
#6 测试
@SpringBootTest
class SbhbaseApplicationTests {
    @Resource
    private HbaseService hbaseService;
​
    @Test
    void contextLoads() {
​
        System.out.println(hbaseService.findAll());
    }
​
}
​

Create namespace

create_namespace 'towercrane'
​
create 'towercrane:tchandler','base'
​
exit

View big data

cd /opt/data
ls
cat event_attendees.csv | head -1
cat user_friends.csv  | head -1

Guess you like

Origin blog.csdn.net/just_learing/article/details/126214989