美文网首页
Phoenix开发

Phoenix开发

作者: 诺之林 | 来源:发表于2020-12-18 14:13 被阅读0次

    本文的主线 项目 => 操作

    本文的示例代码参考hbase-phoenix

    本文基于Phoenix搭建 / Phoenix使用

    项目

    spring init -b=2.2.10.RELEASE -j=1.8 -l=java -d=web --build=gradle hbase-phoenix && cd hbase-phoenix
    
    vim build.gradle
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.1'
        implementation('org.apache.phoenix:phoenix-core:5.0.0-HBase-2.0') {
            exclude(module: 'slf4j-log4j12')
            exclude(module: 'log4j')
        }
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
    
    vim src/main/resources/application.properties
    
    spring.datasource.url=jdbc:phoenix:localhost
    spring.datasource.driver-class-name=org.apache.phoenix.jdbc.PhoenixDriver
    

    操作

    vim src/main/java/com/example/hbasephoenix/GPSModel.java
    
    package com.example.hbasephoenix;
    
    public class GPSModel {
        public Integer id;
        public Double longitude;
        public Double latitude;
        public String city;
        public Integer createdTs;
    }
    
    vim src/main/java/com/example/hbasephoenix/GPSMapper.java
    
    package com.example.hbasephoenix;
    
    import java.util.List;
    import java.util.Map;
    
    import org.apache.ibatis.annotations.*;
    
    @Mapper
    public interface GPSMapper {
        @Select("SELECT * FROM t_gps WHERE id = #{id}")
        public List<GPSModel> findById(Integer id);
    }
    
    vim src/main/java/com/example/hbasephoenix/GPSController.java
    
    package com.example.hbasephoenix;
    
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    public class GPSController {
        @Autowired
        private GPSMapper gpsMapper;
    
        @RequestMapping("/")
        public List<GPSModel> index(@RequestParam("id") Integer id) {
            return gpsMapper.findById(id);
        }
    }
    
    vim src/main/java/com/example/hbasephoenix/DemoApplication.java
    
    package com.example.hbasephoenix;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.example.hbasephoenix")
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    ./gradlew bootrun
    
    curl 'http://localhost:8080/?id=1001' | json
    
    [
        {
            "id": 1001,
            "longitude": 118.77807441,
            "latitude": 32.0572355,
            "city": "BeiJing",
            "createdTs": 1608199850
        },
        {
            "id": 1001,
            "longitude": 118.77807441,
            "latitude": 32.0572355,
            "city": "BeiJing",
            "createdTs": 1608219850
        }
    ]
    
    curl 'http://localhost:8080/?id=1002' | json
    
    [
        {
            "id": 1002,
            "longitude": 118.77807441,
            "latitude": 32.0572355,
            "city": "BeiJing",
            "createdTs": 1608229850
        }
    ]
    

    参考

    相关文章

      网友评论

          本文标题:Phoenix开发

          本文链接:https://www.haomeiwen.com/subject/ztctnktx.html